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

MockitoTestNGListener does not close session on exception in before method #88

Open
whitelake-city opened this issue Aug 15, 2022 · 0 comments

Comments

@whitelake-city
Copy link

whitelake-city commented Aug 15, 2022

The MockitoTestNGListener does not close the session of before methods in case an exception is thrown in the before method. All follow-up tests will fail. This is a problem especially in test pipelines when you run 100s of tests. Finding the cause takes longer than necessary. In these scenarios it also appears that getting the class name from ITestResult returns null. This causes an NPE when calling hasMockitoTestNGListener.

I would propose the following solutions in the listener:

protected boolean hasMockitoTestNGListener(ITestResult testResult) {
		return nonNull(testResult.getTestClass())
				&& findAnnotation(testResult, Listeners.class)
				.map(Listeners::value)
				.map(Arrays::stream)
				.orElseGet(Stream::empty)
				.anyMatch(listener -> listener == MockitoTestNGListener.class);
}

Important part is to return true in shouldBeRunAfterInvocation in case an exception was thrown in the before method.

private boolean shouldBeRunAfterInvocation(IInvokedMethod method, ITestResult testResult) {
	return (method.isTestMethod() && hasMockitoTestNGListener(testResult))
		|| (isBeforeMethod(method) && nonNull(testResult.getThrowable()));
}

Hope this makes sense. I actually ran into the situation and applying these changes fixed it for me. With the fix I only see the tests from the same test class failing and not other tests afterwards as well.

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

1 participant