Comments (3)
Expected behaviour should be consistent with the other argument matchers. In the simplest case that means it should not fail if there is at least a single matching verification. In assertArg(...)
's case this would mean there should be at least a single verification that does not throw a throwable of some sort. (junit's AssertionFailedError
extends Error
, not exception)
@Test
void assertArgShouldNotFailExample() {
Consumer<Integer> mock = mock();
mock.accept(1);
mock.accept(2);
verify(mock).accept(assertArg(i -> assertThat(i).isEqualTo(2)));
}
Because the below two examples do not fail either:
@Test
void verifyExampleA() {
Consumer<Integer> mock = mock();
mock.accept(1);
mock.accept(2);
verify(mock).accept(argThat(i -> i == 2));
}
@Test
void verifyExampleB() {
Consumer<Integer> mock = mock();
mock.accept(1);
mock.accept(2);
verify(mock).accept(eq(2));
}
from mockito.
In the code above, the 'someMock.someMethod' function is called twice with different arguments.
The two verify() functions written in the verification code will be called twice.
verify(someMock).someMethod(
assertArg(someInt -> assertThat(someInt).isEqualTo(1)),
anyString()
);
In the above code, assert false is thrown in assertThat() when called with '2' entered as the first parameter. I think it's a question of usage.
from mockito.
There is one way but it is not very elegant, so just treat it as a fun fact 🙂
You can make some static utility method similar to assertArg
:
public static <T> T assertArgs(Consumer<T>... assertions) {
AtomicInteger counter = new AtomicInteger();
return ArgumentMatchers.argThat(actual -> {
int index = counter.getAndIncrement();
if (assertions.length > index) {
assertions[index].accept(actual);
}
return true;
});
}
Then in your test you can write:
@Test
void verifyTwoDifferentCallsWithAssertArgs() {
someMock.someMethod(1, "any string");
someMock.someMethod(2, "any string");
verify(someMock, times(2)).someMethod(
assertArgs(
firstInt -> assertThat(firstInt).isEqualTo(1),
secondInt -> assertThat(secondInt).isEqualTo(2)),
anyString());
}
There is probably one more better way to do it, but I didn't check it yet: you could try to handle you assertion in assertArgs
so it doesn't break your test, but returns true
/false
+ assertion fail description. Then if verification fails, you have to pass assertion fail description to verification fail message (this can be probably done by properly implementing Matcher or something).
from mockito.
Related Issues (20)
- @Mock and MockedConstruction
- application HOT 1
- In TypeBasedCandidateFilter.isCompatibleTypes -> ClassCastException: class TypeVariableImpl cannot be cast to class Class HOT 9
- Spy does not see method called in feedback call HOT 3
- Mockito.mock(...) does not work inside an Arquillian integration test anymore HOT 1
- Feature Request: MockedConstruction needs a setInitializer method
- Mockito does not work on Java 21 HOT 3
- Mockito.mock() not mocking the Class in Java 17 HOT 1
- ArgumentMatcher for one property
- Tests failures after adding mockito-inline on Java 21
- springjpa+junit5+mockito+querydsl
- ungraceful failure from instrumented code
- AdditionalMatchers.and() and or() swap matcher order HOT 5
- Failing while building mockito-inline version 4.6.1 using ./gradlew build (org.mockito:mockito-inline:4.6.1) HOT 1
- Mockito does not work with OTEL extension api in classpath
- verify can't differ/handle logging methods with "Object... arguments" in signature
- Not possible to mock Thread.sleep
- WIthMockito feature request
- potential editorconfig enhancement HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mockito.