mockito / mockito

Most popular Mocking framework for unit tests written in Java
http://mockito.org
MIT License
14.91k stars 2.57k forks source link

[kotlin] RETURNS_DEEP_STUBS not working in instrumentation tests with kotlin-allopen #1334

Open marcosalis opened 6 years ago

marcosalis commented 6 years ago

Mockito 2.15.0 supportTest 1.0.1 Kotlin 1.2.30

I'm trying to mock (using RETURNS_DEEP_STUBS) some Kotlin data and normal classes in a library module that uses the kotlin-allopen compiler plugin in an Android instrumentation test. Standard mocking works fine, but no deep stubs are created, i.e. a String getter for a val constructor parameter still returns null. Is this a known limitation or should it work?

Code example:

@OpenClassOnDebug
class MyKotlinClass(val myString: String)
@RunWith(AndroidJUnit4.class)
public class MyInstrumentationTest {

    @Mock (answer = Answers.RETURNS_DEEP_STUBS)
    MyKotlinClass myClass;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void getItems() throws Exception {
        assertTrue(myClass.getMyString() != null); // this fails!
    }

}
marcosalis commented 6 years ago

I've just realised my example test also fails on normal unit tests, even with the /resources/mockito-extensions/org.mockito.plugins.MockMaker configuration with mock-maker-inline.

marcosalis commented 6 years ago

Any updates on this?

aidangrabe commented 6 years ago

I'm also hitting this issue. For big data classes it's a pain to have to mock everything manually

marcosalis commented 6 years ago

This is still happening with Mockito 2.18.3 and kotlin / kotlin-allopen 1.2.41