This class is at the top of my test hierarchy:
@TestPropertySource("/test.properties") @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest public abstract class ApplicationAbstractTest { }
And a few more test classes:
@WebAppConfiguration @ActiveProfiles("mysql") abstract public class AbstractControllerTest extends ApplicationAbstractTest { protected MockMvc mockMvc; @Autowired private WebApplicationContext webApplicationContext; @PostConstruct private void postConstruct() { mockMvc = MockMvcBuilders .webAppContextSetup(webApplicationContext) .apply(springSecurity()) .build(); } }
JsonUserServiceTest:
@ActiveProfiles("json") public class JsonUserServiceTest extends ApplicationAbstractTest { @Before public void setUp() throws Exception { ... } }
ContactControllerTest:
public class ContactControllerTest extends AbstractControllerTest { @Test public void testGet() throws Exception { mockMvc.perform(get("/update-" + ID + "-contact") .with(userAuth(USER)))
So, when I run ContactControllerTest along - it succeeds, and the print method shows me:
Handler: Type = com.telecom.web.ContactController Method = public java.lang.String com.myApp.web.ContactController.details(java.lang.Integer,org.springframework.ui.ModelMap)
But when I run all the tests, so the JsonUserServiceTest works first, the ContactControllerTest fails. And print shows:
Handler: Type = null ... java.lang.AssertionError: No ModelAndView found
What is wrong with the configuration? Or how to fix it?
UPD: at the same time, a test like this always works fine:
public class UserControllerTest extends AbstractControllerTest { @Test public void testRegister() throws Exception { mockMvc.perform(get("/register")) .andDo(print()) .andExpect(view().name("profile")) .andExpect(forwardedUrl("/WEB-INF/jsp/profile.jsp")); } }
UPD: There is a controller method that I am testing:
@GetMapping("/update-{id}-contact") public String details(@PathVariable Integer id, ModelMap model) { Integer userId = AuthorizedUser.id(); LOG.info("get contact {} for User {}", id, userId); Contact contact = service.get(id, userId); model.addAttribute("contact", contact); return "details"; }
I also have a bean:
@Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/jsp/"); viewResolver.setSuffix(".jsp"); return viewResolver; }
UPD: I tried setting up mockMvc in a separate class:
@Configuration public class TestConfig { @Autowired private WebApplicationContext webApplicationContext; @Bean public MockMvc mockMvc() { return MockMvcBuilders .webAppContextSetup(webApplicationContext) .apply(springSecurity()) .build(); } }
And added it here:
@WebAppConfiguration @ContextConfiguration(classes = {TestConfig.class}) @ActiveProfiles("mysql") abstract public class AbstractControllerTest extends ApplicationAbstractTest {
but i got:
java.lang.IllegalStateException: springSecurityFilterChain cannot be zero. Ensure that a Bean named springSecurityFilterChain that implements the filter is present or enters the filter to be used.