Spring2.5+JUnit4单元测试

来源:互联网 发布:sql select as 别名 编辑:程序博客网 时间:2024/05/17 01:45

要求:

      JDK1.5以上(因为Junit4是用注解来实现的)

     

需要的包

      spring-2.5.jar

      junit-4.4.jar

      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;@ContextConfigurationpublic 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

定义类级别的元数据,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注解来配置的。

@BeforeTransactionpublic void beforeTransaction() {    // logic to be executed before a transaction is started}

@AfterTransaction

表明被注解的public void方法应该在测试方法的事务结束之后执行, 该事务是通过@Transactional注解来配置的。

@AfterTransactionpublic void afterTransaction() {    // logic to be executed after a transaction has ended}

    junit-4.4.jar (157.7 KB)

    spring.jar (2.8 MB)

    spring-test.jar (176.3 KB)

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 饭盒盖吸不住怎么办 饭盒微波炉加热后打不开怎么办 微波炉加热饭盒打不开怎么办 塑料饭盒加热后打不开怎么办 真空锅锅盖打不开怎么办 玻璃真空水壶打不开怎么办 保温饭盒盖子打不开怎么办 饭盒盖章松了怎么办 电压力锅卡住了怎么办 铁的饭盒打不开怎么办 微波炉饭盒盖子打不开怎么办 微波炉盖子吸住了怎么办 剩下的糯米饭怎么办 饭盒微波加热打不开怎么办 微波炉触屏不灵怎么办 微波炉旋钮坏了怎么办 微波炉蒸馒头硬怎么办 小猫两天不吃饭怎么办 减肥吃了巧乐兹怎么办 孕妇吃了点蟹籽怎么办 做寿司没有肉松怎么办 做寿司没有海苔怎么办 孩子不爱吃早餐怎么办 早上不想吃早餐怎么办 早上来不及吃早餐怎么办 早餐吃撑了怎么办 减肥吃撑了怎么办 哺乳吃爆米花了怎么办 干吃方便面后怎么办 吃了牛奶上火怎么办 绿豆糕吃多了怎么办 吃完火锅肚子烧怎么办 吃东西经常烫到怎么办 黄焖鸡酱料过期了怎么办 自热饭水弄错了怎么办 电饭锅米饭硬了怎么办 米饭太软了怎么办 米饭变硬粘锅怎么办 晚上不吃饭便秘怎么办 饭店米饭剩了怎么办 隔夜饭硬了怎么办