junit4 单元测试

it2022-05-05  138

1.所需jar包

下载地址:http://search.maven.org/

搜索junit-4.12-beta-1

  aspectjweaver-1.8.2

  spring-test-4.0.6.RELEASE

2.3中测试形式

(1)最古老的

package sy.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import sy.model.User; import sy.service.IUserService; //@RunWith(SpringJUnit4ClassRunner.class) //@ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml"}) public class TestMybatis { //使用test注解 @Test public void test1(){ //读取配置文件,如有多个,用逗号隔开 ApplicationContext ctf = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"}); //获取userService实例 IUserService userService = (IUserService) ctf.getBean("userService"); User user = userService.getUserById("0"); System.out.println(user); } }

  (2).如果有多个test,则spring每次都要去加载读取上下文,很浪费资源,因此

package sy.test; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import sy.model.User; import sy.service.IUserService; //@RunWith(SpringJUnit4ClassRunner.class) //@ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml"}) public class TestMybatis { private IUserService userService; private ApplicationContext ctf; @Before public void before(){ ctf = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"}); userService = (IUserService) ctf.getBean("userService"); } //使用test注解 @Test public void test1(){ User user = userService.getUserById("0"); System.out.println(user); } //使用autowired注解 @Autowired public void setUserService(IUserService userService) { this.userService = userService; } }

@Before注解会在所有test测试之前执行

(3)spring与junit整合

package sy.test; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import sy.model.User; import sy.service.IUserService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:spring.xml","classpath:spring-mybatis.xml"}) public class TestMybatis { private IUserService userService; //使用test注解 @Test public void test1(){ User user = userService.getUserById("0"); System.out.println(user); } //使用autowired注解 @Autowired public void setUserService(IUserService userService) { this.userService = userService; } }

 

转载于:https://www.cnblogs.com/ruo-/p/3950176.html


最新回复(0)