?????й?Junit4?????????????:
????@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