Getting 'Misplaced or misused argument matcher detected' on mockito tests

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

Getting 'Misplaced or misused argument matcher detected' on mockito tests

问题

我有以下的单元测试:

@InjectMocks
private PaymentTicketService paymentTicketService;

@Test
public void blahblahTest() {

    ...

    String spokesmanDocumentNumber = "47246536809";
    PaymentTicketPdfResponse paymentTicketPdfResponse = Fixture.from(...).gimme(...);
   
    when(paymentTicketService.generatePaymentTicket(any(GeneratePaymentTicketRequest.class), spokesmanDocumentNumber)).thenReturn(paymentTicketPdfResponse);

    ...
}

测试方法如下:

public PaymentTicketPdfResponse generatePaymentTicket(GeneratePaymentTicketRequest generateBankTicketRequest, String spokesmanDocumentNumber) {
    ...
}

在我的测试的when子句上,我收到一个检测到错误的错误位置或错误使用的参数匹配器错误。它说我不能在验证或存根之外使用参数匹配器。
我真的不明白这个错误,因为我正在向模拟的方法传递any(GeneratePaymentTicketRequest.class)和一个实际的字符串。

我在这里做错了什么?

英文:

I have the following Unit test:

@InjectMocks
private PaymentTicketService paymentTicketService;

@Test
public void blahblahTest() {

    ...

    String spokesmanDocumentNumber = "47246536809";
    PaymentTicketPdfResponse paymentTicketPdfResponse = Fixture.from(...).gimme(...);
   
    when(paymentTicketService.generatePaymentTicket(any(GeneratePaymentTicketRequest.class), spokesmanDocumentNumber)).thenReturn(paymentTicketPdfResponse);

    ...
}

The testing method is this one:

public PaymentTicketPdfResponse generatePaymentTicket(GeneratePaymentTicketRequest generateBankTicketRequest, String spokesmanDocumentNumber) {
    ...
}

I'm getting a Misplaced or misused argument matcher detected error on the 'when' clause of my test. It says that I cannot use argument matchers outside of verification or stubbing.
I don't really understand this error, as I'm passing any(GeneratePaymentTicketRequest.class) and a acctual string to the mocked method.

What I'm doing wrong here?

答案1

得分: 2

无法混合使用匹配器和实际值。请将值包装在匹配器中:

when(paymentTicketService.generatePaymentTicket(any(GeneratePaymentTicketRequest.class), eq(spokesmanDocumentNumber))).thenReturn(paymentTicketPdfResponse);

英文:

You can't mix matchers and actual values. Wrap the value in a matcher:


when(paymentTicketService.generatePaymentTicket(any(GeneratePaymentTicketRequest.class), eq(spokesmanDocumentNumber))).thenReturn(paymentTicketPdfResponse);

huangapple
  • 本文由 发表于 2023年7月17日 23:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705774.html
匿名

发表评论

匿名网友

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

确定