1.所需jar包
下載地址:http://search.maven.org/
搜索junit-4.12-beta-1
aspectjweaver-1.8.2
sPRing-test-4.0.6.RELEASE
2.3中測(cè)試形式
(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(){ //讀取配置文件,如有多個(gè),用逗號(hào)隔開 ApplicationContext ctf = new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"}); //獲取userService實(shí)例 IUserService userService = (IUserService) ctf.getBean("userService"); User user = userService.getUserById("0"); System.out.println(user); } }
(2).如果有多個(gè)test,則spring每次都要去加載讀取上下文,很浪費(fèi)資源,因此
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注解會(huì)在所有test測(cè)試之前執(zhí)行
(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; } }
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注