ÒªÇó:JDK1.5ÒÔÉÏ(ÒòΪJunit4ÊÇÓÃ×¢½âÀ´ÊµÏÖµÄ)
ÐèÒªµÄ°ü£º
1.spring-2.5.jar
2.junit-4.4.jar
3.spring-test.jar
²âÊÔÀà
package user; import static org.junit.Assert.fail; import java.util.Date; import org.junit.After; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests; import org.springframework.test.context.transaction.TransactionConfiguration; import com.sample.model.user.User; import com.sample.service.user.IUserService; /** ÉèÖÃÒª¼ÓÔØµÄÅäÖÃÎļþ */ @ContextConfiguration( locations={ "classpath:spring/persistenceContext.xml", "classpath:spring/aopContext.xml", "classpath:spring/daoContext.xml", "classpath:spring/serviceContext.xml" } ) /** ÉèÖÃÊÇ·ñ»Ø¹öÊý¾Ý */ @TransactionConfiguration(defaultRollback = false) public class UserTest extends AbstractTransactionalJUnit4SpringContextTests{ /** ÉèÖÃ×Ô¶¯×¢ÈëµÄÊôÐÔ */ @Autowired private IUserService userService; @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test @Rollback(false) public void testSaveUser() { User user=new User(); user.setUsername("zhoujun"); user.setCreatetime(new Date()); userService.saveUser(user); } @Test public void testGetUserById() { User user=userService.getUserById("1"); System.out.println(user.getUsername()); System.out.println(user.getCreatetime()); } } |
ÓйØ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´¦Àí²âÊÔÖ´Ðг¬Ê±£¨Èçͨ¹ýÔÚÒ»¸öµ¥¶ÀµÄÏß³ÌÖÐÖ´ÐвâÊÔ·½·¨£©µÄ·½Ê½£¬ ÎÒÃDz»¿ÉÄÜÔÚÒ»¸öÊÂÎñÉÏÏÂÎÄÖеIJâÊÔ·½·¨ÉÏʹÓÃJUnitµÄ@Test(timeout=...)ÅäÖá£Òò´Ë£¬
Èç¹ûÄãÏ뽫һ¸ö²âÊÔ·½·¨ÅäÖóɼÆÊ±ÇÒ¾ßÊÂÎñÐԵģ¬ Äã¾Í±ØÐëÁªºÏʹÓÃSpringµÄ@Timed¼°@Transactional×¢½â¡£
»¹ÖµµÃ×¢ÒâµÄÊÇ@Test(timeout=...)Ö»¹Ü²âÊÔ·½·¨±¾ÉíÖ´ÐеĴÎÊý£¬Èç¹û³¬³öµÄ»°Á¢¿Ì¾Í»áʧ°Ü£»
È»¶ø£¬@Timed¹Ø×¢µÄÊDzâÊÔÖ´ÐеÄ×Üʱ¼ä£¨°üÀ¨½¨Á¢ºÍÏú»Ù²Ù×÷ÒÔ¼°Öظ´£©£¬²¢ÇÒ²»»áÁî²âÊÔʧ°Ü¡£
@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 |
¶¨ÒåÀ༶±ðµÄÔªÊý¾Ý£¬TestExecutionListeners»áʹÓÃ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 } |
|