我有一个类,
class AppSave{
@Autowired
AppDaoimpl daoimpl;
@Autowired
AppService appService;
method1(){daoimpl.fewCode()}
method2(){appService.someCode()}
}
书面测试类,
class AppSaveTest{
@Mock
AppDaoimpl daoimpl;
//What Should I do here So that I can Call someCode() of appService
AppService appService;
@InjectMocks
private AppSave appSave;
test(){
method2()
}
只是为了你的信息使用junit1.4。如果我模拟Appservice然后我可以执行impl。在somCode()中。
答案 0 :(得分:1)
您需要将测试标记为@SpringBootTest,以便在测试中使用您要使用的Spring bean。然后,您可以将您想要模拟的任何内容标记为@MockBean,如下所示:
@RunWith(SpringRunner.class)
@SpringBootTest
class AppSaveTest{
@MockBean
AppDaoimpl daoimpl;
@Autowired
AppService appService;
@Autowired
private AppSave appSave;
...
}
或者如果你想继续使用@InjectMocks:
@RunWith(SpringRunner.class)
@SpringBootTest
class AppSaveTest{
@Mock
AppDaoimpl daoimpl;
@Autowired
AppService appService;
@InjectMocks
@Autowired
private AppSave appSave;
...
}