Spring Boot Testing Concepts: Difference between revisions

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


[[@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]]
* [[@RunWith]](SpringRunner.class)
* [[@IntegrationTest]], [[@WebIntegrationTest]]


[[@WebMvcTest]]
=Spring Boot Mockito Support=
 
{{Internal|Spring Boot Mockito Support|Spring Boot Mockito Support}}
[[@RunWith]](SpringRunner.class)
 
[[@IntegrationTest]], [[@WebIntegrationTest]]


=The Default XyzApplicationTests=
=The Default XyzApplicationTests=

Revision as of 01:13, 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

Spring Boot Mockito Support

Spring Boot Mockito Support

The Default 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

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTests {

    @Autowired
    private SomeComponent someComponentWeWantToTest;

    // ...

}

Testing Logging Configuration

By default, test logging is executed by Logback.