Wim Deblauwe
Wim Deblauwe

Reputation: 26878

Best way to exclude unit test @Configurations in Spring?

In my Spring Boot project, I have a main class annotated with @SpringBootConfiguration. I also have some unit tests that use @SpringApplicationConfiguration that points to an inner class that defines a Spring context for usage in my unit test (using some mocks).

I now want to write an integration test that starts the full context of my application. However, this does not work as it also picks up the Spring contexts that are defined as inner classes in other unit tests.

What would be the best way to avoid that? I did see the exclude and excludeName properties on @SpringBootConfiguration, but I am unsure how to use them.

UPDATE:

Some code to explain the problem more:

My main class:

package com.company.myapp;

@SpringBootApplication
@EnableJpaRepositories
@EnableTransactionManagement
@EntityScan(basePackageClasses = {MyApplication.class, Jsr310JpaConverters.class})
public class MyApplication {
  public static void main(String[] args) {
    SpringApplication.run(MyApplication.class, args);
  }
}

I have a unit test for Spring REST Docs:

package com.company.myapp.controller

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration
@WebAppConfiguration
public class SomeControllerDocumentation {

@Rule
public final RestDocumentation restDocumentation = new RestDocumentation("target/generated-snippets");

  // Some test methods here


  // Inner class that defines the context for the unit test
  public static class TestConfiguration {
    @Bean
    public SomeController someController() { return new SomeController(); }

    @Bean
    public SomeService someService() { return new SomeServiceImpl(); }

    @Bean
    public SomeRepository someRepository() { return mock(SomeRepository.class);
}

So the unit test uses the context defined in the inner class. Now I want a different test that tests if the "normal" application context of the app starts up:

package com.company.myapp;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(MyApplication.class)
@WebAppConfiguration
public class MyApplicationTest {

    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void whenApplicationStarts_thenContextIsInitialized() {
        assertThat(applicationContext).isNotNull();
    }

}

This test will now not only wire the stuff it should, but also the beans from the SomeControllerDocumentation.TestConfiguration inner class. This I want to avoid.

Upvotes: 3

Views: 6318

Answers (3)

Wim Deblauwe
Wim Deblauwe

Reputation: 26878

Since Spring Boot 1.4, the problem can be avoided by annotation the configuration in the unit tests with @TestConfiguration

Upvotes: 1

kryger
kryger

Reputation: 13181

You could use profiles: annotate the relevant configuration beans with @Profile("unit-test") and @Profile("integration-test") and inside the tests specify which profile should be active via @ActiveProfiles.

However, it sounds like you could avoid the problem altogether just by reorganising your configuration structure. It's hard to assume anything without seeing any code, though.

Upvotes: 3

Azat Nugusbayev
Azat Nugusbayev

Reputation: 1421

I think you talk about @Ignore

Upvotes: -2

Related Questions