Failed to load ApplicationContext when running Integration Tests

自闭症网瘾萝莉.ら 提交于 2019-12-11 03:56:05

问题


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 = "account@test.com";
    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.


回答1:


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



来源:https://stackoverflow.com/questions/42127297/failed-to-load-applicationcontext-when-running-integration-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!