Skip to content

Commit

Permalink
AdditionalMatchers.and() and or() swap matcher order (#3335)
Browse files Browse the repository at this point in the history
Fixes #3331
  • Loading branch information
dev-jonghoonpark committed May 11, 2024
1 parent f3821ff commit 12cef84
Show file tree
Hide file tree
Showing 2 changed files with 45 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
43 changes: 43 additions & 0 deletions src/test/java/org/mockitousage/matchers/AdditionalMatcherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 12cef84

Please sign in to comment.