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

Fixes #2281: Make MockedConstruction stubs close on demand #2442

Merged
merged 5 commits into from Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -21,9 +21,9 @@
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoSession;
import org.mockito.ScopedMock;
import org.mockito.internal.configuration.MockAnnotationProcessor;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.session.MockitoSessionLoggerAdapter;
Expand Down Expand Up @@ -181,7 +181,7 @@ private Optional<MockitoSettings> retrieveAnnotationFromTestClasses(final Extens
@Override
@SuppressWarnings("unchecked")
public void afterEach(ExtensionContext context) {
context.getStore(MOCKITO).remove(MOCKS, Set.class).forEach(mock -> ((MockedStatic<?>) mock).closeOnDemand());
context.getStore(MOCKITO).remove(MOCKS, Set.class).forEach(mock -> ((ScopedMock) mock).closeOnDemand());
context.getStore(MOCKITO).remove(SESSION, MockitoSession.class)
.finishMocking(context.getExecutionException().orElse(null));
}
Expand All @@ -200,7 +200,7 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
parameter.getType(),
parameter::getParameterizedType,
parameter.getName());
if (mock instanceof MockedStatic<?>) {
if (mock instanceof ScopedMock) {
context.getStore(MOCKITO).get(MOCKS, Set.class).add(mock);
}
return mock;
Expand Down
@@ -0,0 +1,37 @@
package org.mockitousage;
temp-droid marked this conversation as resolved.
Show resolved Hide resolved

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* Tests to assert that ScopedMock mocks are properly closed on scope exit.
*/
@ExtendWith(MockitoExtension.class)
class CloseOnDemandTest {
Copy link
Contributor

Choose a reason for hiding this comment

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

Argh, my apologies. Since this only works for the inline mockmaker, the test file needs to be in https://github.com/mockito/mockito/tree/main/subprojects/junitJupiterInlineMockMakerExtensionTest/src/test/java/org/mockitousage

Please run ./gradlew build locally to ensure that the tests pass before you update the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No worries, and thank you for pointing that out, I was getting confused by the error message.

Also the project was building fine locally, which I find suspicious based on the error message I receive on Github.


@Test
void create_mocked_static_1(@Mock MockedStatic<String> mockedStatic) {
assertNotNull(mockedStatic);
}

@Test
void create_mocked_static_2(@Mock MockedStatic<String> mockedStatic) {
assertNotNull(mockedStatic);
}

@Test
void create_mocked_construction_1(@Mock MockedConstruction<String> mockConstruction) {
assertNotNull(mockConstruction);
}

@Test
void create_mocked_construction_2(@Mock MockedConstruction<String> mockConstruction) {
assertNotNull(mockConstruction);
}
}