10

Hi I am trying to so spring junit test cases... and I require my full application context to be loaded. However the junit test does not initialize the full application context.

Test class:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class MongoDbRepositoryTest {

    @Value("${spring.datasource.url}")
    private String databaseUrl;

    @Inject
    private ApplicationContext appContext;

    @Test
    public void testCRUD() {
        System.out.println("spring.datasource.url:" + databaseUrl);
        showBeansIntialised();
        assertEquals(1, 1);
    }

    private void showBeansIntialised() {
        System.out.println("BEEEAAANSSSS");
        for (String beanName : appContext.getBeanDefinitionNames()) {
            System.out.println(beanName);
        }
    }

Output:

spring.datasource.url:${spring.datasource.url}
BEEEAAANSSSS
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.internalPersistenceAnnotationProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor

Main Application Class Annotations:

@ComponentScan(basePackages = "com.test")
@EnableAutoConfiguration(exclude = { MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class })
@EnableMongoRepositories("com.test.repository.mongodb")
@EnableJpaRepositories("com.test.repository.jpa")
@Profile(Constants.SPRING_PROFILE_DEVELOPMENT)
public class Application { ...

Hence it should scan all the spring bean in the package com.test and also load them into the applicationcontext for the Junit testcase. But from the output of the beans initalised it doesnt seem to be doing this.

3
  • 1
    For one thing, your application class is only active for a profile which you're not setting to be active in the test. Commented Feb 25, 2015 at 9:41
  • using @Inject is discouraged - this annotation stems from EE and has its own usecase. You will never be able to inject a spring ApplicationContext into a pure EE-app ... Use @Autowired, which is Spring-specific and has its own lifecycle which will never be dropped Commented Jan 5, 2023 at 11:35
  • Both Inject and Autowired are effective ways to inject dependencies into Spring components, and the choice between them often comes down to personal preference or the specific needs of your application. Commented Feb 23, 2023 at 16:55

3 Answers 3

7

You need to annotate your test class with @ActiveProfiles as follows; otherwise, your Application configuration class will always be disabled. That's why you currently do not see any of your own beans listed in the ApplicationContext.

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@ActiveProfiles(Constants.SPRING_PROFILE_DEVELOPMENT)
public class MongoDbRepositoryTest { /* ... */ }

In addition, Application should be annotated with @Configuration as was mentioned by someone else.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Sam..that resolved my issue! Also thanks for your contributions to Spring Recipes ..it was one of my first resources for learning Spring!
0

Are you maybe missing an @Configuration annotation on your Application class?

2 Comments

Even after adding @Configration to my Application.class it didnt work.
Since I couldn't comment, I posted an answer. Noted for future reference.
0

Adding @ActiveProfile in each test class you cant scale better added this in VM options

-Dspring.profiles.active=test

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.