我有一个类似的类,
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。在somcode()。
您需要将测试标记为@springboottest,以便在测试中可以使用要使用的弹簧豆。然后,您可以将您想嘲笑的任何东西都标记为@mockbean:
@RunWith(SpringRunner.class)
@SpringBootTest
class AppSaveTest{
@MockBean
AppDaoimpl daoimpl;
@Autowired
AppService appService;
@Autowired
private AppSave appSave;
...
}
或如果要继续使用@injectsmocks:
@RunWith(SpringRunner.class)
@SpringBootTest
class AppSaveTest{
@Mock
AppDaoimpl daoimpl;
@Autowired
AppService appService;
@InjectMocks
@Autowired
private AppSave appSave;
...
}