NotAMockException在尝试使用verify方法时发生

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

NotAMockException when trying to use verify method

问题

The error you're encountering, "NotAMockException," is because Mockito is treating vinService as not being a mock object when you try to verify its method. This issue can be resolved by ensuring that vinService is correctly mocked and that you're verifying the correct method on the mock object.

Here's the corrected code:

  1. @ExtendWith(MockitoExtension.class)
  2. public class CarServiceTest {
  3. private static final String VIN = "1G100000000000000";
  4. @Mock
  5. private CarService carService;
  6. @Mock
  7. private VinService vinService;
  8. @Test
  9. void vinShouldBeCheckedSuccessfully() {
  10. //when
  11. carService.checkCar(VIN);
  12. //then
  13. Mockito.verify(vinService, Mockito.times(1)).checkVin(
  14. Mockito.anyList(),
  15. Mockito.anyString(),
  16. Mockito.any()); // Consumer parameter
  17. }
  18. }

Make sure that you have correctly annotated vinService as a mock using @Mock. The method name in verify should match the method you want to verify on the vinService mock, which in your case is checkVin. Additionally, make sure that the case of the method name matches exactly with the method in your VinService class (e.g., checkVin instead of checkVIN).

英文:

My first steps with unit tests and Mockito. I would like to use verify() method to check if it was called once.

  1. public class CarService {
  2. private final VinService vinService;
  3. public void checkCar(String VIN) {
  4. (here there is some business logic like:create list, pass the VIN, and add Consumer)
  5. vinService.checkVin(list, VIN, consumer); //checkVin inside VinService.class is also void
  6. }
  7. }
  8. @ExtendWith(MockitoExtension.class)
  9. public class CarServiceTest {
  10. private static final String VIN = "1G100000000000000"
  11. @Mock
  12. private CarService carService;
  13. @Mock
  14. private VinService vinService;
  15. @Test
  16. void vinShouldBeCheckedSuccessfully() {
  17. //when
  18. carService.checkCar(VIN);
  19. //then
  20. Mockito.verify(vinService, Mockito.times(1)).checkVIN(
  21. Mockito.anyList(),
  22. Mockito.anyString(),
  23. Mockito.any()); // Consumer parameter
  24. }
  25. }

However I am getting the error:

  1. org.mockito.exceptions.misusing.NotAMockException:
  2. Argument passed to verify() is of type VinService and is not a mock!
  3. Make sure you place the parenthesis correctly!
  4. See the examples of correct verifications:
  5. verify(mock).someMethod();
  6. verify(mock, times(10)).someMethod();
  7. verify(mock, atLeastOnce()).someMethod();

Could you help me to understand why?

答案1

得分: 1

Here is the translated code:

实际上,这非常简单。您只需要在您正在测试的服务中使用 @InjectMocks 而不是 @Mock

  1. @InjectMocks
  2. private CarService carService;

此注释表示 Mockito 将正确地将所有模拟对象注入到您的服务中(将 VinService 注入到 CarService 中)。

更新以供评论使用
如果您在某处初始化您的服务,那么您可以轻松地在测试中添加您的模拟对象。

  1. @ExtendWith(MockitoExtension.class)
  2. public class CarServiceTest {
  3. private static final String VIN = "1G100000000000000";
  4. @InjectMocks
  5. private CarService carService;
  6. @Test
  7. void vinShouldBeCheckedSuccessfully() {
  8. VinService vinService = Mockito.mock(VinService.class);
  9. //when
  10. carService.checkCar(VIN);
  11. //then
  12. Mockito.verify(vinService, Mockito.times(1)).checkVIN(
  13. Mockito.anyList(),
  14. Mockito.anyString(),
  15. Mockito.any()); // Consumer parameter
  16. }
  17. }
英文:

Actually, it`s very simple. You just need to use @InjectMocks instead @Mock for the service that you are testing:

  1. @InjectMocks
  2. private CarService carService;

This annotation says that Mockito will properly inject all mocks into your service(VinService into CarService).

Updated for comment
if you have somewhere initializing your service then you can easily add your mock inside test.

  1. @ExtendWith(MockitoExtension.class)
  2. public class CarServiceTest {
  3. private static final String VIN = "1G100000000000000"
  4. @InjectMocks
  5. private CarService carService;
  6. @Test
  7. void vinShouldBeCheckedSuccessfully() {
  8. VinService vinService = Mockito.mock(VinService.class);
  9. //when
  10. carService.checkCar(VIN);
  11. //then
  12. Mockito.verify(vinService, Mockito.times(1)).checkVIN(
  13. Mockito.anyList(),
  14. Mockito.anyString(),
  15. Mockito.any()); // Consumer parameter
  16. }
  17. }

huangapple
  • 本文由 发表于 2023年5月22日 00:49:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300968.html
匿名

发表评论

匿名网友

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

确定