简体   繁体   English

Mockito 单元测试:参数匹配器的使用无效

[英]Mockito unit testing: Invalid use of argument matchers

I see dozens of occurrences of this same Mockito exception but, in my case, I really don't understand what am I suppose to do as I don't mix matchers and values.我看到数十次出现同样的 Mockito 异常,但就我而言,我真的不明白我应该做什么,因为我不混合匹配器和值。 Here is the case:情况如下:

I have a REST resource which invokes a service and I want to mock the service and to inject the mocked service into the REST resource.我有一个调用服务的 REST 资源,我想模拟该服务并将模拟的服务注入 REST 资源。 Here is the code:这是代码:

  @Mock
  private Service service;
  @InjectMocks
  private Resource resource;
  ...
  @Test
  public void test() throws URISyntaxException
  {
    assertThat(resource).isNotNull();
    doNothing().when(service).createItem(anyLong(), any(Item.class));
    when(resource.createItem(any(Item.class))).thenReturn(Response.created(new URI("/items")).build());
    Response response = resource.createItem(item);
    Mockito.verify(service).createItem(item);
  }

And the exception:还有一个例外:

-> at fr.simplex_software.micro_services_without_spring.customers.tests.TestCustomers.testCreateCustomer(TestCustomers.java:59)

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));

For more info see javadoc for Matchers class.

I don't think that I'm mixing matchers and values here.我不认为我在这里混合了匹配器和值。 Could anyone please help?有人可以帮忙吗?

You are trying to stub your class under test ( Resource ) that is not a mock.您正在尝试存根您的 class 正在测试( Resource )不是模拟的。

when(resource.createItem(any(Item.class))).thenReturn(Response.created(new URI("/items")).build());

When writing a unit test for your Resource class there shouldn't be the need to mock any internals of this class and only its collaborators.在为您的Resource class 编写单元测试时,不需要模拟此 class 的任何内部结构以及它的协作者。

  • @Mock creates a mock of the collaborator ( Service ) of your class. @Mock创建 class 的合作者 ( Service ) 的模拟。
  • @InjectMocks instantiates a real instance of your class under test while injecting all mocks to it. @InjectMocks实例化您正在测试的 class 的真实实例,同时将所有模拟注入其中。

You can't stub any methods of your tested class because you are working with a real instance of it.您不能存根测试过的 class 的任何方法,因为您正在使用它的真实实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Mockito中的anyString()抛出参数匹配器的无效使用 - anyString() in mockito is throwing an invalid use of argument matchers Java Mockito 参数匹配器的无效使用 - Java Mockito Invalid use of argument matchers Mockito 错误:.InvalidUseOfMatchersException:参数匹配器的使用无效 - Mockito Error :.InvalidUseOfMatchersException: Invalid use of argument matchers Mockito varargs参数匹配器的无效使用 - Mockito varargs Invalid use of argument matchers mockito 测试错误无效使用参数匹配器 - mockito test error Invalid use of argument matchers Mockito org.mockito.exceptions.misusing.InvalidUseOfMatchersException:无效使用参数匹配器! 预计有0个符合条件的记录,有1个记录: - Mockito org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 0 matchers expected, 1 recorded: Java Mockito useConstructor withSettings,错误参数匹配器的无效使用 - Java Mockito useConstructor withSettings, error Invalid use of argument matchers org.mockito.exceptions.misusing.InvalidUseOfMatchersException:参数匹配器的无效使用 - org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers 如何修复 Mockito Invalid use of argument matchers 错误 - How to fix Mockito Invalid use of argument matchers error Mockito - 无效使用匹配器异常 - Mockito - Invalid use of Matchers Exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM