/**
*
* Description:
*
* @RunWith - JUnit annotation that specifies that the Spring class runner
* should be used for running the test.
* @ContextConfiguration - Specifies the location of your Spring application
* context xml file(s). This gets inherited by subclasses,
* so you can use it in a base class to configure all of
* the tests in your application.
* @Test - Indicates that the method should be run as a test. Note that although
* I used the pre JUnit 4 naming conventions (class names postfixed with
* "Test" and method names prefixed with "test"), this is not a
* requirement.
* @Ignore - When this annotation is added, the associated test method will not
* be run. This is a great alternative to commenting out the methods
* which was required prior to JUnit 4. In this case, the test would
* fail if the
* @Ignore annotation wasn't used because objectUnderTest.getSomethingFalse()
* does not equal true.
* @Autowired - Indicates that Spring should "wire" this dependency when
* initializing the test. In this case, a Spring bean named
* objectUnderTest will be provided.
*
*
* 要创建一个基于Spring2.5的JUnit4.4测试用例其实相当简单,主要进行以下几步的工作:
* (1)创建一个扩展自AbstractTransactionalJUnit4SpringContextTests的类
* ,该基类是Spring2
* .5为方便在JUnit4环境进行事务测试的类,它还提供了一个simpleJdbcTemplate属性让你可以方便地操控数据库表
* ,便于对测试数据进行有效的操作;
* (2)用@ContextLocation注解指定你要加载的Spring配置信息所在的位置;(默认的加载文件信息请参阅Spring
* Documentation); (3)用@Autowired或@Resource注解注入你的Service接口,@Autowrired是指按类型将Spring
* Bean注入;而@Resource则按名称将Spring Bean注入。
* (4)用@Before准备待测试的数据,如果我们的数据库表结构没有任何数据
* ,则可以在这里预先插入记录,以便进行单元测试,当整个测试完成后,这些数据都不会被保留在数据库中。
* (5)在需要进行测试的方法上使用JUnit4.4提供的@Test注解进行标示;
*
*
* Test类可以选择继承 extends AbstractTransactionalJUnit4SpringContextTests
*
*
* Copyright (c) 2008-2009 paraDise sTudio(DT). All Rights Reserved.
* @version 1.0 Apr 13, 2009 5:08:34 PM
* mousepotato(paradise.lsj#gmail.com)created
*/
@RunWith(SpringJUnit4ClassRunner.class)
// applicationContext*.xml这种匹配写法,是不支持的,有多少个配置文件,就引入多少个。
@ContextConfiguration(locations = {"classpath:conf/spring/spring_base.xml"})
// @TestExecutionListeners({DependencyInjectionTestExecutionListener.class})
public class LoginTest {
private static Logger log = Logger.getLogger(ILoginService.class);
public LoginTest() {
super();
}
private ILoginService loginService;
@Autowired(required = false)
@Qualifier("loginService")//由于可以有n多类去实现接口,这个地方需要注明实现接口的类。
public void setLoginService(ILoginService loginService) {
this.loginService = loginService;
}
}
@Transactional
@Service("loginService")
public class LoginServiceImpl implements ILoginService {
}