Skip to content

Add optional restricted mock system property#1454

Merged
Raibaz merged 2 commits intomockk:masterfrom
nishatoma:add-strict-mocking-system-property
Dec 2, 2025
Merged

Add optional restricted mock system property#1454
Raibaz merged 2 commits intomockk:masterfrom
nishatoma:add-strict-mocking-system-property

Conversation

@nishatoma
Copy link
Contributor

Overview

Hello!

Currently, we can enable restricted mocking via mockk.properties file. One thing that would be really helpful for large projects with multi-modules is the ability to have restricted mocking per class or even per gradle command via terminal. This would make it easier to tackle modules one-by-one, for example:

./gradlew -Pmockk.throwExceptionOnBadMock=true feature:your-module:test

I've added a couple of tests to demonstrate this works with current config as well.

@Raibaz, what are your thoughts on this?

@Raibaz
Copy link
Collaborator

Raibaz commented Dec 1, 2025

Looks good to me.

Would you be to add a mention of this in the docs here?

Comment on lines 17 to 18
throwExceptionOnBadMock = loadThrowExceptionSetting(properties) ||
loadThrowExceptionSystemProperty()
Copy link
Contributor

@TWiStErRob TWiStErRob Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think || is the right combiner here, it should be system ?: setting.

Suppose the properties file contains:
mockk.throwExceptionOnBadMock=true
and you run
gradlew -Pmockk.throwExceptionOnBadMock=false
what would you expect?

I would run that command explicitly because "I just want to see the tests run without this check".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback! I will look into applying this suggestion once I am home!

@nishatoma
Copy link
Contributor Author

Looks good to me.

Would you be to add a mention of this in the docs here?

Thank you for the feedback! I will do that once I get home:)

@nishatoma
Copy link
Contributor Author

@Raibaz @TWiStErRob Added MD and addressed comment, thank you!

@Raibaz Raibaz merged commit 6866dd0 into mockk:master Dec 2, 2025
22 checks passed
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

Successfully merging this pull request may close these issues.

3 participants