Not @Autowired when running JUnit

I got a NullPointerException when running JUnit and got stuck.

I should have Autowired on the non-test class side, but I get null.

Sample.java


public class Sample{
   @Autowired
   private Hoge hoge; //← null at test execution
    
   /**The following method*/
} 

Cause

On the test code side, it wasn't Autowired.

SampleTest.java


public class SampleTest{
   @Autowired
   private Hoge hoge; //I forgot
    
   /**The following method*/
} 

It will be injected when Spring is executed, so I thought I didn't have to worry about it, but I have to declare it on the test side as well.

Recommended Posts

Not @Autowired when running JUnit
The case that @Autowired could not be used in JUnit5
Disable display when not logged in
How to not start Flyway when running unit tests in Spring Boot