Java 8.Spring--整合Junit测试
Posted yuzhenfu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 8.Spring--整合Junit测试相关的知识,希望对你有一定的参考价值。
1.导包
导入spring-test包
2.在Person中添加注解
import com.Spring.pojo.Person; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml")
3.创建测试类
package com.spring.pojo; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.Spring.pojo.Person; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class RunWithTest { @Resource(name="person") private Person p; @Test @SuppressWarnings("resource") public void testProperty() { AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-injection.xml"); Person person1=(Person) context.getBean("person1"); System.out.println(person1); } }
以上是关于Java 8.Spring--整合Junit测试的主要内容,如果未能解决你的问题,请参考以下文章
Java之Spring Boot入门到精通IDEA版SpringBoot整合其他框架Junit,Redis,MyBatis(一篇文章精通系列)中
Spring:概述,IOC(Bean管理),整合Web项目,整合JUnit单元测试
报错记录解决Junit测试报错Failed to retrieve PlatformTransactionManager for @Transactional test: [DefaultTest(代