mockito测试
在企业开发里面,用mock来做测试。可以避免一些依赖的问题。
大概的形式如下
@Before
public void setUp() {
super.setUp();
instance = PowerMockito.spy(sysOrgServiceImpl);
try {
PowerMockito.doReturn(sysOrgMapper)
.when(instance, "getBaseMapper");
MemberModifier.field(SysOrgServiceImpl.class, "baseMapper")
.set(instance, sysOrgMapper);
PowerMockito.doReturn(true)
.when(instance, "remove", any(QueryWrapper.class));
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void selectByOrgName() {
when(sysOrgMapper.selectByOrgName(any(String.class), any(Integer.class)))
.thenReturn(1);
assertThat(instance.selectByOrgName("dev", 1)
, instanceOf(Integer.class));
}