更新時(shí)間:2021-09-13 11:19:28 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1147次
1.加入 junit jar包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2.標(biāo)注@Test注解、調(diào)用測(cè)試框架方法、調(diào)用 httpclient 方法
Assert.assertNotNull(user);
Assert.assertNull(user.getId());
Assert.assertTrue(userList.size() > 0);
Assert.assertEquals("admin", user.getUserName());
1.加入 junit 及 spring-test jar包
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.14.RELEASE</version>
</dependency>
2.在測(cè)試類上添加注解,并指定spring配置文件的classpath
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/spring-context.xml") //@ContextConfiguration(classes=xxx.class)
public class TestJedis {
}
3.標(biāo)注@Test注解、調(diào)用測(cè)試框架方法、調(diào)用 httpclient 方法(同上)
@ContextConfiguration(locations = "classpath:conf/applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public abstract class AbstractTestCase extends
AbstractTransactionalDataSourceSpringContextTests {
}
將配置項(xiàng)都寫(xiě)在父類中,測(cè)試子類繼承該AbstractTestCase類,測(cè)試的時(shí)候,不會(huì)導(dǎo)致數(shù)據(jù)庫(kù)數(shù)據(jù)污染,因?yàn)槔^承AbstractTransactionalDataSourceSpringContextTests每次數(shù)據(jù)庫(kù)操作完成之后都會(huì)回滾。
以上就是動(dòng)力節(jié)點(diǎn)小編介紹的"Spring測(cè)試框架的使用",希望對(duì)大家有幫助,想了解更多可查看Spring框架教程。動(dòng)力節(jié)點(diǎn)Java在線學(xué)習(xí)教程,針對(duì)沒(méi)有任何Java基礎(chǔ)的讀者學(xué)習(xí),讓你從入門(mén)到精通,主要介紹了一些Java基礎(chǔ)的核心知識(shí),讓同學(xué)們更好更方便的學(xué)習(xí)和了解Java編程,感興趣的同學(xué)可以關(guān)注一下。
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)