web单元测试经常会在执行前准备一些数据
执行完毕后删除这些数据 如下伪代码
public void testFindUser(){
jdbcTemplate.insert('insert into user...');
jdbcTemplate.insert('insert into user...');
User u = userManager.findUser("....");
Assert();
jdbcTemplate.delete();
}
这很烦!
采用jdbcTemplate或一些其他的方法也很烦
毕竟要写一堆无关紧要的代码
先看我们改进后的效果
BeforeMethod 是在执行测试方法前准备数据
AfterMethod 来清理数据
其value值是一些 sql语句
public class MessageManagerTest extends JUnit4TestCase {
@Autowired
MessageManager messageManager;
@Test
@BeforeMethod({
"insert into sns_user(id, email,password, nickname, enabled) values ('10','100@justel.com.cn','password','haha', 1);",
"insert into sns_user(id, email,password, nickname, enabled) values ('11','101@justel.com.cn','password','haha2', 1);"
})
@AfterMethod({"delete from sns_message", "delete from sns_user"})
public void send(){
Message msg = new Message();
try {
messageManager.send(msg);
} catch (UserNotExistException e) {
Assert.fail();
}
int count = simpleJdbcTemplate.queryForInt("select count(*) from sns_message where id = ?", msg.getId());
Assert.assertEquals(1, count);
}
}
会写sql就ok了 不用其他知识
再来看看具体实现
很简单扩展一下spring 提供的TestExecutionListener接口就行了
public interface TestExecutionListener {
void prepareTestInstance(TestContext testContext) throws Exception;
//测试前执行
void beforeTestMethod(TestContext testContext) throws Exception;
//测试后执行
void afterTestMethod(TestContext testContext) throws Exception;
}
扩展后的类
public class PreparedDataTestExecutionListener extends AbstractTestExecutionListener {
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
BeforeMethod beforeMethod = testContext.getTestMethod().getAnnotation(BeforeMethod.class);
if(beforeMethod != null){
executeBeforeMethod(testContext, beforeMethod);
}
}
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
AfterMethod afterMethod = testContext.getTestMethod().getAnnotation(AfterMethod.class);
if(afterMethod != null){
executeAfterMethod(testContext, afterMethod);
}
}
/**
* 准备数据
*/
protected void executeBeforeMethod(TestContext testContext, BeforeMethod beforeMethod){
DataSource dataSource = (DataSource)testContext.getApplicationContext().getBean("dataSource");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String[] sqls = beforeMethod.value();
for (String sql : sqls) {
jdbcTemplate.execute(sql);
}
}
/**
* 清理数据
*/
protected void executeAfterMethod(TestContext testContext, AfterMethod afterMethod){
DataSource dataSource = (DataSource)testContext.getApplicationContext().getBean("dataSource");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String[] sqls = afterMethod.value();
for (String sql : sqls) {
jdbcTemplate.execute(sql);
}
}
}
实际上是将jdbcTemplate转移了个位置而已
这个PreparedDataTestExecutionListener怎么用呢
接着看代码
@ContextConfiguration(locations = { "classpath:/application-test.xml" })
@TestExecutionListeners({PreparedDataTestExecutionListener.class})
public class JUnit4TestCase extends AbstractTransactionalJUnit4SpringContextTests{
}
这样你的testcase只要继承JUnit4TestCase就可以使用BeforeMethod和AfterMethod了
附上BeforeMethod和AfterMethod
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface BeforeMethod {
String[] value() default {};
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface AfterMethod {
String[] value() default {};
}
分享到:
相关推荐
这里的`SpringJUnit4ClassRunner`是JUnit的扩展,它会启动Spring容器并初始化测试类中的bean。 接着,我们可以使用`@Autowired`注解来自动注入需要的依赖。假设我们有一个`MyService`服务需要在测试中使用: ```...
单元测试—— Spring 环境下测试,所需要的jar包: spring-test-4.3.29.RELEASE.jar、junit-4.13.1.jar、hamcrest-core-1.3.jar。
描述中的链接指向了一篇名为“Spring 2.5+Junit4的单元测试”的博客文章,虽然具体内容未给出,但可以推断这篇文章可能讲述了如何在Spring 2.5版本中集成Junit4进行单元测试的实践和技巧。 标签“源码”意味着我们...
在实际使用中,我们通常会创建一个测试类,继承自`SpringJUnit4ClassRunner`或`AbstractJUnit4SpringContextTests`,这样SpringJUnit就会自动启动Spring容器,并在每个测试方法前初始化所需的bean。此外,还可以使用...
这包括JUnit4本身,Spring的`spring-test`模块以及Spring Boot的`spring-boot-starter-test`模块。这三个依赖分别提供了JUnit4的基本功能,Spring对测试的支持以及Spring Boot测试相关的工具和配置。以下是添加依赖...
spring添加 单元测试 junit4 +spring jar包: hamcrest-core-1.3.jar junit-4.12.jar spring-test-4.2.5.RELEASE.jar
基于Spring的JUnit4单元测试
在Spring 2.x中,尽管JUnit4已经发布,但很多项目仍可能依赖于JUnit3,因为它稳定且满足需求。这个压缩包文件包含了一系列的项目文件,这些文件对于理解一个基于Spring的Java项目结构至关重要: 1. `.project`:这...
在"spring4+junit4.8 +多线程TheadTool"的场景下,我们可以深入探讨以下几个知识点: 1. **Spring4框架**:Spring4在Spring3的基础上进行了许多改进,包括对Java 8的支持,提升了与NoSQL数据库的集成,以及对...
10. **扩展性与兼容性**:JUnit4设计得非常开放,可以通过实现`Runner`接口来扩展其功能,例如SpringJUnit4ClassRunner可以结合Spring框架进行测试。同时,JUnit4与大多数IDE和构建工具(如Maven、Gradle)良好集成...
Spring框架结合JUnit4和Ant构建的测试环境,能够有效地帮助开发者进行高效、可靠的测试。本篇将深入探讨如何利用这些工具进行测试,以及它们各自的关键功能。 首先,让我们了解Spring框架。Spring是Java企业级应用...
Java Spring Junit System-Rules 1.16.1 是一个专门为Java开发人员设计的测试框架扩展,它在Spring和JUnit的基础上提供了一组强大的规则,用于处理系统级别的测试。System-Rules库允许开发者在单元测试中更有效地...
Spring Junit单元测试加载配置文件失败问题解决方法 在软件开发过程中,单元测试是一个非常重要的步骤,它可以帮助开发者检测代码中的错误、提高代码的可靠性和稳定性。Spring Junit是一个常用的单元测试框架,它...
除了基本的测试功能,JUnit4还包含了一些扩展框架,如TestNG和Mockito,它们能帮助我们进行更复杂的测试场景。TestNG提供了更丰富的测试注解和并行测试支持,而Mockito则是一个强大的模拟框架,可以帮助我们创建和...
在SSM环境中,使用Junit4和spring-test库进行单元测试是标准做法。下面将详细解释如何使用这两个库以及所需的jar包。 Junit4是Java领域广泛使用的单元测试框架,它提供了一套丰富的注解,使得编写测试用例变得更加...
这是一个基于Spring3、MyBatis3和JUnit4的可运行项目示例,旨在提供一个完整的、可测试的Java Web应用程序框架。这个项目的核心是利用Spring作为应用的ioc(Inversion of Control,控制反转)和aop(Aspect Oriented...
与JUnit3相比,JUnit4的灵活性和可扩展性得到了显著提升,使得测试驱动开发(TDD)在Java领域变得更加普及。 ## 二、JUnit4的核心组件 ### 1. 测试注解 - `@Test`: 表示一个方法是测试方法,可以包含断言。 - `@...
JUnit4是JUnit的主要版本,它引入了诸如`@Test`、`@Before`、`@After`等注解,使得测试逻辑更加清晰。例如,你可以这样编写一个简单的测试用例: ```java import org.junit.Test; import static org.junit.Assert.*...
为了方便使用连接数据库,项目进行测试,方便使用spring配置定时任务,excel 读写操作等(项目是完整配置的东东),相关包由于文件太大无法上传,放入共享文件夹,有需要可以查看 http://yunpan.cn/c3bcVRVIpSgk4 ...
1. **配置测试环境**:引入Spring Test和JUnit相关的依赖,创建一个继承自`AbstractJUnit4SpringContextTests`或`SpringRunner`的测试类。在测试类上使用`@RunWith(SpringRunner.class)`注解启用Spring测试支持,并...