Äú¿ÉÒÔ¾èÖú£¬Ö§³ÖÎÒÃǵĹ«ÒæÊÂÒµ¡£

1Ôª 10Ôª 50Ôª





ÈÏÖ¤Â룺  ÑéÖ¤Âë,¿´²»Çå³þ?Çëµã»÷Ë¢ÐÂÑéÖ¤Âë ±ØÌî



  ÇóÖª ÎÄÕ ÎÄ¿â Lib ÊÓÆµ iPerson ¿Î³Ì ÈÏÖ¤ ×Éѯ ¹¤¾ß ½²×ù Modeler   Code  
»áÔ±   
 
   
 
 
     
   
 ¶©ÔÄ
  ¾èÖú
Spring2.5+JUnit4µ¥Ôª²âÊÔ
 
×÷Õß william_yao£¬»ðÁú¹ûÈí¼þ    ·¢²¼ÓÚ 2014-07-21
  3351  次浏览      27
 

ÒªÇó: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
}
   
3351 ´Îä¯ÀÀ       27
Ïà¹ØÎÄÕÂ

΢·þÎñ²âÊÔÖ®µ¥Ôª²âÊÔ
һƪͼÎÄ´øÄãÁ˽â°×ºÐ²âÊÔÓÃÀýÉè¼Æ·½·¨
È«ÃæµÄÖÊÁ¿±£ÕÏÌåϵ֮»Ø¹é²âÊÔ²ßÂÔ
È˹¤ÖÇÄÜ×Ô¶¯»¯²âÊÔ̽Ë÷
Ïà¹ØÎĵµ

×Ô¶¯»¯½Ó¿Ú²âÊÔʵ¼ù֮·
jenkins³ÖÐø¼¯³É²âÊÔ
ÐÔÄܲâÊÔÕï¶Ï·ÖÎöÓëÓÅ»¯
ÐÔÄܲâÊÔʵÀý
Ïà¹Ø¿Î³Ì

³ÖÐø¼¯³É²âÊÔ×î¼Ñʵ¼ù
×Ô¶¯»¯²âÊÔÌåϵ½¨ÉèÓë×î¼Ñʵ¼ù
²âÊԼܹ¹µÄ¹¹½¨ÓëÓ¦ÓÃʵ¼ù
DevOpsʱ´úµÄ²âÊÔ¼¼ÊõÓë×î¼Ñʵ¼ù
×îл¼Æ»®
DeepSeekÔÚÈí¼þ²âÊÔÓ¦ÓÃʵ¼ù 4-12[ÔÚÏß]
DeepSeek´óÄ£ÐÍÓ¦Óÿª·¢Êµ¼ù 4-19[ÔÚÏß]
UAF¼Ü¹¹ÌåϵÓëʵ¼ù 4-11[±±¾©]
AIÖÇÄÜ»¯Èí¼þ²âÊÔ·½·¨Óëʵ¼ù 5-23[ÉϺ£]
»ùÓÚ UML ºÍEA½øÐзÖÎöÉè¼Æ 4-26[±±¾©]
ÒµÎñ¼Ü¹¹Éè¼ÆÓ뽨ģ 4-18[±±¾©]

LoadRunnerÐÔÄܲâÊÔ»ù´¡
Èí¼þ²âÊÔ½á¹û·ÖÎöºÍÖÊÁ¿±¨¸æ
ÃæÏò¶ÔÏóÈí¼þ²âÊÔ¼¼ÊõÑо¿
Éè¼Æ²âÊÔÓÃÀýµÄËÄÌõÔ­Ôò
¹¦ÄܲâÊÔÖйÊÕÏÄ£Ð͵Ľ¨Á¢
ÐÔÄܲâÊÔ×ÛÊö


ÐÔÄܲâÊÔ·½·¨Óë¼¼Êõ
²âÊÔ¹ý³ÌÓëÍŶӹÜÀí
LoadRunner½øÐÐÐÔÄܲâÊÔ
WEBÓ¦ÓõÄÈí¼þ²âÊÔ
ÊÖ»úÈí¼þ²âÊÔ
°×ºÐ²âÊÔ·½·¨Óë¼¼Êõ


ij²©²ÊÐÐÒµ Êý¾Ý¿â×Ô¶¯»¯²âÊÔ
IT·þÎñÉÌ Web°²È«²âÊÔ
IT·þÎñÉÌ ×Ô¶¯»¯²âÊÔ¿ò¼Ü
º£º½¹É·Ý µ¥Ôª²âÊÔ¡¢Öع¹
²âÊÔÐèÇó·ÖÎöÓë²âÊÔÓÃÀý·ÖÎö
»¥ÁªÍøweb²âÊÔ·½·¨Óëʵ¼ù
»ùÓÚSeleniumµÄWeb×Ô¶¯»¯²âÊÔ