我对mockito不是新手,但这次我在工作中发现了一个有趣的案例 . 我希望你能帮助我 .
我需要注入mock来改变测试期间的某些方法行为 . 问题是,bean结构是嵌套的,并且这个bean在其他bean中,不能从test方法访问 . 我的代码看起来像这样:
@Component
class TestedService {
@Autowired
NestedService nestedService;
}
@Component
class NestedService {
@Autowired
MoreNestedService moreNestedService;
}
@Component
class MoreNestedService {
@Autowired
NestedDao nestedDao;
}
@Component
class NestedDao {
public int method(){
//typical dao method, details omitted
};
}
所以在我的测试中,我希望调用NestedDao.method来返回模拟的答案 .
class Test {
@Mock
NestedDao nestedDao;
@InjectMocks
TestedService testedSevice;
@Test
void test() {
Mockito.when(nestedDao.method()).thenReturn(1);
//data preparation omitted
testedSevice.callNestedServiceThatCallsNestedDaoMethod();
//assertions omitted
}
}
我试过做一个initMocks:
@BeforeMethod
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
还要在我的测试类上添加注释:
@RunWith(MockitoJUnitRunner.class)
总是从方法中得到无效指针或错误的答案(不是嘲笑) .
我猜这是嵌套调用的错误,因此无法模拟这个Dao . 我也读过@InjectMocks只适用于setter或构造函数注入,我很遗憾(在私有字段上只是@Autowire),但是当我尝试时它没有用 .
有什么猜测我错过了什么? ;)