UnitTest:spring junit mockito 使用
1、配置pom.xml,加入junit,mockito
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<!--表示开发的时候引入,发布的时候不会加载此包 -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
2、使用junit测试
测试service层,该方法实现通过项目id去获取项目表的信息,数据库为mysql,Dao层通过mybatis与数据库进行连接。
@RunWith(SpringJUnit4ClassRunner.class) 用于配置spring中测试的环境
@ContextConfiguration(Locations="../*.xml") 用于指定配置文件所在的位置
@Test标注在方法前,表示其是一个测试的方法
Assert 断言,判断实际值与实际值是否一致
运行结果@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:config/spring.xml",
"classpath:config/spring-mybatis.xml" })
public class TestProjectService {
@Autowired(required = true)
private ProjectNameService projectNameService;
@Test
public void testQueryById1() {
ProjectName projectName = projectNameService.getProjectNameById(1);
System.out.print(projectName.getProjectname());
Assert.assertEquals("baseinfo",projectName.getProjectname());
}
}
3、使用mockito 进行测试
如果说我的mysql 出现异常或者说我想隔离系统,这时候可以用到mockito,对于java 还可以选择jmock,easymock等。
3.1 mock
Mock不是真实的对象,创建一个该类型的class的虚拟对象,并可以设置对象行为
InjectMocks创建这个类的对象并自动将标记@Mock、@Spy等注解的属性值注入到这个中
MockitoAnnotations.initMocks(this); 初始化标注有@Mock,@InjectMocks,@Spy
Mockito.when().thenReturn();设置mock对象的行为
运行结果@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:config/spring.xml",
"classpath:config/spring-mybatis.xml" })
public class TestProjectServiceMockito {
@InjectMocks
@Autowired
private ProjectNameService projectNameService;
@Mock
ProjectNameMapper projectNameMapper;
@Before
public void setup(){
MockitoAnnotations.initMocks(this);
}
@Test
public void testMock(){
ProjectName projectName = new ProjectName();
projectName.setDate(new Date(2016-01-01));
projectName.setProjectname("hello");
projectName.setProjectid(1);
Mockito.when(projectNameMapper.selectByPrimaryKey(1)).thenReturn(projectName);
ProjectName prjName = projectNameService.getProjectNameById(1);
System.out.print("返回mock的值:"+prjName.getProjectname());
Assert.assertEquals("hello",prjName.getProjectname());
}
}
3.2 spy
Spy是一个真实的对象,但它可以设置对象行为,不指定行为时调用真实方法
通过Mockito.doReturn().when() 来指定行为
Mockito.reset 清楚各种的交互或者预设值
运行结果@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:config/spring.xml",
"classpath:config/spring-mybatis.xml" })
public class TestProjectServiceSyp {
@Spy
@Autowired
private ProjectNameService projectNameService;
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
}
@Test
public void testSyp1(){
/*
不指定 spy() 返回默认
*/
ProjectName prjName = projectNameService.getProjectNameById(1);
System.out.print("不指定spy(),返回默认值,调用真实方法:"+prjName.getProjectname()+"\n");
Assert.assertEquals("baseinfo",prjName.getProjectname());
}
@Test
public void testSyp2(){
/*
指定spy() 返回指定值
通过 reset() 充值 返回真实值
*/
ProjectName projectName = new ProjectName();
projectName.setDate(new Date(2016-01-01));
projectName.setProjectname("hello");
projectName.setProjectid(1);
Mockito.doReturn(projectName).when(projectNameService).getProjectNameById(1);
ProjectName prjName = projectNameService.getProjectNameById(1);
System.out.print("spy()预设值,返回桩实现:"+prjName1.getProjectname()+"\n");
Assert.assertEquals("baseinfo",prjName1.getProjectname());
Mockito.reset(projectNameService);
ProjectName prjName1 = projectNameService.getProjectNameById(1);
System.out.print("通过reset(),清除预设值:" + prjName.getProjectname()+"\n");
Assert.assertEquals("hello",prjName.getProjectname());
}
}
扫一扫,关注TestDev