8000 Add support for meta annotations · Issue #1384 · mockk/mockk · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for meta annotations #1384

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

Open
smilasek opened this issue Apr 28, 2025 · 3 comments
Open

Add support for meta annotations #1384

smilasek opened this issue Apr 28, 2025 · 3 comments

Comments

@smilasek
Copy link

Hey,

In couple of our projects we were trying to use mockk in a following way:

@Retention(RUNTIME)
@Target(TYPE)
@CheckUnnecessaryStub
@ExtendWith(MockKExtension.class)
public @interface MockkTest {
}

Our goal is to be able to quickly annotate all of our tests by simply @MockkTest, which would configure some default behaviour like checking for unnecessary stubs and eventually more. This way we could end up with multiple annotations like @MockWithStubsCheck, @MockWithVerificationEnabled` etc

Unfortunately, after some testing, I have found out that MockkExtension is checking for the presence of an annotation on a class or eventually looking for a configured property. Snippet below:

    private val ExtensionContext.checkUnnecessaryStub: Boolean
        get() = testClass.checkUnnecessaryStub ||
                getConfigurationParameter(CHECK_UNNECESSARY_STUB_PROPERTY).map { it.toBoolean() }.orElse(false)

    private val Optional<out AnnotatedElement>.checkUnnecessaryStub
        get() = map { it.getAnnotation(CheckUnnecessaryStub::class.java) != null }
            .orElse(false)

So, in a nutshell, it does not have any support for meta annotations. Is there any big deal for you to add that?

Currently, we are trying to mitigate it by adding the following snippet to build.gradle:

  tasks.withType(Test).configureEach {
    systemProperty "mockk.junit.extension.checkUnnecessaryStub", "true"
  }

but its not scalable and we would like to eventually have that annotation as a part of some internal lib so we could reuse it in various places.

Thanks for the feedback

@Raibaz
Copy link
Collaborator
Raibaz commented Apr 28, 2025

I have nothing against it: in fact, it seems like a good feature addition to me 👍

I'd gladly review a PR about it and the implementation doesn't even seem to complex, so I'll mark this as a good first issue.

@VitalyVPinchuk
Copy link
Contributor

Hi, can I try to implement it?
@Raibaz

@Raibaz
Copy link
Collaborator
Raibaz commented May 11, 2025

Sure, go ahead!

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

3 participants
0