7

I'm trying to run the mvn integration-test phase and I'm getting Failed to load ApplicationContext error when the integration tests are executed (the unit tests get executed correctly). I'm running my tests with the SpringJUnit4ClassRunner class using.

This is the full stack trace:

2017-02-09 03:22:15.705 [main] ERROR o.s.t.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5c072e3f] to prepare test instance [com.dentilax.app.accountservice.AccountServiceIT@768b970c]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to load an ApplicationContext from [MergedContextConfiguration@71623278 testClass = AccountServiceIT, locations = '{}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]].
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:263)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ... 25 common frames omitted

Also, I'm using an archetype, you can see the annotated Configuration classes here. What am I doing wrong?

This is my Test class:

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class PatientServiceIT {

    private static final String EMAIL = "[email protected]";
    private static final String NAME = "test";
    private static final String SURNAMES = "account";
    private static final String PASSWORD = "testaccount";
    private static final String POSTAL_CODE = "15002";
    private static final String MOBILE_NUMBER = "694749217";

    @Autowired
    private AccountRepository accountRepository;

    @Autowired
    private PatientRepository patientRepository;

    @Autowired
    private PatientService patientService;

    private PatientDetails createPatientDetails() {
        return new PatientDetails(EMAIL, PASSWORD, NAME, SURNAMES, MOBILE_NUMBER, POSTAL_CODE);
    }

    private Account createPatient() {
        Account patientAccount = new Account(EMAIL, PASSWORD, NAME, SURNAMES, Role.ROLE_DENTIST);
        Patient patient = new Patient(POSTAL_CODE, MOBILE_NUMBER);
        patientAccount.setPatient(patient);
        return patientAccount;
    }

    @Test
    public void savePatient() {
        // call
        Patient patient = patientService.save(createPatientDetails());

        // assert
        assertEquals(patient.getAccount(), createPatient());
    }

}

PS: I'm suing maven failsafe for the integration-test goal and surefire por the test goal.

5
  • Are you running your're config fully java driven or do you still have a ApplicationContext.xml? Commented Feb 9, 2017 at 8:18
  • Also can you show your relevant test class, AccountServiceIT? Commented Feb 9, 2017 at 11:07
  • Hi @T.Jung I'm using Java config only. Commented Feb 9, 2017 at 13:21
  • Hi @Morfic I've updated my question with the test code Commented Feb 9, 2017 at 13:23
  • Does this answer your question? Spring testing and maven Commented Oct 29, 2020 at 19:42

1 Answer 1

4

You are missing the definition of the context with the @ContextConfiguration(classes = ...) annotation in your test. As classes you might define single configuration(s) or your whole production application context (that includes all the others). The benefit of declaring just the configuration classes you need is that the whole bootstrapping for the test is faster.

Note: Spring tests cache their specified application context. If you have to run 9/10 tests with the whole config it will take less time to use the whole config again than declaring a new set of context config. But you should aim to get a small config footprint for your integration tests so you can focus on the domain slice you are working in and not have to handle or maintain other context configurations.

In general tests run with SpringJUnit4ClassRunner expect to have an application context to run with. Further reading: Link to the spring docs

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

3 Comments

Hi! I tried to annotate the test class with @ContextConfiguration(classes = Application.class) and it's not working.. If I extends the test class with WebSecurityConfigurationAware works fine, but I don't understand why and I can't find any information on that..
You mean some kind of this github.com/kolorobot/spring-mvc-quickstart-archetype/blob/… (google search). So you are using spring security? If you include the whole application (as mentioned above) you have also to do the right setup with .addFilters(springSecurityFilterChain) (the chain has to be autowired/injected).
Hi, that's working, thanks! But why I need to add a filter to the Spring security chain?

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.