Skip to content

Commit

Permalink
Fix reporting for Mockito.only when wanted call is first (#3287)
Browse files Browse the repository at this point in the history
Fixes #3286
  • Loading branch information
PiotrPrzybylak committed Mar 5, 2024
1 parent d0a919c commit 4058f07
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/main/java/org/mockito/internal/verification/Only.java
Expand Up @@ -25,14 +25,16 @@ public void verify(VerificationData data) {
MatchableInvocation target = data.getTarget();
List<Invocation> invocations = data.getAllInvocations();
List<Invocation> chunk = findInvocations(invocations, target);
if (!chunk.isEmpty()) {
markVerified(chunk.get(0), target);
}
if (invocations.size() != 1 && !chunk.isEmpty()) {
Invocation unverified = findFirstUnverified(invocations);
throw noMoreInteractionsWanted(unverified, (List) invocations);
}
if (invocations.size() != 1 || chunk.isEmpty()) {
throw wantedButNotInvoked(target);
}
markVerified(chunk.get(0), target);
}

@Override
Expand Down
48 changes: 38 additions & 10 deletions src/test/java/org/mockito/internal/verification/OnlyTest.java
Expand Up @@ -10,28 +10,34 @@
import java.util.List;

import org.junit.Test;
import org.mockito.exceptions.base.MockitoAssertionError;
import org.mockito.Mock;
import org.mockito.exceptions.verification.NoInteractionsWanted;
import org.mockito.exceptions.verification.WantedButNotInvoked;
import org.mockito.internal.invocation.InvocationBuilder;
import org.mockito.internal.invocation.InvocationMatcher;
import org.mockito.internal.verification.api.VerificationData;
import org.mockito.invocation.Invocation;
import org.mockito.invocation.MatchableInvocation;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;

public class OnlyTest {
public class OnlyTest extends TestBase {

@Mock IMethods mock;

Only only = new Only();

public class VerificationDataStub implements VerificationData {
private final Invocation invocation;
public static class VerificationDataStub implements VerificationData {
private final Invocation[] invocations;
private final InvocationMatcher wanted;

public VerificationDataStub(InvocationMatcher wanted, Invocation invocation) {
this.invocation = invocation;
public VerificationDataStub(InvocationMatcher wanted, Invocation... invocations) {
this.invocations = invocations;
this.wanted = wanted;
}

public List<Invocation> getAllInvocations() {
return Arrays.asList(invocation);
return Arrays.asList(invocations);
}

@Override
Expand All @@ -45,7 +51,7 @@ public InvocationMatcher getWanted() {
}

@Test
public void shouldMarkAsVerified() {
public void shouldMarkAsVerifiedWhenAssertionSucceded() {
// given
Invocation invocation = new InvocationBuilder().toInvocation();
assertFalse(invocation.isVerified());
Expand All @@ -58,7 +64,7 @@ public void shouldMarkAsVerified() {
}

@Test
public void shouldNotMarkAsVerifiedWhenAssertionFailed() {
public void shouldNotMarkAsVerifiedWhenWantedButNotInvoked() {
// given
Invocation invocation = new InvocationBuilder().toInvocation();
assertFalse(invocation.isVerified());
Expand All @@ -69,10 +75,32 @@ public void shouldNotMarkAsVerifiedWhenAssertionFailed() {
new VerificationDataStub(
new InvocationBuilder().toInvocationMatcher(), invocation));
fail();
} catch (MockitoAssertionError e) {
} catch (WantedButNotInvoked e) {
}

// then
assertFalse(invocation.isVerified());
}

@Test
public void shouldMarkWantedOnlyAsVerified() {

// given
InvocationBuilder invocationBuilder = new InvocationBuilder();
Invocation wanted = invocationBuilder.mock(mock).simpleMethod().toInvocation();
Invocation different = new InvocationBuilder().mock(mock).differentMethod().toInvocation();

// when
try {
only.verify(
new VerificationDataStub(
invocationBuilder.toInvocationMatcher(), wanted, different));
fail();
} catch (NoInteractionsWanted e) {
}

// then
assertTrue(wanted.isVerified());
assertFalse(different.isVerified());
}
}

0 comments on commit 4058f07

Please sign in to comment.