一、添加依赖
<!--spring-test依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--junit依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
二、编写待测程序(求两个数的较大值)
public class Max {
private int a;
private int b;
public Max(int a, int b) {
this.a = a;
this.b = b;
}
public int getMax() {
return a > b ? a : b;
}
}
三、配置bean
<!--配置一个max的bean-->
<bean id="max" class="com.spring.ioc.Max">
<constructor-arg name="a" value="5"/>
<constructor-arg name="b" value="3"/>
</bean>
四、编写测试程序
//指定单元测试环境
@RunWith(SpringJUnit4ClassRunner.class)
//指定配置文件路劲
@ContextConfiguration(locations = {"/applicationContext.xml"})
public class MaxTest {
//自动注入
@Autowired
private Max max;
@Test
public void getMax() {
assertEquals(5,max.getMax());
}
}
网友评论