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

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

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:

@ExtendWith(MockitoExtension.class)
public class CarServiceTest {

    private static final String VIN = "1G100000000000000";

    @Mock
    private CarService carService;

    @Mock
    private VinService vinService;

    @Test
    void vinShouldBeCheckedSuccessfully() {

        //when
        carService.checkCar(VIN);

        //then
        Mockito.verify(vinService, Mockito.times(1)).checkVin(
                Mockito.anyList(),
                Mockito.anyString(),
                Mockito.any()); // Consumer parameter
    }
}

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.

public class CarService {

    private final VinService vinService;

    public void checkCar(String VIN) {
        (here there is some business logic like:create list, pass the VIN, and add Consumer)

        vinService.checkVin(list, VIN, consumer); //checkVin inside VinService.class is also void
    }
}

@ExtendWith(MockitoExtension.class)
public class CarServiceTest {

    private static final String VIN = "1G100000000000000"

    @Mock
    private CarService carService;

    @Mock
    private VinService vinService;

    @Test
    void vinShouldBeCheckedSuccessfully() {

        //when
        carService.checkCar(VIN);

        //then
        Mockito.verify(vinService, Mockito.times(1)).checkVIN(
                Mockito.anyList(),
                Mockito.anyString(),
                Mockito.any()); // Consumer parameter
    }
}

However I am getting the error:

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

Could you help me to understand why?

答案1

得分: 1

Here is the translated code:

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

@InjectMocks
private CarService carService;

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

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

@ExtendWith(MockitoExtension.class)
public class CarServiceTest {

    private static final String VIN = "1G100000000000000";

    @InjectMocks
    private CarService carService;

    @Test
    void vinShouldBeCheckedSuccessfully() {
        VinService vinService = Mockito.mock(VinService.class);

        //when
        carService.checkCar(VIN);

        //then
        Mockito.verify(vinService, Mockito.times(1)).checkVIN(
            Mockito.anyList(),
            Mockito.anyString(),
            Mockito.any()); // Consumer parameter
    }
}
英文:

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

@InjectMocks
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.

@ExtendWith(MockitoExtension.class)
    public class CarServiceTest {

        private static final String VIN = "1G100000000000000"

        @InjectMocks
        private CarService carService;

        @Test
        void vinShouldBeCheckedSuccessfully() {
            VinService vinService = Mockito.mock(VinService.class);

            //when
            carService.checkCar(VIN);

            //then
            Mockito.verify(vinService, Mockito.times(1)).checkVIN(
                Mockito.anyList(),
                Mockito.anyString(),
                Mockito.any()); // Consumer parameter
        }
    }

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:

确定