antoinemeyer/mock-in-bean

Nested test classes that do not use MockInBean fail

jamfor352 opened this issue · 5 comments

Not every Spring boot integration test will necessarily use a MockBean/MockInBean, however when including the mock-in-bean dependency for the sake of those we do want to use it for, the MockInBeanTestExecutionListener will intercept every test rather than just ones needed to be used. This causes an issue with Nested SpringBootTest tests that do not use any Mocks, as ((LinkedList<FieldState>) applicableTestContext.getAttribute(ORIGINAL_VALUES_ATTRIBUTE_NAME)) will fail as applicableTestContext is null (because we didn't set it up in beforeTestClass). I don't think forcing a test listener is the right solution anyway, it should be able to be disabled on tests if wished...

long term suggestion - make it a test extension (rather than test listener) that implements BeforeEachCallback and AfterEachCallback and is then enabled on a per-test base using @ExtendWith(MockInBeanTestExtension.class), so it doesn't interfere with every test. short term solution - return if applicableTestContext is null

@jamfor352 I am trying to find a way to reproduce but haven't succeeded so far. Could you please provide a quick example ?

oh, I see it failing if your top-level class is @Nested, is that what you are experiencing?
I am not sure why you would want that but yes, I'll push the fix.

If that is the actual issue, I have a fix pending confirmation in v1.5.2.
If that is a different issue, please confirm usecase and let me know, thanks! :)

@antoinemeyer the issue was in 1.5.1 (the PR fixed it) where if you have a test class where no MockInBean is declared, and nested spring test classes, then a NullPointerException is thrown. IE - the fix just makes sure that the test execution listener doesn't throw an exception if part of your codebase has nested tests that don't use the mocks basically

I am unable to reproduce your specific use case but I pushed the fix to handle top level nested test classes.
If your issue still persists, please reopen and provide a test case.
Thanks! :)