您的位置:首页 > 编程语言 > Java开发

maven下使用junit对spring进行单元测试_01基本应用

2016-12-06 18:10 555 查看

https://my.oschina.net/dlpinghailinfeng/blog/336694

一、开发环境

maven版本:3.0.5

spring版本:spring3.2.3 release

junit版本:4.11

eclipse版本:3.7.2 r2

jdk版本:1.6 

二、文件清单

pom.xml

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.5</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>

</plugins>
</build>


public class Employee {
private Integer age;
private String name;
public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Employee(Integer age,String name){
this.age=age;
this.name=name;
}

@Override
public String toString(){
return "Employee name is "+name+",age is"+age;

}

}


import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:ApplicationContext.xml"})
public class EmpolyeeTest {
@Autowired
ApplicationContext ctx;

@Test
public void testEmployee(){
Employee employee =(Employee) ctx.getBean("employee");
assertEquals("zhangsan",employee.getName());
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: