6

I am developing a spring boot application and write some junit test.

But I find when I run any tests, tomcat is also started up, It makes those tests very slow and waste many times.

When I develop a SpringMvc application, junit test can run without start tomcat, It saves many times.

So, I want to ask it there anyway to run springboot test with out start tomcat?

Shuai Li
  • 2,426
  • 4
  • 24
  • 43
  • Can you post the logs where you detect that tomcat is getting starts ?? – DEBENDRA DHINDA May 06 '19 at 09:22
  • Can you provide us snippets of your test class? My tests are annotated with @RunWith(SpringRunner.class) and tomcat is not started when running mvn test – JavaBoy May 06 '19 at 09:31

2 Answers2

7

Running a test with @SpringBootTest does not start an embedded server by default. By default, it runs in the MOCK environment.

By default, @SpringBootTest will not start a server. You can use the webEnvironment attribute of @SpringBootTest to further refine how your tests run:

MOCK(Default) : Loads a web ApplicationContext and provides a mock web environment. Embedded servers are not started when using this annotation. If a web environment is not available on your classpath, this mode transparently falls back to creating a regular non-web ApplicationContext. It can be used in conjunction with @AutoConfigureMockMvc or @AutoConfigureWebTestClient for mock-based testing of your web application.

Documentation link: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications

I guess what you wanted to achieve could be achieved by Slice Test concept. In general, you don't need a full-fledged mock environment or environment with an embedded server with all the configured beans in the spring container when you are performing unit tests.

For e.g. you have to unit test your Controller then you have @WebMvcTest annotation in place that will configure only web related beans and ignore the rest of the beans.

To test whether Spring MVC controllers are working as expected, use the @WebMvcTest annotation. @WebMvcTest auto-configures the Spring MVC infrastructure and limits scanned beans to @Controller, @ControllerAdvice, @JsonComponent, Converter, GenericConverter, Filter, WebMvcConfigurer, and HandlerMethodArgumentResolver. Regular @Component beans are not scanned when using this annotation.

Documentation link: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-mvc-tests

Similarly, for the database layer, there is @DataJpaTest

Documentation link: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test

Long story short: when you intend to do unit testing with Spring framework, slice test is the one you should use in most of the cases.

Kajzer
  • 2,138
  • 3
  • 32
  • 46
Dhruv
  • 10,291
  • 18
  • 77
  • 126
-2

If you are placing the following annotations, this will start the embedded container...

@RunWith(SpringRunner.class)
@SpringBootTest

Because, if you see the SpringBootTestContextBootstrapper.class class , this has been invoked the container which is invoked by @BootstrapWith(SpringBootTestContextBootstrapper.class) when we specify @SpringBootTest

You can remove those and can do as follows:

import org.junit.Test;    
public class HellotomApplicationTests {    
    @Test
    public void contextLoads() {
    }

}

R-Click and RunAs Junit

O/P enter image description here

DEBENDRA DHINDA
  • 1,163
  • 5
  • 14