8000 Throw a warning when mocking classes that should not be mocked · Issue #1304 · mockk/mockk · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Throw a warning when mocking classes that should not be mocked #1304

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

Closed
Raibaz opened this issue Oct 8, 2024 · 5 comments
Closed

Throw a warning when mocking classes that should not be mocked #1304

Raibaz opened this issue Oct 8, 2024 · 5 comments

Comments

@Raibaz
Copy link
Collaborator
Raibaz commented Oct 8, 2024

There are a few classes that it is generally a bad idea to mock: trying to mock them is usually a code smell indicating that a test is not testing the correct part of the application, and mocking them usually leads to problems.

Some of these are:

  • System
  • Collections (particularly HashMap)
  • Data classes, and in general value objects
  • Classes that deal with I/O, like File or Path (you should be wrapping them in a layer that only deals with I/O rather than mixing it with business logic)

Mockk should at least log a warning when trying to mock one of these classes, but ideally it should also have a configuration flag that disables mocking them altogether and has it throw an exception when a user tries to.

We could maybe even make the list of non-mockable classes configurable.

@RaulErnesto08
Copy link

Hello!
I'm looking to contribute to open-source projects, can I work on this issue? 😄

@Raibaz
Copy link
Collaborator Author
Raibaz commented Oct 8, 2024

Sure, go ahead!

@MannanAhmad
Copy link

Hi @Raibaz , I would like to work on this, can you please assign this to me?

@MahmoudMabrok
Copy link

Hi @Raibaz is it still valid to have next issue open when we will raise exception for File class

#603

@Raibaz
Copy link
Collaborator Author
Raibaz commented Oct 14, 2024

@MahmoudMabrok, no, you're right, this feature is meant exactly to prevent issues like #603 from being opened.

Raibaz added a commit that referenced this issue Feb 11, 2025
…classes

[#1304] feat: Restrict mocking of certain classes and add configuration option
SaintPatrck added a commit to bitwarden/android that ref 9B0D erenced this issue Mar 4, 2025
Update mockk from 1.13.13 to 1.13.17

<details>
<summary>mockk/mockk (io.mockk:mockk)</summary>

### p`v1.13.17`](https://redirect.github.com/mockk/mockk/releases/tag/1.13.17)

[Compare Source](mockk/mockk@1.13.16...1.13.17)

##### What's Changed

-   Fix(Issue #1333): Bug fix for the issue with MockK 1.13.16 Wraps Results Objects Twice by @kpadhiamex in [#1334](mockk/mockk#1334)
-   Fix (issue# 1329) parallel testing for unmockkAll by @kpadhiamex in [#1335](mockk/mockk#1335)
-   [[#1304]](mockk/mockk#1304) feat: Restrict mocking of certain classes and add configuration option by @devtaebong in [#1340](mockk/mockk#1340
-   Update README.md - Clarify that private fields cannot be mocked by @p4ulor in [#1347](mockk/mockk#1347)
-   Added new property "failOnSetBackingFieldException" to fail test if a backing field could not be set by @cgm-aw in [#1349](mockk/mockk#1349)
-   Fix compilation error in constructedWith docs by @TWiStErRob in [#1354](mockk/mockk#1354)

**Full Changelog: **Full Changelog**: mockk/mockk@1.13.16...1.13.17

### [`v1.13.16`](https://redirect.github.com/mockk/mockk/releases/tag/1.13.16)

[Compare Source](https://redirect.github.com/mockk/mockk/compare/1.13.14...1.13.16)

##### What's Changed

-   Fix( Issue [#&#8203;1073](https://redirect.github.com/mockk/mockk/issues/1073)): Bug fix for the issue with mocking value classes with coEvery by [@&#8203;kpadhiamex](https://redirect.github.com/kpadhiamex) in [https://github.com/mockk/mockk/pull/1332](https://redirect.github.com/mockk/mockk/pull/1332)

**Full Changelog**: mockk/mockk@1.13.14...1.13.16

### [`v1.13.14`](https://redirect.github.com/mockk/mockk/releases/tag/1.13.14)

[Compare Source](https://redirect.github.com/mockk/mockk/compare/1.13.13...1.13.14)

##### What's Changed

-   fix(1308): Handle nullable complex and nested value classes by [@&#8203;VasilisDrettas-tomtom](https://redirect.github.com/VasilisDrettas-tomtom) in [https://github.com/mockk/mockk/pull/1314](https://redirect.github.com/mockk/mockk/pull/1314)
-   Fix(Issue no. 1330) for Relaxed Mocking Value When Property is Nested Value Class by [@&#8203;kpadhiamex](https://redirect.github.com/kpadhiamex) in [https://github.com/mockk/mockk/pull/1331](https://redirect.github.com/mockk/mockk/pull/1331)

##### New Contributors

-   [@&#8203;kpadhiamex](https://redirect.github.com/kpadhiamex) made their first contribution in [https://github.com/mockk/mockk/pull/1331](https://redirect.github.com/mockk/mockk/pull/1331)

**Full Changelog**: mockk/mockk@1.13.13...1.13.14

</details>
@Raibaz Raibaz closed this as completed Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0