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
MockedConstruction not autoclosed #2281
Comments
Is this still an open issue? Potentially interested in helping out here. |
@wisecodecraft Yes feel free to send us a PR with a fix 😄 |
Thanks for looking into this. I just checked and this still happens with updated Versions: Related to that (but maybe separate), would be the missing ability to configure the injected MockedConstruction:
|
Hi @snv , From the javadoc it seems clear Mockito expects you to handle it:
@Override
@SuppressWarnings("unchecked")
public void afterEach(ExtensionContext context) {
context.getStore(MOCKITO).remove(MOCKS, Set.class).forEach(mock -> ((MockedStatic<?>) mock).closeOnDemand());
context.getStore(MOCKITO).remove(SESSION, MockitoSession.class)
.finishMocking(context.getExecutionException().orElse(null));
} |
Hi, i hoped to use it the same way as a MockedStatic instance, letting the test-environment inject me what i need. Somewhat like that: @Test
public void testing_for_something(@Mock MockedConstruction<WhatEver> whatEverConstruction){
// arrange
//add configuration to whatEverConstruction
// act
//call something that instantiates whatEver
// assert
//stuff
} When using this pattern with MockedStatic it does work, but sadly not with MockedConstruction, which has to be used in a try-with-ressources block. @Test
public void testing_for_something(){
// arrange
//configure other stuff
try (MockedConstruction ignored = mockConstruction(
whatEver.class,
(mock, ctx) -> {
//add configuration to mock
}
) {
// act
//call something that instantiates WhatEver
}
// assert
//stuff
} Since https://javadoc.io/static/org.mockito/mockito-core/3.12.4/org/mockito/MockitoAnnotations.html#openMocks-java.lang.Object- returns AutoCloseable i kinda assumed that the instances get closed after each test. I know this is marked as an incubating feature, so i hope this is useful feedback. |
MockedConstruction should behave like MockedStatic when we use @mock on it.
MockedConstruction should behave like MockedStatic when we use @mock on it.
Thanks |
Hi,
i am using jUnit5 5.7.1 and Mockito 3.9.0, still on Java 8, starting the tests from IntelliJ IDEA 2020.3.4
I have two testclasses (marked with
@ExtendWith({MockitoExtension.class})
), which each have a test-method, which mocks the construction of MOCKED_CLASS via@Mock
as parameter.The second test fails though, with
I can prevent that from happening, if i add a call to
ScopedMock#closeOnDemand
on the injectedMockedConstruction<MOCKED_CLASS>
at the end of the first invoced test method.So my conclusion is that the mocked construction was not auto-closed at the end of the method's scope, which is what i expected
@Mock
in combination with@ExtendWith({MockitoExtension.class})
to do.Otherwise
@Mock
would be unusable for this, and i would have to use the try-with-ressouces syntax, to make sure theMockedConstruction
actually gets released, even if the test fails (which is the workaround i will use for now).The text was updated successfully, but these errors were encountered: