JUnit用于while循环的方法(JUnit for a method with while loop)

编程入门 行业动态 更新时间:2024-10-26 14:40:03
JUnit用于while循环的方法(JUnit for a method with while loop)

我在为带有While循环的方法编写JUnit测试时遇到了一些困难。 我的方法如下所示:

private void deleteMethod(DeleteRequest dr){ // below statement am calling some service which returns me a object after querying it from Database. SomeObject ob = db.getdata(dr); while(ob != null) { // this method deletes the Data from DB db.deleteData(ob); // again calling the same service operation as we did before while loop. I have a situation where my service only returns single record at a time. It is avoidable that I need to do a dirty job. ob = db.getdata(dr); }

下面是我的JUnit:

@Test public void testDeleteMethod() throws Exception{ DeleteRequest mockDR = new DeleteRequest(); mockDR.setX(y); SomeObject mockOB = new SomeObject(); mockOB.setZ(k); // making a mockcall to the method before I can assert anything mockClassObject.deleteMethod(mockDR); }

上面的JUnit方法永远都会被执行,我明白它不会脱离while循环。 我如何处理这个问题。 只是提到使用Mockito框架,我不知道是否有任何方法可以处理Mockito中的这种情况。

I am facing some difficulty in writing a JUnit test for a method with a While loop. My method looks like below:

private void deleteMethod(DeleteRequest dr){ // below statement am calling some service which returns me a object after querying it from Database. SomeObject ob = db.getdata(dr); while(ob != null) { // this method deletes the Data from DB db.deleteData(ob); // again calling the same service operation as we did before while loop. I have a situation where my service only returns single record at a time. It is avoidable that I need to do a dirty job. ob = db.getdata(dr); }

Below is my JUnit:

@Test public void testDeleteMethod() throws Exception{ DeleteRequest mockDR = new DeleteRequest(); mockDR.setX(y); SomeObject mockOB = new SomeObject(); mockOB.setZ(k); // making a mockcall to the method before I can assert anything mockClassObject.deleteMethod(mockDR); }

The above JUnit method is getting struck in execution for ever and I understand that its not getting out of the while loop. How I can approach this issue. Just to mention am using Mockito framework and am not aware if at all there is any way to handle this situation in Mockito.

最满意答案

使用Mockito,您必须先模拟数据库连接,然后在测试方法之前使用@InjectMocks,构造函数注入或通过setter将其注入服务。 这就是我写测试的方式。

@Test
public void testDeleteMethod() throws Exception{
    DeleteRequest deleteRequest = new DeleteRequest();
    deleteRequest.setX(y);
    SomeObject someObject = new SomeObject();
    someObject.setZ(k);

    Database db = Mockito.mock(Database.class);
    // Notice chain of calls
    Mockito.when(db.getdata(deleteRequest))
        .thenReturn(someObject).thenReturn(null);

    // TODO Inject the Database object into your mockClassObject.

    // making a mock call to the method before I can assert anything
    mockClassObject.deleteMethod(deleteRequest);

    Mockito.verify(db, Mockito.times(2)).getdata(deleteRequest);
    Mockito.verify(db).deleteData(someObject);
}
 

您可以看到我在db.getData()方法上链接两次调用,第一次返回someObject ,第二次返回null 。

Using Mockito, you have to mock the db connection and inject it into your service using @InjectMocks, constructor injection, or via a setter before you test your method. This is how I would write your test.

@Test
public void testDeleteMethod() throws Exception{
    DeleteRequest deleteRequest = new DeleteRequest();
    deleteRequest.setX(y);
    SomeObject someObject = new SomeObject();
    someObject.setZ(k);

    Database db = Mockito.mock(Database.class);
    // Notice chain of calls
    Mockito.when(db.getdata(deleteRequest))
        .thenReturn(someObject).thenReturn(null);

    // TODO Inject the Database object into your mockClassObject.

    // making a mock call to the method before I can assert anything
    mockClassObject.deleteMethod(deleteRequest);

    Mockito.verify(db, Mockito.times(2)).getdata(deleteRequest);
    Mockito.verify(db).deleteData(someObject);
}
 

You can see that I chain the calls on the db.getData() method two times, the first time returns someObject and the second time returns null.

更多推荐

本文发布于:2023-08-01 13:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1359314.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   JUnit   loop   method

发布评论

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

>www.elefans.com

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