[JAVA] [Spring Boot] Until @Autowired is run in the test class [JUnit5]

things to do

With SpringBoot + JUnit5, do it until the constructor pattern injection is moved at the time of testing.


It worked if I gave @ExtendWith (SpringExtension.class) and @SpringBootTest.

Sample code

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@DisplayName("Test to move constructor pattern")
class AutowiredTest {

    private final AutowiredClass autowiredClass;

    private final String hoge;
    private final String fuga;

    AutowiredTest(AutowiredClass autowiredClass) {
        this.autowiredClass = autowiredClass;

        hoge = "String";
        fuga = "String";

    @DisplayName("Some test")
    void uploadFile() {
        assertEquals(hoge, fuga);


Since it is JUnit5, you can write various things in the class without adding public. Also, the constructor will inject even private.

Recommended Posts

[Spring Boot] Until @Autowired is run in the test class [JUnit5]
What is @Autowired in Spring boot?
Test field-injected class in Spring boot test without using Spring container
Write test code in Spring Boot
[Spring Boot] Mock the contents that are autowired outside the scope [JUnit 5]
Form class validation test with Spring Boot
Autowired fields in a class that inherits TextWebSocketHandler in Spring Boot become NULL
Run a Spring Boot project in VS Code
View the Gradle task in the Spring Boot project
Get the path defined in Controller class of Spring boot as a list
Specify the encoding of static resources in Spring Boot
I want to get the information of the class that inherits the UserDetails class of the user who is logged in with Spring Boot.
[JUnit 5 compatible] Write a test using JUnit 5 with Spring boot 2.2, 2.3
[JUnit 5] Write a validation test with Spring Boot! [Parameterization test]
Until you start development with Spring Boot in eclipse 1
Until you start development with Spring Boot in eclipse 2
I wrote a test with Spring Boot + JUnit 5 now
Why the get method is needed in the Calendar class
How to use the same Mapper class in multiple data sources with Spring Boot + MyBatis
Spring Boot 1.x will reach EOL in the next year.
The case that @Autowired could not be used in JUnit5
Use Spring Test + Mockito + JUnit 4 for Spring Boot + Spring Retry unit tests
Set context-param in Spring Boot
Spring Boot 2 multi-project in Gradle
Run LIFF with Spring Boot
What is the BufferedReader class?
Test private methods in JUnit
Test private methods in JUnit
[JUnit] Test the thrown exception
Major changes in Spring Boot 1.5
Until the code is executed
RSocket is supported in Spring Boot 2.2, so give it a try
Be aware that the default for Spring boot redirect is http
ProxyFactory is convenient when you want to test AOP in Spring!
Get a proxy instance of the component itself in Spring Boot
See the relative redirect behavior with the server.tomcat.use-relative-redirects setting in Spring Boot
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
Let's write a test code for login function in Spring Boot
Spring Boot application development in Eclipse
Spring Boot for the first time
How to run JUnit in Eclipse
JUnit 5 parameterization test is super convenient
Until "Hello World" with Spring Boot
Implement REST API in Spring Boot
[Spring Boot] Role of each class
Use DBUnit for Spring Boot test
Event processing is performed in Spring.
Output JUnit test report in Maven
What are the rules in JUnit?
Test Spring framework controller with Junit
Thymeleaf usage notes in Spring Boot
Spring.messages.fallback-to-system-locale: false is required to default message.properties for i18n support in Spring boot
Procedure to make the value of the property file visible in Spring Boot
Error when the member of Entity class used in SpringWebFlux is final