【发布时间】:2020-02-21 13:26:10
【问题描述】:
我正在使用 mockmvc 编写我的控制器的测试用例
@Mock
private AService aService;
@InjectMocks
private AController aController;
@BeforeEach
public void init() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders
.standaloneSetup(aController)
.setCustomArgumentResolvers(putAuthenticationPrincipal) // for passing the authentication principal
.build();
}
现在当我尝试测试时
@Test
public void testfunction() throws Exception {
String id = UUID.randomUUID().toString();
Mockito.when(aService.getAccount(Mockito.anyString())).thenThrow(new Exception("not avalible"));
mockMvc.perform(MockMvcRequestBuilders.get("/account/{id}", id)
....
}
在此 aService.getAccount() 没有得到模拟。这就是为什么我没有得到想要的结果..
我不明白为什么在这种情况下模拟函数不起作用。
【问题讨论】: