Spring整合Junit测试,并且配置事务

释放双眼,带上耳机,听听看~!

问题

很多时候我们不希望单元测试的结果玷污了数据库,玷污了我们也不希望人肉去check,我们希望它无痕迹的悄悄执行,告诉我最终结果即可。那么怎样让你的UT也具有事务功能呢?

预期效果

我们希望能够达到这样的效果:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1package me.arganzheng.study;
2import static org.junit.Assert.*;
3import org.junit.Test;
4import org.springframework.beans.factory.annotation.Autowired;
5import org.springframework.test.annotation.Rollback;
6/**
7 *  
8 * @author arganzheng
9 */
10public class FooServiceTest{
11    @Autowired
12    private FooService fooService;
13    @Test
14    @Rollback(true)
15    public void testSaveFoo() {
16        Foo foo = new Foo();
17        // ...
18        long id = fooService.saveFoo(foo);
19        assertTrue(id > 0);
20    }
21}
22

虽然我们插入了一条数据,但是在UT后,已经雁过无痕了。

解决方案

在前面一篇文章JUnit与Spring的整合——JUnit的TestCase如何自动注入Spring容器托管的对象中我们详细的介绍了Spring与Junit的整合,提到了Spring3提供了SpringJUnit4ClassRunner基类让我们可以很方便的接入JUnit4。


1
2
3
4
1public class org.springframework.test.context.junit4.SpringJUnit4ClassRunner extends org.junit.runners.BlockJUnit4ClassRunner {
2    ...
3}
4

它是基于事件监听机制(the listener-based test context framework),并且允许用户自定义事件监听器。默认是org
.
springframework
.
test
.
context
.
support
.
DependencyInjectionTestExecutionListener、org
.
springframework
.
test
.
context
.
support
.
DirtiesContextTestExecutionListener和org
.
springframework
.
test
.
context
.
transaction
.
TransactionalTestExecutionListener这三个事件监听器。

其中org
.
springframework
.
test
.
context
.
support
.
DependencyInjectionTestExecutionListener是实现自动注入Spring托管的bean的。而org
.
springframework
.
test
.
context
.
transaction
.
TransactionalTestExecutionListener则是实现事务管理的事件处理器:

org.springframework.test.context.transaction.TransactionalTestExecutionListener

TestExecutionListener which provides support for executing tests within transactions by using @Transactional and @NotTransactional annotations.

Changes to the database during a test run with @Transactional will be run within a transaction that will, by default, be automatically rolled back after completion of the test; whereas, changes to the database during a test run with @NotTransactional will not be run within a transaction. Similarly, test methods that are not annotated with either @Transactional (at the class or method level) or @NotTransactional will not be run within a transaction.

Transactional commit and rollback behavior can be configured via the class-level @TransactionConfiguration and method-level @Rollback annotations. @TransactionConfiguration also provides configuration of the bean name of the PlatformTransactionManager that is to be used to drive transactions.

When executing transactional tests, it is sometimes useful to be able execute certain set up or tear down code outside of a transaction. TransactionalTestExecutionListener provides such support for methods annotated with @BeforeTransaction and @AfterTransaction.

其实我发现Spring的javadoc非常的简单明了又全面,根本不需要到处查找其他的东东,直接看javadoc和源码就可以了。这里简单概括一下:

  • TransactionalTestExecutionListener

  • @Transactional (class-level & method-level)

    • @NotTransactional (method-level)
    • @TransactionConfiguration (class-level)
    • @Rollback (method-level)
    • @BeforeTransaction (method-level)
    • @AfterTransaction (method-level)

注:其中 class-level即Target
(
ElementType
.
TYPE
),method-level即Target
(
ElementType
.
METHOD
)。

这样我们可以如下配置:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1ackage me.arganzheng.study;
2import org.junit.runner.RunWith;
3import org.springframework.test.context.ContextConfiguration;
4import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
5import org.springframework.transaction.annotation.Transactional;  
6/**
7 *  
8 * @author arganzheng
9 */
10@RunWith(SpringJUnit4ClassRunner.class)
11@ContextConfiguration({
12    "classpath:conf-spring/spring-dao.xml",
13    "classpath:conf-spring/spring-service.xml",
14    "classpath:conf-spring/spring-controller.xml"
15})
16@TransactionConfiguration(transactionManager="transactionManager") //可选,默认就是这个
17@Transactionnal
18public class BaseSpringTestCase{
19}
20

然后我们的FooServiceTest这样写就可以拥有事务处理能力了:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1package me.arganzheng.study;
2import static org.junit.Assert.*;
3import org.junit.Test;
4import org.springframework.beans.factory.annotation.Autowired;
5import org.springframework.test.annotation.Rollback;
6/**
7 *  
8 * @author arganzheng
9 */
10public class FooServiceTest extends BaseSpringTestCase{
11    @Autowired
12    private FooService fooService;
13    @Test
14    // @Rollback(true) 默认就是true
15    public void testSaveFoo() {
16        Foo foo = new Foo();
17        // ...
18        long id = fooService.saveFoo(foo);
19        assertTrue(id > 0);
20    }
21}
22

给TA打赏
共{{data.count}}人
人已打赏
安全技术

详解Node.js API系列 Crypto加密模块(2) Hmac

2021-12-21 16:36:11

安全技术

从零搭建自己的SpringBoot后台框架(二十三)

2022-1-12 12:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索