Loading...

Monday, March 9, 2009

Access Spring application context in JUnit 4 tests

Just read the Spring Annotations RefCardz and noticed the neat way to access the Spring application context in a JUnit 4 via autowiring. I read the Spring documentation and learned to extend the AbstractJUnit4SpringContextTests, but this is even easier.

The following two classes will do the same thing, but in the second example we don't have to extend a JUnit class.

package com.mrhaki.spring.test;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

// ApplicationContext will be loaded from "classpath:/com/mrhaki/spring/test/ContextTest-context.xml"
@ContextConfiguration
public class ContextJUnitTest extends AbstractJUnit4SpringContextTests {
    @Test
    public void testContext() {
        Assert.assertNotNull(applicationContext.getBean("test"));
    }
}
package com.mrhaki.spring.test;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationContext will be loaded from "classpath:/com/mrhaki/spring/test/ContextTest-context.xml"
@ContextConfiguration
public class ContextTest {
    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void testContext() {
        Assert.assertNotNull(applicationContext.getBean("test"));
    }
}

6 comments:

Rajasekaran said...

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ContextTest {
@Autowired
private ApplicationContext applicationContext;

@Test
public void testContext() {
Assert.assertNotNull(applicationContext.getBean("test"));
}
}

I am getting above exception

mrhaki said...

@Rajasekaran: I didn't mention you still need to create an Spring configuration XML file. Spring will look for com/mrhaki/spring/test/ContextTest-context.xml in the classpath.

lenny said...

Hello,

I have a configuration file, and I receive the following exception....

SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@787d6a] to prepare test instance [com.pharmcomp.dispenser.core.ContextTest@71dc3d]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:201)
at org.springframework.tes

Can you post your config file and maybe even your files?
Thanks in advance.

Anonymous said...

Thanks for sharing this. Saved my time.

BHANU said...

Hi
I got the application context .
But my requirement is i have to set that context to ApplicationContext class object Here i am getting the classCastException org.dozer.DozerBeanMapper cannot be cast to org.springframework.context.ApplicationContext

Shantanu Sardal said...

Awesome !!

Post a Comment