Skip to content

Commit

Permalink
Fixes mockito#3331 : AdditionalMatchers.and() and or() swap matcher o…
Browse files Browse the repository at this point in the history
…rder
  • Loading branch information
dev-jonghoonpark committed May 9, 2024
1 parent fb9ff6d commit 757a415
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public List<LocalizedMatcher> pullLocalizedMatchers() {
public void reportAnd() {
assertStateFor("And(?)", TWO_SUB_MATCHERS);

ArgumentMatcher<?> m1 = popMatcher();
ArgumentMatcher<?> m2 = popMatcher();
ArgumentMatcher<?> m1 = popMatcher();

reportMatcher(new And(m1, m2));
}
Expand All @@ -52,8 +52,8 @@ public void reportAnd() {
public void reportOr() {
assertStateFor("Or(?)", TWO_SUB_MATCHERS);

ArgumentMatcher<?> m1 = popMatcher();
ArgumentMatcher<?> m2 = popMatcher();
ArgumentMatcher<?> m1 = popMatcher();

reportMatcher(new Or(m1, m2));
}
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/org/mockitousage/matchers/AdditionalMatcherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.matchers;

import org.junit.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.mockito.AdditionalMatchers.and;
import static org.mockito.AdditionalMatchers.or;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class AdditionalMatcherTest {

interface Foo {
int doSomeThing(Object param);
}

@Test
public void shouldMatchArgumentsSequentiallyWithAnd() {
Foo foo = mock(Foo.class);

when(
foo.doSomeThing(
and(
any(String.class),
argThat(String::isEmpty)
)))
.thenReturn(1);

assertNotEquals(foo.doSomeThing(1), 1);
}

@Test
public void shouldMatchArgumentsSequentiallyWithOr() {
Foo foo = mock(Foo.class);

when(
foo.doSomeThing(
or(
any(Integer.class),
and(
any(Object.class),
argThat(String::isEmpty)
)
)))
.thenReturn(1);

assertEquals(foo.doSomeThing(1), 1);
}
}

0 comments on commit 757a415

Please sign in to comment.