Wanted but not invoked: Mockito

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

Wanted but not invoked:Mockito

问题

public class Vote {
    public void isEligibleToVote(int age){
        if(age > 18)
            getResult("yes");
        else
            getResult("no");
    }

    public String getResult(String result){
        return result;
    }
}
@ExtendWith(MockitoExtension.class)
public class VoteTest {

    @Mock
    Vote vote;

    @Test
    public void isEligibleToVote_test(){
        ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
        vote.isEligibleToVote(18);
        verify(vote).getResult(stringArgumentCaptor.capture());
        assertEquals("yes", stringArgumentCaptor.getValue());
    }
}

Error Stacktrace:

Wanted but not invoked:
vote.getResult(<Capturing argument>);
-> at VoteTest.isEligibleToVote_test(VoteTest.java:24)

However, there was exactly 1 interaction with this mock:
vote.isEligibleToVote(18);
-> at VoteTest.isEligibleToVote_test(VoteTest.java:23)

Wanted but not invoked:
vote.getResult(<Capturing argument>);
-> at VoteTest.isEligibleToVote_test(VoteTest.java:24)

However, there was exactly 1 interaction with this mock:
vote.isEligibleToVote(18);
-> at VoteTest.isEligibleToVote_test(VoteTest.java:23)

	at VoteTest.isEligibleToVote_test(VoteTest.java:24)

I understand vote is a mock, but I want to know how I can call vote.isEligibleToVote(18) and capture the arguments of getResult from inside the if and else conditions.

英文:

I am trying to test below Vote.java which has a method called isEligibleToVote() which in turn calls
another method getResult() after a check. I am trying to capture the argument and assert but while doing so I am getting Wanted but not invoked:
vote.getResult(&lt;Capturing argument&gt;);

I understand this is because of vote.isEligibleToVote(18); as I have declared vote using @Mock which is incorrect, but I don't know how can I test this class using ArgumentCaptor and verify methods.

Vote.java

public class Vote {
public void isEligibleToVote(int age){
    if(age&gt;18)
        getResult(&quot;yes&quot;);
    else
        getResult(&quot;no&quot;);

}

public String getResult(String result){
    return result;
}

}

VoteTest.java

@ExtendWith(MockitoExtension.class)
public class VoteTest {

    @Mock
    Vote vote;

    @Test
    public void isEligibleToVote_test(){
        ArgumentCaptor&lt;String&gt; stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
        vote.isEligibleToVote(18);
        verify(vote).getResult(stringArgumentCaptor.capture());
        assertEquals(&quot;yes&quot;, stringArgumentCaptor.getValue());
    }
}

Error Staacktrace

    Wanted but not invoked:
vote.getResult(&lt;Capturing argument&gt;);
-&gt; at VoteTest.isEligibleToVote_test(VoteTest.java:24)

However, there was exactly 1 interaction with this mock:
vote.isEligibletoVote(18);
-&gt; at VoteTest.isEligibleToVote_test(VoteTest.java:23)


Wanted but not invoked:
vote.getResult(&lt;Capturing argument&gt;);
-&gt; at VoteTest.isEligibleToVote_test(VoteTest.java:24)

However, there was exactly 1 interaction with this mock:
vote.isEligibletoVote(18);
-&gt; at VoteTest.isEligibleToVote_test(VoteTest.java:23)


	at VoteTest.isEligibleToVote_test(VoteTest.java:24)

I understand vote is mock, but I want to know how can I call vote.iseligibleTovote(18) and get the arguments of getResult from inside if and else condition

答案1

得分: 5

使用@Spy代替@Mock进行验证。您必须在模拟对象的任何方法上调用when(),而您目前没有这样做,因此出现了期望但未调用的情况。

英文:

Use @Spy instead of @Mock to do the verification. You must call when() on any methods of a mocked object which you aren't doing hence the wanted but not invoked

答案2

得分: 3

TLDR: 使用 @Spy 替代 @Mock

当您想要模拟一个对象并将其插入另一个对象时,使用 @Mock
当您想要查看中间值时,使用 @Spy。在您的情况下是 getResult()

使用以下代码以获得更好的理解:

@RunWith(MockitoJUnitRunner.class)
public class VoteTest {

    @Spy
    Vote vote;

    @Test
    public void isEligibleToVote_test(){
        ArgumentCaptor<String> stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
        vote.isEligibleToVote(19);
        verify(vote).getResult(stringArgumentCaptor.capture());
        assertEquals("yes", stringArgumentCaptor.getValue());
   }
}
英文:

TLDR: Use @Spy insted of @Mock

@Mock is used when you want to mock an object and insert it into another object.<br>
@Spy is used when you want to look at the intermediate values. In your case getResult()

Use the below code for better understanding:

@RunWith(MockitoJUnitRunner.class)
public class VoteTest {

    @Spy
    Vote vote;

    @Test
    public void isEligibleToVote_test(){
        ArgumentCaptor&lt;String&gt; stringArgumentCaptor = ArgumentCaptor.forClass(String.class);
        vote.isEligibleToVote(19);
        verify(vote).getResult(stringArgumentCaptor.capture());
        assertEquals(&quot;yes&quot;, stringArgumentCaptor.getValue());
   }
}

答案3

得分: 0

你的投票是一个模拟,你不能对它调用方法。

看一下这个 when(vote.isEligibleToVote(18)).thenReturn("你想要的")

编辑:请查看 https://www.baeldung.com/mockito-behavior

英文:

your vote is a mock you can't call method on him.

look at when when(vote.isEligibleToVote(18)).thenReturn(&quot;WHat you want&quot;)

edit : look at https://www.baeldung.com/mockito-behavior

huangapple
  • 本文由 发表于 2020年9月17日 20:29:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63938055.html
匿名

发表评论

匿名网友

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

确定