Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mockito extension is ignored when running with Spek #36

Open
vokilam opened this issue May 7, 2017 · 16 comments
Open

Mockito extension is ignored when running with Spek #36

vokilam opened this issue May 7, 2017 · 16 comments

Comments

@vokilam
Copy link

vokilam commented May 7, 2017

Hi.
There is an incubating feature of mockito framework that allows to mock previously unmockable entities like final classes.

Here is a sample project that demonstrates the issue: https://github.com/vokilam/test-spec-mockito/tree/master/app/src/test/java/com/vokilam/test_spec_mockito.

The problem is that this feature is ignored when running with Spek plugin (see ExampleUnitTestKotlinSpek.kt). Both java junit test (ExampleUnitTest.java) and kotlin junit test (ExampleUnitTestKotlin.kt) works as expected (final class is mocked).

In case of Spek test/resources/mockito-extensions/org.mockito.plugins.MockMaker file is ignored and I got the following output:

Testing started at 18:56 ...
May 07, 2017 6:56:50 PM org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry loadTestEngines
INFO: Discovered TestEngines with IDs: [spek]
org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class com.vokilam.test_spec_mockito.MainActivity
Mockito cannot mock/spy because :
 - final class

I'm not 100% sure this problem is connected with Spek plugin, however I wanted to identify a problem and to direct it to a corresponding tracker.

@raniejade
Copy link
Owner

In your kotlin unit test please remove @Throws(Exception::class), it should fail as well. Can you try adding :assembleDebugUnitTest as a before run task for the run configuration?

@vokilam
Copy link
Author

vokilam commented May 8, 2017

Removed @Throws annotation – same behavior as before: kotlin unit test is passing, final class is mocked (no error messages from mockito).

Added :assembleDebugUnitTest task to default configuration for Spek (as described here). Before that I did it manually. Same behavior for spek test – mokito extension is not working.

@raniejade
Copy link
Owner

That's weird, I'll have to test with an android project. It's working with a non-android one. I'll get back to you if I have leads.

@vokilam
Copy link
Author

vokilam commented May 8, 2017

My environment is

Android Studio 2.3.1
Build #AI-162.3871768, built on April 1, 2017
JRE: 1.8.0_112-release-b06 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

macOS Sierra 10.12.4

@vokilam
Copy link
Author

vokilam commented May 15, 2017

Anything I can help you with?

@raniejade
Copy link
Owner

@vokilam Sorry, I haven't really have started the investigation - I'm a bit busy with work stuff right now.

@raniejade
Copy link
Owner

@vokilam i'm setting up my android environment, by any chance is the test passing when running from the command line?

@vokilam
Copy link
Author

vokilam commented May 21, 2017 via email

@raniejade
Copy link
Owner

I'm not sure - not an android guy, probably the task that runs the unit tests :)

@vokilam
Copy link
Author

vokilam commented May 22, 2017

Let me check, I will get back

@vokilam
Copy link
Author

vokilam commented May 23, 2017

./gradlew test – completes successfully, reports are in app/build/reports/tests folder. I had to annotate test class with @RunWith(JUnitPlatform::class) so it can be picked by gradle.

@raniejade
Copy link
Owner

So most likely the problem is within the IDE. While using @RunWith(JUnitPlatform::class) is the test passing when running from the IDE? If it passes, the problem is with the spek plugin, if not most likely it's an IDE issue.

@vokilam
Copy link
Author

vokilam commented May 23, 2017

I'm a bit confused with results I get. I will check in IDEA also and get back :)

@vokilam
Copy link
Author

vokilam commented May 26, 2017

Okay, here are my results.

Set up IDEA
I set up a gradle based java project in IDEA with junit 5.
I created a classic java test and spek test. Each test mocks a public final class MyClass.

MyClass test = Mockito.mock(MyClass.class);

I created a file test/resources/mockito-extensions/org.mockito.plugins.MockMaker with following line (configure Mockito extension – now it should mock final classes):

mock-maker-inline

Results

  1. Run ./gradlew junitPlatformTest – two tests succeeded
  2. Run java test from IDE – success
  3. Run spek test from IDE (spek plugin) – success
  4. Comment mock-maker-inline line and rerun previous tests – all failed with following exception (as expected):
    org.mockito.exceptions.base.MockitoException:
    Cannot mock/spy class MyClass
    Mockito cannot mock/spy because: final class

Set up Android Studio
I set up a gradle based Android project with junit 4.
Same setup for test classes. Spek test class has @RunWith(JUnitPlatform::class) so it can be visible when testing from console.
Same setup for mockito extension.

Results

  1. Run ./gradlew testDebugUnitTest – results are in build/reports/tests/testDebugUnitTest, two tests succeeded
  2. Run java test from IDE – success
  3. Run spek test from IDE – failed with MockitoException: Cannot mock/spy class MainActivity – that was not expected
  4. Comment mock-maker-inline line and rerun previous tests – all failed with exception.

Set up Android Studio JUnit 5
I set up a gradle based Android project with junit 5.
Same setup for test classes. @RunWith(JUnitPlatform::class) annotation is removed because test is already visible for console command.
Same setup for mockito extension.

  1. Run ./gradlew junitPlatformTestDebug – two tests succeeded
  2. Run java test from IDE – success
  3. Run spek test from IDE – failed with MockitoException: Cannot mock/spy class MainActivity – that was not expected
  4. Comment mock-maker-inline line and rerun previous tests – all failed with exception.

Summing up: Mockito extension is not applied when test is run from Android Studio via Spek plugin (junit4 and junit5). Same setup for IDEA (junit5) works fine.

@raniejade
Copy link
Owner

Hi, sorry for the very long delay. I still cant replicate it, can you try moving test/resources/mockito-extensions/org.mockito.plugins.MockMaker to main/resources/mockito-extensions/org.mockito.plugins.MockMaker and see if it passes?

@elyashivlavi
Copy link

Hi, I'm facing the same issue...
I tried put this extension in main/resources, but still get the same error...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants