Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3286 : Mockito.only() points to the wanted call as unwanted if it is the first being calledIssue3286 #3287

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/org/mockito/internal/verification/Only.java
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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());
}
}