English 中文(简体)
Mockito Junit测试案
原标题:Mockito Junit test case for while loop with break statement

I am writing Junit test case(Junit 5 with jupitar) for existing code that has while loop with break statement.

public int deleteRecords(int batchsize) {
   //some code here 
   int totalDeletedRecords = 0;
   while(true) {
       int deleted = deleteRecord();
       if (deleted == 0) { //means no record found to delete
          break;
       }
       totalDeletedRecords += deleted;
   }
}
private int deleteRecord()  {
    List<User> usersList = repository.findAll();  //get the records from the database; 
    if (CollectUtils.isEmpty()) {
        return 0;
    }
    //then deleting the total records batch wise
    //construct list with ids and pass that to delete method
    repository.delete(List of ids);
    return userList.size();
}

My Junit测试案

@Test
public void testDeleteRecordSuccess() {
    //mock repository call
    User user = User.builder().name("xyz").build();
    when(repository.findAll()).thenReturn(Arrays.asList(user));
    doNothing().when(repository).delete(anyList());
    int deleted = service.deleteRecords(1000);
    assertEquals(1, deleted);
}

按照模拟计算,记录总有1个大小, lo只执行。 休息需要停止,而且所有法典应涵盖范围。

让我知道,如何在这种情景中改变存放处的要求。

问题回答

或许应该让<条码>交存。

@Test
public void testDeleteRecordSuccess() {
    // Create a test user
    User user = User.builder().name("xyz").build();
    
    // Create two lists, one with the user and one empty
    List<User> listWithUsers = Arrays.asList(user);
    List<User> emptyList = Collections.emptyList();
    
    // Mock repository calls
    // First call to findAll() will return the list with users, second call will return an empty list
    when(repository.findAll()).thenReturn(listWithUsers, emptyList);
    
    // Mock the .delete() call to do nothing
    doNothing().when(repository).delete(anyList());

    // The service call should now proceed as expected, without any infinite loop
    int deleted = service.deleteRecords(1000);
    
    // Verify that records were  deleted 
    assertEquals(1, deleted);
    
    // Verify calls to mock object
    verify(repository, times(2)).findAll();
    verify(repository, times(1)).delete(anyList());
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签