If you are using JUnit 4 to create tests with the TestContext framework, you will have two options to
access the managed application context. The first option is by implementing the
ApplicationContextAware interface. For this option, you have to explicitly specify a Spring-specific test
runner for running your test—SpringJUnit4ClassRunner. You can specify this in the @RunWith annotation
at the class level.
如果你使用Junit4与TestContext框架做测试,那么有2种选项来访问被管理的Application Context. 第一种就是实现ApplicationContextAware接口,使用这种方式必须要明确指定Spring的Test Runner来运行测试 - SpringJUnit4ClassRunner. 你可以使用类级别的@RunWith注释来指定它
第一种方法
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/beans.xml")
public class AccountServiceJUnit4ContextTests implements ApplicationContextAware {
private static final String TEST_ACCOUNT_NO = "1234";
private ApplicationContext applicationContext;
private AccountService accountService;
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Before
public void init() {
accountService =
(AccountService) applicationContext.getBean("accountService");
accountService.createAccount(TEST_ACCOUNT_NO);
accountService.deposit(TEST_ACCOUNT_NO, 100);
}
@Test
public void deposit() {
accountService.deposit(TEST_ACCOUNT_NO, 50);
assertEquals(accountService.getBalance(TEST_ACCOUNT_NO), 150, 0);
}
@Test
public void withDraw() {
accountService.withdraw(TEST_ACCOUNT_NO, 50);
assertEquals(accountService.getBalance(TEST_ACCOUNT_NO), 50, 0);
}
@After
public void cleanup() {
accountService.removeAccount(TEST_ACCOUNT_NO);
}
}
You can specify the bean configuration file locations in the locations attribute of the
@ContextConfiguration annotation at the class level. These locations are classpath locations relative to
the test class by default, but they support Spring’s resource prefixes. If you don’t specify this attribute
explicitly, the TestContext framework will load the file by joining the test class name with -context.xml
as the suffix (i.e., AccountServiceJUnit4Tests-context.xml) from the same package as the test class.
By default, the application context will be cached and reused for each test method, but if you want it
to be reloaded after a particular test method, you can annotate the test method with the @DirtiesContext
annotation so that the application context will be reloaded for the next test method.
第2种方法
The second option to access the managed application context is by extending the TestContext
support class specific to JUnit 4: AbstractJUnit4SpringContextTests. This class implements the
ApplicationContextAware interface, so you can extend it to get access to the managed application
context via the protected field applicationContext. However, you first have to delete the private field
applicationContext and its setter method. Note that if you extend this support class, you don’t need to
specify SpringJUnit4ClassRunner in the @RunWith annotation, because this annotation is inherited from
the parent.
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
@ContextConfiguration(locations = "/beans.xml")
public class AccountServiceJUnit4ContextTests extends
AbstractJUnit4SpringContextTests {
private static final String TEST_ACCOUNT_NO = "1234";
private AccountService accountService;
@Before
public void init() {
accountService =
(AccountService) applicationContext.getBean("accountService");
accountService.createAccount(TEST_ACCOUNT_NO);
accountService.deposit(TEST_ACCOUNT_NO, 100);
}
...
}