创建一个测试用例,测试保存对象并返回长整型数值的服务方法。

huangapple go评论70阅读模式
英文:

Create test for service method that saves an object and returns long value

问题

I am trying to test a service method. The method takes in information, uses this information to create an object. Than it saves this object. Than it returns the id of that object.

当我尝试测试这个服务方法时,该方法接收信息,使用这些信息创建一个对象,然后保存该对象,最后返回该对象的ID。

When I am trying to test this, my test keeps failing, stating that the returned object is null. I am wondering how I can test this. At this point I am trying to mock the generated object with an getId() that returns 1L. Than I let the save method return the mocked object. Also this doesn't seem to work.

当我尝试测试时,我的测试一直失败,指出返回的对象为空。我想知道如何测试这个。在这一点上,我尝试模拟生成的对象,使其具有返回1L的getId()。然后,我让保存方法返回模拟的对象。但这似乎也不起作用。

I could really use some help how I should properly make a test for this method.

我真的需要一些帮助,了解如何正确地为这个方法编写测试。

Example method:

示例方法
```java
public long addNotification(ObjectWithInformation objectWithInformation){
   NewObject newObject = // retrieve information based on objectWithInformation
   repository.save(newObject);
   repository.flush();
   return newObject.getId();
}

How I am currently trying to test (test compiles, getId() still returns nullvalue):

我目前尝试测试的方式(测试编译通过,但getId()仍然返回null值):

@Test
void addNotification(){
     NewObject newObjectmock = mock(NewObject.class);
     Mockito.when(newObjectmock.getId()).thenReturn(1L);
     when(repository.save(Mockito.any(NewObject.class))).thenReturn(newObjectmock);

     service.addNotification(ObjectWithInformation objectWithInformation);

     //verify methods, which I cannot reach due to getId() returning null in the service method.
}
英文:

I am trying to test a service method. The method takes in information, uses this information to create an object. Than it saves this object. Than it returns the id of that object.

When I am trying to test this, my test keeps failing, stating that the returned object is null. I am wondering how I can test this. At this
point I am trying to mock the generated object with an getId() that returns 1L. Than I let the save method return the mocked object. Also this doesn't seem to work.

I could really use some help how I should properly make a test for this method.

Example method:

public long addNotification(ObjectWithInformation objectWithInformation){
       NewObject newObject = // retrieve information based on objectWithInformation
       repository.save(newObject);
       repository.flush();
       return newObject.getId();

}

How I am currently trying to test (test compiles, getId() still returns nullvalue):

@Test
void addNotification(){
     NewObject newObjectmock = mock(NewObject.class);
     Mockito.when(newObjectmock.getId()).thenReturn(1L);
     when(repository.save(Mockito.any(NewObject.class))).thenReturn(newObjectmock);

     service.addNotification(ObjectWithInformation objectWithInformation);

     //verify methods, which I cannot reach due to getId() returning null in the service method.

}

答案1

得分: 1

你不能对在函数内生成的对象进行测试,也不能提前对其进行模拟。这只能通过接受对象作为参数并返回这些对象的函数来实现。

你的模拟是正确的,但是JUnit不能训练函数内的newObject以返回一个id。所以你必须训练你的存储库以返回一个带有id的模拟对象(这是你做对了的地方)。你的函数有一个中间步骤,你传递训练过的方法并返回一个空值,因为newObject没有id值。你将不得不在函数中立即返回id。

此外,使用saveAndFlush而不是save()和flush()。两者都可以工作,但第一个更清晰,对测试效果更好。

这将使其正常工作:


when(repository.saveAndFlush(Mockito.any(NewObject.class))).thenReturn(newObjectmock);

public long addNotification(ObjectWithInformation objectWithInformation){
       NewObject newObject = // 根据objectWithInformation检索信息
       return repository.saveAndFlush(newObject).getId();
}

英文:

You cannot perform tests on an object generated inside a function, nor can you mock it in advance. This can only be achieved with functions that accept object as parameters and return those objects.

You're mock is correct, but junit cannot train the newobject inside the function to return an id. So you have to train your repository to return a mock object with the id (this is what you did correct). Your function has a between step, you pass the trained method and return a null value cause the newObject holds no id value. You will have to return the id immediatly in your function.

Also use saveAndFlush instead of save() and flush(). Both will work but the first is cleaner and works better for the test.

this will make it work:


when(repository.saveAndFlush(Mockito.any(NewObject.class))).thenReturn(newObjectmock);

public long addNotification(ObjectWithInformation objectWithInformation){
       NewObject newObject = // retrieve information based on objectWithInformation
       return repository.saveAndFlush(newObject).getId();
}

huangapple
  • 本文由 发表于 2023年2月14日 21:23:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定