Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@SpringBootTest for a non-spring-boot application

I am creating a non-spring-boot application using spring-rest, spring-data-jpa etc and I would like to do integration testing using spring boot (1.4.1.RELEASE). Note that I don't have a SpringApplication class or @SpringApplication annotation anywhere

On my test class I have

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MyConfiguration.class)
public class MyIT { }

@RestController
public class MyController { }

This is starting an embedded tomcat and I can see that my controller is being initialized, however, I get a 404 when calling my service using TestRestTemplate. It appears that DispatcherServlet does not seem to know about my controllers

Also, I had to define a servletContainer bean as follows

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
    factory.setPort(9000);
    factory.setSessionTimeout(10, TimeUnit.MINUTES);
    return factory;
}

Am I missing any configuration for Spring to have my controllers visible to embedded tomcat? I tried using @EnableAutoConfiguration @ComponentScan on the test class but they don't have any effect. I have wasted two days on this and any hints are greatly appreciated!!

Complete MyIT class

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = { TestContextConfiguration.class })
public class MyIT {

@Value("${local.server.port}")
private int serverPort;

@Resource
private TestRestTemplate restTemplate;

@Test
public void test() throws Exception {
    System.out.println("Port:" + serverPort);
    System.out.println("Hello:" + this.restTemplate.getForEntity("/", String.class));
}

}

Controller class

@RestController
public class MyController {

    @GetMapping("/")
    public String hello() {
        System.out.println("Hello called");
        return "Hello";
    }

}

Output of the test

Port:9000
Hello:<404 Not Found,<!DOCTYPE html><html><head><title>Apache Tomcat/8.5.5 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 404 - /</h1><div class="line"></div><p><b>type</b> Status report</p><p><b>message</b> <u>/</u></p><p><b>description</b> <u>The requested resource is not available.</u></p><hr class="line"><h3>Apache Tomcat/8.5.5</h3></body></html>,{Content-Type=[text/html;charset=utf-8], Content-Language=[en], Content-Length=[992], Date=[Wed, 05 Oct 2016 14:26:46 GMT]}>
like image 779
ashburnite Avatar asked Oct 04 '16 17:10

ashburnite


People also ask

Can we use Mockito without spring?

Mocking with Mockito (and without Spring) As a mocking framework, we'll use Mockito, since it's well-rounded, well-established, and well-integrated into Spring Boot. But the best kind of test doesn't use Spring at all, so let's first look at how to use Mockito in a plain unit test to mock away unwanted dependencies.

What is @SpringBootTest used for?

The @SpringBootTest annotation is useful when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests. We can use the webEnvironment attribute of @SpringBootTest to configure our runtime environment; we're using WebEnvironment.

Can we use spring boot for non Spring application?

The application can also be called as Spring Boot Standalone application. To develop a non web application, your Application needs to implement CommandLineRunner interface along with its run method. This run method acts like a main of your application.

When we should not use spring boot?

When you have a large number of different web apps, it can make sense to let the knowledge on that part only in the production team. In that case, you would not use Spring boot. On the other end, if the hosting is externalized, Spring boot allows to give a full package.


1 Answers

This can be achieved by creating a TestConfig inner class and annotate it with @SpringBootConfiguration like:

@SpringBootConfiguration
public static class TestConfig {
}

Then, in your test class:

@RunWith(SpringRunner.class)
@SpringBootTest(...)
@Import(MyIT.TestConfig.class)
public class MyIT {

    @SpringBootConfiguration
    @ComponentScan("com.example")
    public static class TestConfig {
    }
}

Note that TestConfig class also has @ComponentScan annotation. This is used by Spring to find your applicacion beans.

like image 111
mpianello Avatar answered Oct 23 '22 02:10

mpianello