0

I've created a basic integration test using the standard MockMvc setup for my Spring Boot / Thymeleaf application. My simple test is just to verify that the About page of the website loads. When I run the test and verify status, I get a "java.lang.AssertionError: Status expected:<200> but was:<404>" error

I've tried several different ways to create the MockMvc object. Most of my other attempts left the mockMvc object null, but I can verify that it's being created and is attempting to load the page.

Here's my controller:

    @RequestMapping(value = "/about", method = RequestMethod.GET)
    public String loadAbout(Model model) {
        LOGGER.trace("Loading about page");
        return "about";
    }

Here's my test class:

@Test(groups = { "sboot" })
@WebMvcTest
@WebAppConfiguration
@ImportAutoConfiguration(ThymeleafAutoConfiguration.class)
@ActiveProfiles("dev")
@ContextConfiguration(classes = { WebSecurityConfig.class, WebMvcConfig.class, DatabaseConfiguration.class })
public class UserIT extends AbstractTestNGSpringContextTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Test
    public void AboutPage() throws Exception {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();      
        mockMvc.perform(get("/about"))
                    .andExpect(status().isOk());
    }
}

This is the error message I'm seeing:

10:00:32.382 [DEBUG] [TestEventLogger] Gradle suite > Gradle test > com.eshrsys.sboot.test.integration.UserIT.AboutPage FAILED
10:00:32.382 [DEBUG] [TestEventLogger]     java.lang.AssertionError: Status expected:<200> but was:<404>
10:00:32.382 [DEBUG] [TestEventLogger]         at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:55)
LeeRosenberg
  • 83
  • 1
  • 2
  • 9

1 Answers1

0

I found a solution that appears to be working for me, and I think will be sufficient for me. Instead of a WebApplicationContext, I switched to the standalone setup, added a ViewResolver and my simple test now passes.

Here's my updated test class:

@Test(groups = { "sboot" })
@WebMvcTest
@WebAppConfiguration
@ImportAutoConfiguration(ThymeleafAutoConfiguration.class)
@ActiveProfiles("dev")
@ContextConfiguration(classes = { WebSecurityConfig.class, WebMvcConfig.class, DatabaseConfiguration.class })
public class UserIT extends AbstractTestNGSpringContextTests {

//  @Autowired
//  private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Test
    public void AboutPage() throws Exception {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/templates/");
        viewResolver.setSuffix(".html");

        mockMvc = MockMvcBuilders.standaloneSetup(new EshrsysController()).setViewResolvers(viewResolver).build();
//      this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).setViewResolvers(viewResolver).build();     

        this.mockMvc.perform(get("/about"))
                    .andExpect(status().isOk());
    }
}
LeeRosenberg
  • 83
  • 1
  • 2
  • 9