5

I have an integration test class annotated with @SpringBootTest which starts up the full application context and lets me execute my tests. However I am unable to @Autowired beans into the test class itself. Instead I get an error:

No qualifying bean of type 'my.package.MyHelper' available".

If I do not @Autowire my helper class, but keep the code directly inside the setUp function, the test works as expected.

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
public class CacheControlTest {

    @Autowired
    private MyHelper myHelper;

    @Before
    public void setUp() {
        myHelper.doSomeStuff();
    }

    @Test
    public void test1() {
        // My test
    }
}

How can I make use of Spring autowiring inside the test class while also using @SpringBootTest?

Following @user7294900 advice below, creating a separate @Configuration file and adding this at the top of CacheControlTest works:

@ContextConfiguration(classes = { CacheControlTestConfiguration.class })

However is there any way of keeping the configuration inside the CacheControlTest class itself? I have tried adding inside my test class:

public class CacheControlTest {

    @TestConfiguration
    static class CacheControlTestConfiguration {
        @Bean
        public MyHelper myHelper() {
            return new MyHelper();
        }
    }

}

And

public class CacheControlTest {

    @Configuration
    static class CacheControlTestConfiguration {
        @Bean
        public MyHelper myHelper() {
            return new MyHelper();
        }
    }
}

But they do not seem to have any effect. I still get the same error. The same configuration block works when placed in an separate file as mentioned above though.

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
Tarmo
  • 1,136
  • 13
  • 28
  • 2
    Do you have a bean of type `MyHelper`? Where is it defined? – Boris the Spider Dec 17 '18 at 09:30
  • 1
    The bean is defined under test classes in the package my.helpers. It is not available in the normal application context - should be available only in test context – Tarmo Dec 17 '18 at 09:31
  • 1
    Well there you go - you need to define a test `@Configuration` and import it. – Boris the Spider Dec 17 '18 at 09:32
  • I have updated the question with the @Configuration blocks I have tried. They do not seem to have an effect – Tarmo Dec 17 '18 at 09:36
  • Where did you put that block? I do question the wisdom of complicating tests by using Spring to autowire test helpers - what's the thinking behind that? – Boris the Spider Dec 17 '18 at 09:39
  • 1
    https://stackoverflow.com/a/50643036/2071828 – Boris the Spider Dec 17 '18 at 09:40
  • I want to use a helper class to encapsulate the DB connection creation and DB setup calls in one place so that they can be reused in several tests. – Tarmo Dec 17 '18 at 09:46
  • Why does that need to involve Spring? This seems like over engineering your tests to me. – Boris the Spider Dec 17 '18 at 10:00
  • I guess I could load the application.yml file for retrieving my DB connection parameters manually but everywhere else I just load the configuration using @Value annotations and doing it the same way in this one case did not feel like overengineering.. just common sense. – Tarmo Dec 17 '18 at 10:02

1 Answers1

5

Add ContextConfiguration for your Test Class:

@ContextConfiguration(classes = { CacheControlTestConfiguration.class })
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • 1
    One shouldn't need this work modern Spring Boot - nested configuration classes should be picked up automatically. – Boris the Spider Dec 17 '18 at 09:37
  • If possible I'd like to keep the configuration inside the test class itself. – Tarmo Dec 17 '18 at 09:41
  • Actually this works.. If I place my configuration in a separate file and add this line to my Test class, my test works as expected. However is there any way to do this while also keeping the configuration inside the test file? – Tarmo Dec 17 '18 at 10:12