[英]How to use @InjectMocks along with @Autowired annotation in Junit
[英]Mockito: How to partially use @InjectMocks-Annotation for MockMvc testing?
我需要測試一個控制器,並且在大多數情況下使用MockMvc
仍然足夠。 但是仍然有一些分支很難生成測試用例。 所以我將Mockito
用於存根方法並為這個特殊的測試用例拋出異常。
我的控制器實現了一項服務,設置如下所示:
MyController.class :
@RestController
@RequestMapping("/myTest")
public class MyController {
@Autowired
private MyService myService;
@RequestMapping(value = "/", method = { RequestMethod.POST }, produces = "application/json")
public final int controllerMethod() {
return myService.serviceMethod();
}
}
MyService.class :
@Service
public class MyService {
@PreAuthorize("hasRole('ROLE_ADMIN')")
public int serviceMethod() {
return 5;
}
}
所以我有多個測試方法,在某些方法中我模擬MyService.class
以拋出異常。 但在其他方法中,我想測試正常功能。 所以這里出現了問題。 如果測試沒有以正確的順序運行,正常的功能測試將無法工作,因為MyService.class
仍然被模擬,我猜。 所以在這個例子中,serviceMethod 返回 null 而不是 5。
有沒有辦法部分 InjectMocks - 所以我的意思是只針對特殊方法而不是類?
這是示例代碼:
MyControllerTest.class :
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
@WebAppConfiguration
public class MyControllerTest {
@Autowired
private WebApplicationContext webApplicationContext;
@Autowired
MyService myService;
@InjectMocks
@Resource
MyController myController;
@Resource
private FilterChainProxy springSecurityFilterChain;
private MockMvc mockMvc;
@Before
public void setup() throws SQLException {
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
}
@Test
@WithMockUser(username = "validUser", roles = { "ADMIN" })
public void testMyController() throws Exception {
mockMvc.perform(post("/myTest/").with(csrf())).andDo(print()).andExpect(status().is2xxSuccessful()).andExpect(jsonPath("$", is(5)));
}
@Test
@WithMockUser(username = "validUser", roles = { "ADMIN" })
public void testMyControllerException() throws Exception {
myService = Mockito.spy(MyService.class);
MockitoAnnotations.initMocks(this);
Mockito.doThrow(new IndexOutOfBoundsException()).when(myService).serviceMethod();
mockMvc.perform(post("/myTest/").with(csrf())).andDo(print()).andExpect(status().is4xxClientError());
Mockito.verify(myService, Mockito.times(1)).serviceMethod();
}
}
因此,如果我只運行其中一項測試,則兩者都運行良好。 但是,如果我同時運行並且testMyControllerException()
將首先執行,那么testMyController()
由於testMyController()
的 myService-Instance 而失敗,我猜。
有沒有辦法解決這個問題,或者我必須把這個測試分成兩個不同的測試類?
我試圖找到unmocking的一種方式myController.class
在@After
與-Annotation Mockito.reset(myController);
. 但沒有為我工作。
對於 jUnit4,您可以使用@FixMethodOrder(MethodSorters.NAME_ASCENDING)
https://github.com/junit-team/junit4/wiki/Test-execution-order
無論如何,我個人更喜歡在不同的課程中使用它。
要獨立於執行順序並且不嚴格執行順序,最好的方法是使用類注釋@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
。 對於每個測試,它都會生成一個 newApplicationContext。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.