每次使用http调用的junit测试用例后进行数据库清理

编程入门 行业动态 更新时间:2024-10-17 02:46:24
本文介绍了每次使用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:

  • 在Junit测试用例中。

  • 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测试用例后进行数据库清理

    本文发布于:2023-11-06 23:56:36,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1565006.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数据库   测试   http   junit   用例后

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!