首先导入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
代测代码
public class Max {
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
private int a;
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
private int b;
public Max(int a,int b){
this.a=a;
this.b=b;
}
public int getmax(){
return a>b?a:b;
}
}
bean的配置
<bean id="max" class="com.spring.IoC.work1.Max">
<constructor-arg name="a" value="2"/>
<constructor-arg name="b" value="5"/>
</bean>
测试代码
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
//指定单元测试环境
@RunWith(SpringJUnit4ClassRunner.class)
//指定配置文件路径
@ContextConfiguration(locations = {"/spring.xml"})
public class MaxTest {
//自定注入Max
@Autowired
private Max max;
@Test
public void getMax() {
assertEquals(5,max.getmax() );
}
}
网友评论