JUnit 单元测试启动 Spring 容器
http://blog.csdn.net/qq_18416057/article/details/53409722介绍在spring的框架下,做单元测试的两种办法。 一、使用spring中对Junit框架的整合功能 除了junit4和spring的jar包,还需要spring-test.jar。引入如下依赖: [html] view plain copy print?<depen
·
http://blog.csdn.net/qq_18416057/article/details/53409722
介绍在
spring
的框架下,做单元
测试
的两种办法。
一、使用spring中对Junit框架的整合功能
除了junit4和spring的jar包,还需要spring-test.jar。引入如下依赖:
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.4</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>2.5</version>
- </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>2.5</version>
</dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>3.1.1.RELEASE</version>
- </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
@ContextConfiguration需要配上spring的配置文件,这样就可以在测试类中使用注解简单的注入需要的bean了。简单高效。
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations ={“/applicationContext.xml”})
- public class ProducerConsumerTest {
- @Autowired
- private Sender sender;
- @Autowired
- @Qualifier(“queueDestination”)
- private Destination destination;
- @Test
- public void testSend() {
- for (int i=0; i<2; i++) {
- sender.sendInfo(destination, ”你好,生产者!这是消息:” + (i+1));
- }
- }
- }
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"/applicationContext.xml"})
public class ProducerConsumerTest {
@Autowired
private Sender sender;
@Autowired
@Qualifier("queueDestination")
private Destination destination;
@Test
public void testSend() {
for (int i=0; i<2; i++) {
sender.sendInfo(destination, "你好,生产者!这是消息:" + (i+1));
}
}
}
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration({“classpath:applicationContext.xml”})
- public class ReadDaoImplTest {
- @Resource ReadDao readDao;
- @Test
- public void getListTest(){
- List<Client> clientList = readDao.getList(”client.test”, null);
- for(Client c:clientList){
- System.out.println(c.getVersionNum());
- }
- }
- }
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml"})
public class ReadDaoImplTest {
@Resource ReadDao readDao;
@Test
public void getListTest(){
List<Client> clientList = readDao.getList("client.test", null);
for(Client c:clientList){
System.out.println(c.getVersionNum());
}
}
}
二、手动加载spring的配置文件,并启动spring容器
- public class ReadDaoImplTest {
- public static void main(String[] args){
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
- context.start();
- ReadDao fqaService = (ReadDao) context.getBean(”readDao”);
- System.out.println(fqaService);
- }
- }
public class ReadDaoImplTest {
public static void main(String[] args){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.start();
ReadDao fqaService = (ReadDao) context.getBean("readDao");
System.out.println(fqaService);
}
}
- public class ReadDaoImplTest {
- public static void main(String[] args){
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
- context.start();
- ReadDao fqaService = (ReadDao) context.getBean(”readDao”);
- System.out.println(fqaService);
- }
- }
public class ReadDaoImplTest {
public static void main(String[] args){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.start();
ReadDao fqaService = (ReadDao) context.getBean("readDao");
System.out.println(fqaService);
}
}
运行这两种测试方法,EclipseIDE下都只需要Ctrl+F11。
更多推荐
已为社区贡献6条内容
所有评论(0)