I have an application using spring-boot and an embedded Tomcat container.
As far as I can tell, my code is the same as the spring-boot sample project. However, when I run my test, I get a 404 instead of a 200 (in the case where I try to post instead of get I receive a 405, which is consistent with Tomcat being incorrectly setup):
Failed tests:
UserControllerTest.testMethod:45 Status expected:<200> but was:<404>
My Java-based configuration (some configuration classes omitted):
@Configuration
@ComponentScan
@EnableAutoConfiguration
@Import({ ServiceConfig.class, DefaultRepositoryConfig.class })
public class ApplicationConfig {
private static Log logger = LogFactory.getLog(ApplicationConfig.class);
public static void main(String[] args) {
SpringApplication.run(ApplicationConfig.class);
}
@Bean
protected ServletContextListener listener() {
return new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent sce) {
logger.info("ServletContext initialized");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
logger.info("ServletContext destroyed");
}
};
}
}
UserController.java:
@RestController
@RequestMapping("/")
public class UserController {
@Autowired
UserService userService;
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<String> testMethod() {
return new ResponseEntity<>("Success!", HttpStatus.OK);
}
}
UserControllerTest.java:
RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {ApplicationConfig.class})
public class UserControllerTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}
@Test
public void testMethod() throws Exception {
this.mockMvc.perform(get("/")).andExpect(status().isOk());
}
}
Is there something basic I'm missing? I'm not providing my own Mvc configuration, and I haven't touched the Spring MVC DispatcherServlet, so I assume spring-boot will configure Tomcat automatically.
Spring Boot is a popular Java-based framework to develop microservices. By default, the Spring Tool Suite (STS) IDE -- which is used to build a Spring Boot application -- will automatically create an embedded Tomcat server with the microservices developed each time a build or deployment occurs.
By default, Spring Boot provides an embedded Apache Tomcat build. By default, Spring Boot configures everything for you in a way that's most natural from development to production in today's platforms, as well as in the leading platforms-as-a-service.
We can start Spring boot applications in an embedded tomcat container that comes with some pre-configured default behavior via a properties file. In this post, we will learn to modify the default tomcat configurations via overriding respective properties in application.
It turns out the issue was the component scanning configuration. Even though the @ComponentScan
annotation was used, the controller was under a separate package, so Spring never included it in the dispatcher.
Adding @ComponentScan(basePackages = "com.my.controller"))
solved my issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With