Spring Boot Testing Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 24: Line 24:


=Annotations=
=Annotations=
Required:


* [[@SpringBootTest]] - this is required, otherwise beans won't get injected in our tests.
* [[@SpringBootTest]] - this is required, otherwise beans won't get injected in our tests.
* [[@WebMvcTest]]
* [[https://kb.novaordis.com/index.php/Spring_Boot_Mockito_Support#.40MockBean|@MockBean]] - automatically injects Mockito mocks.
* [[@RunWith]](SpringRunner.class)
 
* [[@IntegrationTest]], [[@WebIntegrationTest]]
Optional:
<Font color=darkgray>
* [[@WebMvcTest]] more research
* [[@RunWith]](SpringRunner.class) more research
* [[@IntegrationTest]] more research
* [[@WebIntegrationTest]] more research
</Font>


=Spring Boot Mockito Support=
=Spring Boot Mockito Support=

Revision as of 02:44, 3 October 2021

External

Internal

Overview

Dependencies

Use:

dependencies {
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

This started dependency pulls everything that is needed to write JUnit tests.

Annotations

Required:

  • @SpringBootTest - this is required, otherwise beans won't get injected in our tests.
  • [[1]] - automatically injects Mockito mocks.

Optional:

Spring Boot Mockito Support

Spring Boot Mockito Support

The Default Generated XyzApplicationTests

Spring Initializr creates a

@SpringBootTest
class XyzApplicationTests {
  @Test
  void contextLoads() {
  }
}

How does this work? How is this useful? Does it use name conventions?

SpringBoot Test Example

@SpringBootTest
public class MyTests {

    @Autowired
    private SomeComponent someComponentWeWantToTest;

    // ...

}

For an example of how to use Mockito mocks, see:

Spring Boot Mockito Support

Testing Logging Configuration

By default, test logging is executed by Logback.