Spring Boot Testing Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * Spring Boot Concepts * Spring Framework Testing Concepts =Ov...")
 
 
(15 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
* https://www.baeldung.com/spring-boot-testing
* https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4
* https://www.baeldung.com/injecting-mocks-in-spring
* https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.testing
=Internal=
=Internal=
* [[Spring_Boot_Concepts#Testing|Spring Boot Concepts]]
* [[Spring_Boot_Concepts#Testing|Spring Boot Concepts]]
* [[Spring_Framework_Testing_Concepts#Spring_Boot_Testing_Concepts|Spring Framework Testing Concepts]]
* [[Spring_Framework_Testing_Concepts#Spring_Boot_Testing_Concepts|Spring Framework Testing Concepts]]
Line 6: Line 11:
=Overview=
=Overview=


 
=Dependencies=
{{Internal|Spring Framework Testing Concepts|Spring Framework Testing Concepts}}
 
<font color=darkgray>Process: https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4</font>


Use:
Use:
Line 21: Line 23:
This started dependency pulls everything that is needed to write JUnit tests.
This started dependency pulls everything that is needed to write JUnit tests.


[[@SpringBootTest]] - this is required, otherwise beans won't get injected in our tests.
=Annotations=


[[@WebMvcTest]]
Required:


[[@RunWith]](SpringRunner.class)
* [[@SpringBootTest]] - this is required, otherwise beans won't get injected in our tests.
* [[Spring_Boot_Mockito_Support#.40MockBean|@MockBean]] - automatically injects Mockito mocks.


[[@IntegrationTest]], [[@WebIntegrationTest]]
Optional:
<Font color=darkgray>
* [[@WebMvcTest]] more research
* [[@RunWith]](SpringRunner.class) more research
* [[@IntegrationTest]] more research
* [[@WebIntegrationTest]] more research
</Font>


==SpringBoot Test Example==
=Spring Boot Mockito Support=
{{Internal|Spring Boot Mockito Support|Spring Boot Mockito Support}}
 
=The Default Generated XyzApplicationTests=
 
Spring Initializr creates a
<syntaxhighlight lang='java'>
@SpringBootTest
class XyzApplicationTests {
  @Test
  void contextLoads() {
  }
}
</syntaxhighlight>
 
<font color=darkgray>How does this work? How is this useful? Does it use name conventions?</font>
 
=SpringBoot Test Example=


<syntaxhighlight lang='java'>
<syntaxhighlight lang='java'>
@SpringBootTest
@SpringBootTest
@RunWith(SpringRunner.class)
public class MyTests {
public class MyTests {


Line 44: Line 69:
</syntaxhighlight>
</syntaxhighlight>


==Testing Logging Configuration==
For an example of how to use Mockito mocks, see: {{Internal|Spring_Boot_Mockito_Support#Overview|Spring Boot Mockito Support}}
 
=Testing Logging Configuration=


By default, test logging is executed by [[Logback]].
By default, test logging is executed by [[Logback]].
==Spring Boot Mockito Support==
{{Internal|Spring Boot Mockito Support|Spring Boot Mockito Support}}

Latest 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.
  • @MockBean - 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.