Spring Boot 2 JUnit#
By default, Spring boot uses Junit 4. To write tests in Junit 5, read this migration guide : Junit 5 with Spring boot 2.
JUnit Test Class#
@RunWith(SpringRunner.class)
– tells JUnit to run using Spring’s testing support. SpringRunner is the new name forSpringJUnit4ClassRunner
. It enables full support of spring context loading and dependency injection of the beans in the tests.@SpringBootTest
– annotation that can be specified on a test class that runs Spring Boot based tests. It provides ability to start a fully running web server listening on any defined or random port.webEnvironment
– enables requests and responses to travel over network and mock servlet container is not involved.@LocalServerPort
– injects the HTTP port that got allocated at runtime.