@RunWith(SpringJUnit4ClassRunner.class) 用於配置spring中測試的環境
@Test標註在方法前,表示其是一個測試的方法 無需在其配置文件中額外設置屬性.
@ContextConfiguration 用來指定加載的Spring配置文件的位置,會加載默認配置文件
例如下例會加載:classpath:/com/example/MyTest-context.xml文件
package com.example; @ContextConfiguration public class MyTest { // class body... }
@ContextConfiguration 註解有以下兩個常用的屬性:
- locations:可以通過該屬性手工指定 Spring 配置文件所在的位置,可以指定一個或多個 Spring 配置文件。如下所示:
@ContextConfiguration(locations={“xx/yy/beans1.xml”,” xx/yy/beans2.xml”}) - inheritLocations:是否要繼承父測試用例類中的 Spring 配置文件,默認爲 true。如下面的例子:
@ContextConfiguration(locations={"base-context.xml"}) public class BaseTest { // ... } @ContextConfiguration(locations={"extended-context.xml"}) public class ExtendedTest extends BaseTest { // ... }
如果 inheritLocations 設置爲 false,則 ExtendedTest 僅會使用 extended-context.xml 配置文件,否則將使用 base-context.xml 和 extended-context.xml 這兩個配置文件。
在使用所有註釋前必須使用@RunWith(SpringJUnit4ClassRunner.class),讓測試運行於Spring測試環境
Spring框架在org.springframework.test.annotation
包中提供了常用的Spring特定的註解集,如果你在Java5或以上版本開發,可以在測試中使用它。
@IfProfileValue
提示一下,註解測試只針對特定的測試環境。 如果配置的ProfileValueSource類返回對應的提供者的名稱值, 這個測試就可以啓動。這個註解可以應用到一個類或者單獨的方法。
@IfProfileValue(name=”java.vendor”, value=”Sun Microsystems Inc.”)
public void testProcessWhichRunsOnlyOnSunJvm() {
// some logic that should run only on Java VMs from Sun Microsystems
}
同時@IfProfileValue可配置一個值列表 (使用OR 語義) 來在JUnit環境中獲得TestNG的測試組支持。 看下面的例子:
@IfProfileValue(name=”test-groups”, values={”unit-tests”, “integration-tests”})
public void testProcessWhichRunsForUnitOrIntegrationTestGroups() {
// some logic that should run only for unit and integration test groups
}
@ProfileValueSourceConfiguration
類級別註解用來指定當通過@IfProfileValue註解獲取已配置的profile值時使用何種ProfileValueSource。 如果@ProfileValueSourceConfiguration沒有在測試中聲明,將默認使用 SystemProfileValueSource。
@ProfileValueSourceConfiguration(CustomProfileValueSource.class)
public class CustomProfileValueSourceTests {
// class body…
}
@DirtiesContext
在測試方法上出現這個註解時,表明底層Spring容器在該方法的執行中被“污染”,從而必須在方法執行結束後重新創建(無論該測試是否通過)。
@DirtiesContext
public void testProcessWhichDirtiesAppCtx() {
// some logic that results in the Spring container being dirtied
}
@ExpectedException
表明被註解方法預期在執行中拋出一個異常。預期異常的類型在註解中給定。如果該異常的實例在測試方法執行中被拋出, 則測試通過。同樣的如果該異常實例沒有在測試方法執行時拋出,則測試失敗。
@ExpectedException(SomeBusinessException.class)
public void testProcessRainyDayScenario() {
// some logic that should result in an Exception being thrown
}
@Timed
表明被註解的測試方法必須在規定的時間區間內執行完成(以毫秒記)。如果測試執行時間超過了規定的時間區間,測試就失敗了。
注意該時間區間包括測試方法本身的執行,任何重複測試(參見 @Repeat),還有任何測試fixture的set up或tear down時間。
Spring的@Timed
註解與JUnit 4的@Test(timeout=...)
支持具有不同的語義。 特別地,鑑於JUnit 4處理測試執行超時(如通過在一個單獨的線程
中執行測試方法)的方式, 我們不可能在一個事務上下文中的測試方法上使用JUnit的@Test(timeout=...)
配置。因此,
如果你想將一個測試方法配置成計時且具事務性的, 你就必須聯合使用Spring的@Timed
及@Transactional
註解。 還值得注意的是@Test(timeout=...)
只管測試方法本身執行的次數,如果超出的話立刻就會失敗;
然而,@Timed
關注的是測試執行的總時間(包括建立和銷燬操作以及重複),並且不會令測試失敗。
@Timed(millis=1000)
public void testProcessWithOneSecondTimeout() {
// some logic that should not take longer than 1 second to execute
}
@Repeat
表明被註解的測試方法必須重複執行。執行的次數在註解中聲明。
注意重複執行範圍包括包括測試方法本身的執行,以及任何測試fixture的set up或tear down。
@Repeat(10)
public void testProcessRepeatedly() {
// …
}
@Rollback
表明被註解方法的事務在完成後是否需要被回滾。 如果true,事務將被回滾,否則事務將被提交。 使用@Rollback接口來在類級別覆寫配置的默認回滾標誌。
@Rollback(false)
public void testProcessWithoutRollback() {
// …
}
@NotTransactional
出現該註解表明測試方法必須不在事務中執行。
@NotTransactional
public void testProcessWithoutTransaction() {
// …
}
Spring TestContext Framework還支持下面這些非特定於測試的註解,並且保持其語義不變。
@Autowired
@Qualifier
@Resource
(javax.annotation)如果JSR-250可用@PersistenceContext
(javax.persistence)如果JPA可用@PersistenceUnit
(javax.persistence)如果JPA可用@Required
@Transactional
@TestExecutionListeners
定義類級別的元數據,TestExecutionListener
s會使用TestContextManager
進行註冊。 通常,@TestExecutionListeners
與@ContextConfiguration
會搭配使用。
@ContextConfiguration @TestExecutionListeners({CustomTestExecutionListener.class, AnotherTestExecutionListener.class}) public class CustomTestExecutionListenerTests { // class body... }
@TransactionConfiguration
爲配置事務性測試定義了類級別的元數據。特別地,如果需要的PlatformTransactionManager不是“transactionManager”的話, 那麼可以顯式配置驅動事務的PlatformTransactionManager
的bean名字。此外, 可以將defaultRollback
標誌改爲false
。通常,
@TransactionConfiguration
與@ContextConfiguration
搭配使用。
@ContextConfiguration @TransactionConfiguration(transactionManager="txMgr", defaultRollback=false) public class CustomConfiguredTransactionalTests { // class body... }
@BeforeTransaction
表明被註解的public void
方法應該在測試方法的事務開始之前執行, 該事務是通過@Transactional
註解來配置的。
@BeforeTransaction
public void beforeTransaction() {
// logic to be executed before a transaction is started
}
@AfterTransaction
表明被註解的public void
方法應該在測試方法的事務結束之後執行, 該事務是通過@Transactional
註解來配置的。
@AfterTransaction
public void afterTransaction() {
// logic to be executed after a transaction has ended
}
AbstractTransactionalJUnit38SpringContextTests、 AbstractTransactionalJUnit4SpringContextTests
AbstractTransactionalTestNGSpringContextTests
已經在類級別預先配置了好了事物支持
在普通spring的junit環境中配置事務
在類之前加入註解
@TransactionConfiguration(transactionManagert="txMgr",defaultRollback=false)
@Transactional
在方法中主要使用的Annotation包括
@TestExecutionListeners({})---用於禁用默認的監聽器 否着需要通過@contextconfiguration配置一個ApplicationContext;
@BeforeTransaction
@Before
@Rollback(true)
@AfterTransaction
@NotTransactional
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations="../applicationContext.xml")
- public class SimpleDaoTest {
- @Autowired
- private SimpleDaoInterface<Object> simpledao;
- @Test
- public void testaddObject()
- {
- System.out.println("執行測試");
- Usertable us = new Usertable("tom","123","orz");
- Object obj = simpledao.addObject(us);
- Assert.assertSame(us, obj);
- System.out.println("運行完成");
- }
- }