Comments (9)
Hello @fishb6nes ,
I think this is (almost) expected behavior.
If I use the following Java code:
import static org.mockito.Mockito.*;
public class Test {
public static void main(String[] args){
Foo mock = mock(Foo.class);
when(mock.getText()).thenReturn(any());
System.out.println(mock.getText());
}
}
public interface Foo {
String getText();
}
I get the following exception:
Exception in thread "main" org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
0 matchers expected, 1 recorded:
-> at org.Test.main(Test.java:8)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
at org.Test.main(Test.java:9)
Looking at their docs it seems like you aren't supposed to return a matcher.
When trying to return anyDouble()
I get the same exception.
I think we rather need a way to also trigger the "correct exception" from Mockito, because we're getting a reflection created null
when Kotlin doesn't accept null (therefore IllegalStateException
).
from kluent.
Another hint is in their Reporter
from kluent.
Thank you for the detailed response.
Hm, so how would we go about stubbing methods that take non-primitive parameters if any(NonPrimitiveClass::class)
throws an exception?
interface Foo {
fun test(foo: Foo): String
}
describe("Foo") {
val mockFoo = mock(Foo::class)
given("it returns 'Test'") {
When calling mockFoo.test(any(Foo::class)) itReturns "Test" // throws "java.lang.IllegalStateException: any(kClass.javaObjectType) must not be null"
}
}
from kluent.
I just tested it and you're right, it doesn't work (we're missing tests for this)
I tested mockito-kotlin and their any()
method is working. From a quick look they heavily use reflection to create an instance.
The problem seems to be that Mockito often returns null on their any()
method, whereas our return type is not nullable. Catching that exception isn't possible because it occurs in the return.
I do see 2 possible solutions:
a) implement our own way to create such objects
b) depend on mockito-kotlin instead of Mockito and wrap mockito-kotlin into the fluent stuff.
I think I prefer the 2nd option because it is also a good opportunity to complete the rest of stubbing/mocking which isn't in Kluent yet :-)
from kluent.
I agree, lets not reinvent the wheel.
I'm not too familiar with this whole 'infix' function thingymebob, but let me know if there's anything I can contribute to. Otherwise I will patiently await any further developments.
from kluent.
I'll get on to this this weekend ;-) Thank you for finding this problem in the first place!
from kluent.
Could you have a look at the PR and the one reviewed commit if this would handle this case?
from kluent.
Awesome, matching classes appears to work like a charm locally 👍
from kluent.
version 1.17 has just been released and contains the fix for this issue :-)
thanks again!
from kluent.
Related Issues (20)
- Publish to Maven Central or other repositories HOT 12
- Cannot use Kluent's mockito-kotlin wrapper since 1.65 HOT 4
- Add new `shouldContainLineWithString` assertion HOT 7
- Improve errors HOT 5
- Semantic versioning to allow +notation on library updates HOT 1
- any() doesn't work in 1.65 HOT 1
- All assertions should support assertSoftly HOT 5
- Support diffs in Intellij HOT 4
- Was VerifyNotCalled removed? HOT 1
- Consistent file and code formatting HOT 7
- Vulnerabilities in junit dependency HOT 1
- changed inheritance from AssertionError caused assertion not working with awaitility HOT 1
- Project maintenance HOT 2
- ``should throw`` for suspended callables missing HOT 1
- Potentially misleading behavior of shoudNotThrow
- kluent-1.69 is configured for jvm version 17 HOT 3
- NoSuchMethodError 'boolean java.lang.CharSequence.isEmpty()' with JRE 11 HOT 5
- Use the newest Multiplatform project structure
- IllegalStateException is thrown when shouldBeEquivalentTo comparison fails
- Android Studio Iguana doesn't show "Show diff" when assert fails HOT 1
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 kluent.