I am trying to add unit testing for my spring boot application. It doesn't work, and throws 404. I am using 1.2.1 release spring boot and have dependencies for spring-boot-starter-test. Please let know if i am missing something. The spring boot app works without any issue.
Here is my code,
public class Application{
public static void main(String[] args) {//NOSONAR
SpringApplication.run(Application.class, args);
}
@Bean
public ServletRegistrationBean displayListServletRegistrationBean(){
return new ServletRegistrationBean(new DisplayServlet(),"/display");
}
@Bean
public ServletRegistrationBean manageServletRegistrationBean(){
return new ServletRegistrationBean(new ManageServlet(),"/manage");
}
}
My junit test case code,
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-test-context.xml"})
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@WebIntegrationTest({"server.port=0", "management.port=0"})
@ActiveProfiles("scratch")
public class ApplicationTest {
@Autowired
private WebApplicationContext context;
private MockMvc mvc;
@Before
public void setUp() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
@Test
public void testHome() throws Exception {
this.mvc.perform(get("/display")).andExpect(status().isOk());
//this.mvc.perform(get("/")).andExpect(status().isOk());
}
}
The console has
org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/display] in DispatcherServlet with name ''
and Junit result has
java.lang.AssertionError: Status expected:<200> but was:<404> at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)