Junit5 Spring Boot Autowire ComponentScan Not Working

Viewed 9387

My problem: if my test refers to an @Bean declaration in the class listed in @SpringBootTest, autowire works. If it refers to a class automatically @ComponentScanned by the class listed in @SpringBootTest, autowire fails. Outside of testing, my app starts without autowire or componentscan issues, and I can confirm that the service I want to load in my test runs fine from non-test. I'm frustrated as hell. Am I broken, or is Junit5 functionality on Spring Boot 2?

My Test:

@ExtendWith(SpringExtension.class)
@SpringBootTest (classes=MyConfig.class)
public class MyTest {
    // fails to autowire
    @Autowired
    private MyService _mySvc ;

    // succeeds!
    @Autowired @Qualifier ( "wtf" )
    private String _wtf ;

MyConfig:

@EnableWebMvc
@SpringBootApplication ( scanBasePackages = "my.packaging" )
@Configuration
public class MyConfig {

    @Bean
    public String wtf ( ) { return "W T F???" ; }

    // No @Bean for MyService because component scan is nicer in the non-test world
4 Answers

I had the same issue with autowiring not working although the test was starting and the problem was that I was still using the old junit4 @Test annotation. Make sure your test method is annotated with @Test from juni5 package org.junit.jupiter.api.Test.

I think because you have annotated as such:

@SpringBootTest (classes=MyConfig.class)

Spring will only look in MyConfig.class for the appropriate beans and is not able to find one for MyService, however, I presume that Spring will scan all packages for a bean when the application is running normally. This is why it works fine in non-test.

Like @Tudro said, org.junit.jupiter.api.Test is for Junit 5. org.junit.Test is for Junit 4.

Use this below:

@ExtendedWith(SpringExtension.class)
@SpringBootTest

Maybe you could add @ContextConfiguration(classes = ApplicationConfiguration.class)

It would look like this

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import example06.junit.group01.Piano;

@SpringBootTest
@ContextConfiguration(classes = ApplicationConfiguration.class)
public class ApplicationConfigurationTest {
    @Autowired
    private Piano piano;
    
    @Test
    public void shouldTestPiano() {
        System.err.println("Testing JUnit5 > " + piano);
    }
}

My config is a simple class

@Configuration
@ComponentScan
public class ApplicationConfiguration {

}
Related