每次使用http调用的junit测试用例后进行数据库清理 [英] Database Cleanup after every junit test cases with http calls

查看:661
本文介绍了每次使用http调用的junit测试用例后进行数据库清理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有spring的junit4来测试我的其余Web服务。
为此,我在内存数据库中使用HSQL。

I am using junit4 with spring to test my rest web services. For this, I am using HSQL in memory database.

要在每个测试用例之后清理记录,我将从表中删除所有记录。

To clean the records after every test case, I am removing all the records from tables.

但我想只删除插入的记录。
我在两个地方向数据库添加数据:

But I want to delete only inserted records. I am adding data to database in two places:


  1. 在Junit测试用例中。

  1. In Junit test cases.

在其他服务中。

我正在制作 http调用来测试服务。此外,我在休息服务中使用相同的内存数据库。

I am making http calls to test the services. Also, I am using same in-memory database in rest services.

请帮助我在每个测试用例后仅删除插入的记录。

Kindly help me in removing only inserted records after each test cases.

已修改:我担心的是删除对http服务的http调用中插入的记录。跟踪这些记录真的很难。它们是我实际代码的一部分。

Edited: My concern is deleting the inserted records in http calls to rest services. It is really hard to keep track of those records. They are part of my actual code.

推荐答案

你可以使用 @Before @After 执行此活动的方法。

you could use @Before and @After methods to perform this activity.

请注意 @Before ,每次测试后都会执行 @After .So @Before 你应该插入记录,现在你知道插入了哪些记录,只删除它们 @After

Note that @Before will be executed before every test and @After will be executed after every Test .So @Before you should insert the records , now you know which records have been inserted , delete only them in @After

如果你想为每个测试添加几个不同的记录然后使用try .... finally

If you want to add few different records for each test then use try .... finally

如下所示

class Test{
     @Before
        public void setUp(){
             // insert x Records 
        }


      @After
        public void tearDown(){
             // delete x Records 
        }


         @Test
                public void someTest() throws Exception {
              //  ... insert few records 
                try{
                doSomething();
              }finally{
            //  deleteRecordsInserted for this test.
             }

            }

}

这篇关于每次使用http调用的junit测试用例后进行数据库清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆