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

Feature: verify strict mocking in Mockito sense #334

Closed
mikezx6r opened this issue Jul 18, 2019 · 10 comments
Closed

Feature: verify strict mocking in Mockito sense #334

mikezx6r opened this issue Jul 18, 2019 · 10 comments

Comments

@mikezx6r
Copy link

Expected Behavior

Using Mockito with JUnit Jupiter, adding the MockitoExtension enforces strict mocks. Mockito's definition is a bit different than Mockk's definition.

Mockito's definition of strict is that all defined expectations in a test are executed. This is automatically checked by the extension at the end of the test

Current Behavior

Mockk currently does not automatically report this when the extension is applied. I think attempting to do this manually is also not possible, or a lot of extra work.

I would like the extension to turn this feature on by default, or have Mockk provide an additional extension that can be applied that turns this feature on. JUnit Jupiter supports more than one extension, but I'm not sure if they can interact. I'm also not sure if Extensions can be 'configured' when they're applied, so not sure exactly how it can/should be implemented.

@amatkivskiy
Copy link

@mikezx6r Is it possible to have this behavior on JUnit 4?

@mikezx6r
Copy link
Author

mikezx6r commented Sep 3, 2019

@amatkivskiy I believe there's a Rule for JUnit4 that does the same thing.

@mikezx6r
Copy link
Author

mikezx6r commented Sep 3, 2019

@oleksiyp I've looked into the Mockito Extension code. Basically, a Mock contains a list of Stubs. Each stub object has a 'called' flag.

The Extension then just iterates through all mocks, and their stubs, and reports those that weren't 'called'.

Don't know if that fits into Mockk's approach as I haven't looked at the code yet, but at least it's not very magical.

@oleksiyp
Copy link
Collaborator

oleksiyp commented Sep 5, 2019

Yes, seems possible to add. Although as a property for extension(if possible)

@mikezx6r
Copy link
Author

mikezx6r commented Sep 8, 2019

Ok, I'll take a look at it. One issue I'm running into when I try to load the project into IntelliJ. In order to import, it appears the project requires an Android SDK be available, but as I don't develop Android apps, I don't have this on my machine.

Are you able to let me know the easiest way to configure this so I can add it to the DEVELOPMENT.md, and setup my environment correctly?

@oleksiyp
Copy link
Collaborator

oleksiyp commented Sep 8, 2019

Have you checked https://github.com/mockk/mockk/wiki btw? It has some hints regarding debugging. I think you don't need Android SDK to open in IDEA.
It should autodetect this situation: https://github.com/mockk/mockk/blob/master/settings.gradle#L1
Check this one https://github.com/mockk/mockk/blob/master/gradle/detect-android-sdk.gradle . This cript try to find it in HOME directory. Maybe you have directory $HOME/Android/Sdk present. Otherwise, it is hard to say.

@mikezx6r
Copy link
Author

mikezx6r commented Sep 8, 2019

Forgot about the Wiki, but unfortunately that doesn't help. It's actually a Gradle build issue as I just tried building project from CLI. Here's what I get when I try to build. An issue building the Android agent module. So perhaps having an Android SDK is not optional anymore?

  • What went wrong:
    A problem occurred configuring project ':mockk-agent-android'.

SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

@oleksiyp
Copy link
Collaborator

oleksiyp commented Sep 8, 2019

So. I checked. Yes this "detect-android-sdk.gradle" is to automatically detect path, not to exclude Android modules.

Just comment out everything related to Android from 'settings.xml':

image

@oleksiyp oleksiyp changed the title Provide an additional option for Strict verification to ensure there are no unused expectations defined Feature: verify strict mocking in Mockito sense Nov 1, 2019
@oleksiyp oleksiyp added this to To do in MockK features Nov 2, 2019
@Raibaz
Copy link
Collaborator

Raibaz commented Feb 7, 2021

This should be relatively straightforward to implement as a flag in the MockKExtension

@Raibaz
Copy link
Collaborator

Raibaz commented May 11, 2022

This was fixed in v1.12.4.

@Raibaz Raibaz closed this as completed May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
MockK features
  
To do
Development

No branches or pull requests

4 participants