Git Product home page Git Product logo

Comments (9)

MarkusAmshove avatar MarkusAmshove commented on August 17, 2024

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.

MarkusAmshove avatar MarkusAmshove commented on August 17, 2024

Another hint is in their Reporter

from kluent.

fishb6nes avatar fishb6nes commented on August 17, 2024

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.

MarkusAmshove avatar MarkusAmshove commented on August 17, 2024

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.

fishb6nes avatar fishb6nes commented on August 17, 2024

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.

MarkusAmshove avatar MarkusAmshove commented on August 17, 2024

I'll get on to this this weekend ;-) Thank you for finding this problem in the first place!

from kluent.

MarkusAmshove avatar MarkusAmshove commented on August 17, 2024

Could you have a look at the PR and the one reviewed commit if this would handle this case?

@fishb6nes

from kluent.

fishb6nes avatar fishb6nes commented on August 17, 2024

Awesome, matching classes appears to work like a charm locally 👍

from kluent.

MarkusAmshove avatar MarkusAmshove commented on August 17, 2024

version 1.17 has just been released and contains the fix for this issue :-)

thanks again!

from kluent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.