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

And a new lightweight mockito extension #3231

Open
5 tasks done
topizio opened this issue Jan 10, 2024 · 2 comments
Open
5 tasks done

And a new lightweight mockito extension #3231

topizio opened this issue Jan 10, 2024 · 2 comments

Comments

@topizio
Copy link

topizio commented Jan 10, 2024

This issue relates with #1509 for the Mockito Junit 5 extension.

The extension is precious to write maintainable code which automatically creates, injects mocks OOTB and validates that all mocks are correctly used. All of this comes with a price: all of this magic slows down each unit test.

For example, a simple unit test written Junit 5.10.1 with mockito Extension 5.8.0

@ExtendWith(MockitoExtension.class)
class CapabilityFeatureVerifierTest {

    @Mock
    private FeatureToggleService featureToggleService;

    @InjectMocks
    private CapabilityFeatureVerifier capabilityFeatureVerifier;

    @Test
    void shouldReturnTrueForCapabilityWithoutFeatureFlagLabelDefined() {
    }

    @Test
    void shouldReturnTrueForCapabilityWitFeatureFlagLabelAndEnabledFeature() {
    }

    @Test
    void shouldReturnFalseForCapabilityWitFeatureFlagLabelAndDisabledFeature() {
    }
}

played via 17.0.9-zulu on Ubuntu 22.04.3 LTS virtualized over Intel i9-10885H CPU @ 2.40GHz × 12 has this execution time:

image

As it seems there's no way to speed up the existing extension, this issue aims to create a lightweight version with less responsibilities but more speed.

check that

  • The mockito message in the stacktrace have useful information, but it didn't help
  • The problematic code (if that's possible) is copied here;
    Note that some configuration are impossible to mock via Mockito
  • Provide versions (mockito / jdk / os / any other relevant information)
  • Provide a Short, Self Contained, Correct (Compilable), Example of the issue
    (same as any question on stackoverflow.com)
  • Read the contributing guide
@TimvdLippe
Copy link
Contributor

I am still not sure what the intent of this extension is. If the extension does not instantiate mocks, then what would it be used for? I think the reason you are looking for an extension is the checks of strict mocks, but your PR also manages static mocks to close them. Rather than such an extension, can you call the required API instead in a @AfterEach?

@topizio
Copy link
Author

topizio commented Jan 29, 2024

Thanks @TimvdLippe . The idea is to have something similar to the regular extension but much quicker (as per screenshots). Quicker, without paying the price of having developers over-mocking.

The drawback is that annotations are no longer supported, so developers are responsible of creating their mocks.

We'd like to have this approach in each test without the need of define @AfterEach in each test definition

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

2 participants