Unit test with mock MVC
Junit test with Mock MVC
@RunWith(SpringJUnit4ClassRunner.class)
@EnableCircuitBreaker
@EnableAspectJAutoProxy
@ContextConfiguration(classes = {ProfileSearchController.class})
@WebMvcTest(ProfileSearchControllerTest.class)
@TestPropertySource({ "classpath:customerprofileservice.properties"
})
//customerprofileservice.properties has configs required for running tests
public class ProfileSearchControllerTest {
@Autowired
ProfileSearchController controller;
@Autowired
private MockMvc mockMvc;
//This is required as Controller to invoking service component
@MockBean
ProfileSearchService searchService;
///ProfileAPI/1.0.0/customers/searchSingle/123456 Post man URL without baseUrl
@Test
public void testGetCustomerProfile() throws Exception {
mockMvc.perform(get("/ProfileAPI/1.0.0/customers/searchSingle/123456")) .andExpect(status().isOk());
}
}
2. create customerprofileservice.properties. In this example,this property file is not required becasue hardcoded url's
ProfileBaseURL=/ProfileAPI/1.0.0
SearchSingleURL=/customers/searchSingle/{customerId}
@RunWith(SpringJUnit4ClassRunner.class)
@EnableCircuitBreaker
@EnableAspectJAutoProxy
@ContextConfiguration(classes = {ProfileSearchController.class})
@WebMvcTest(ProfileSearchControllerTest.class)
@TestPropertySource({ "classpath:customerprofileservice.properties"
})
//customerprofileservice.properties has configs required for running tests
public class ProfileSearchControllerTest {
@Autowired
ProfileSearchController controller;
@Autowired
private MockMvc mockMvc;
//This is required as Controller to invoking service component
@MockBean
ProfileSearchService searchService;
///ProfileAPI/1.0.0/customers/searchSingle/123456 Post man URL without baseUrl
@Test
public void testGetCustomerProfile() throws Exception {
mockMvc.perform(get("/ProfileAPI/1.0.0/customers/searchSingle/123456")) .andExpect(status().isOk());
}
}
2. create customerprofileservice.properties. In this example,this property file is not required becasue hardcoded url's
ProfileBaseURL=/ProfileAPI/1.0.0
SearchSingleURL=/customers/searchSingle/{customerId}
Comments
Post a Comment