question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow @MockitoSettings to be inherited

See original GitHub issue
  • 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

I have a base test class which among other things mocks a “serviceLocator” and a very common class used by many other services:

@ExtendWith(MockitoExtension.class)
public abstract class DomainServiceTests<T> {
	@Mock
	protected ServiceLocator sl;

	@Spy
	protected UserEnvironmentService userEnvironment;

	@BeforeEach
	public void setup() throws Exception {
		Mockito.when(sl.get(UserEnvironmentService.class)).thenReturn(userEnvironment);
	}
}

But whenever any test try to mock the get method of this serviceLocator with a different class I get an UnnecessaryStubbingException:

org.mockito.exceptions.misusing.UnnecessaryStubbingException:
    Unnecessary stubbings detected.
    Clean & maintainable test code requires zero unnecessary code.
    Following stubbings are unnecessary (click to navigate to relevant line of code):
      1. -> at *.DomainServiceTests.setup(DomainServiceTests.java:24)
    Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class.

Then I specify the strictness in the base test class

@MockitoSettings(strictness = Strictness.WARN)
public abstract class DomainServiceTests<T> {

But even though the strictness is not seen by mockito.

I have debugged the code and found that the strictness is retrieved by searching the annotation on the method and the class of the test

https://github.com/mockito/mockito/blob/b2b66f430a5bf890135c0c25872bf9869eb09aec/subprojects/junit-jupiter/src/main/java/org/mockito/junit/jupiter/MockitoExtension.java#L143-L145

This eventually calls

https://github.com/junit-team/junit5/blob/5e41ebe612fd67e905f96f1dd3184a071b65be17/junit-platform-commons/src/main/java/org/junit/platform/commons/util/AnnotationUtils.java#L108-L112

public static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElement element, Class<A> annotationType) {
	Preconditions.notNull(annotationType, "annotationType must not be null");
	boolean inherited = annotationType.isAnnotationPresent(Inherited.class);
	return findAnnotation(element, annotationType, inherited, new HashSet<>());
}

So if you annotate MockitoSettings with java.lang.annotation.Inherited it will work (I turned the inherited variable to true using the debugger to confirm).

For now I will have to annotate every test class with

@MockitoSettings(strictness = Strictness.WARN)

I am using Gradle 7.0 with JUnit 5.5.2

Thanks

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
TimvdLippecommented, Feb 21, 2020

That’s sadly a known issue with Maven Central, but the artifact should be installable for you: https://repo1.maven.org/maven2/org/mockito/mockito-core/3.3.0/

0reactions
nguenocommented, Feb 21, 2020

@TimvdLippe That’s awesome!

Unfortunately Travis is not very happy:

[performRelease] > Could not sync 'mockito/maven/mockito/3.3.0' to Maven Central: HTTP/1.1 400 Bad Request [status:Sync Failed, messages:[Failed to close repository: orgmockito-1399. Server response:
[performRelease]    <nexus-error>
[performRelease]     <errors>
[performRelease]       <error>
[performRelease]         <id>*</id>
[performRelease]         <msg>Unhandled: Repository: orgmockito-1399 has invalid state: closed</msg>
[performRelease]       </error>
[performRelease]     </errors>
[performRelease]   </nexus-error>, Dropping existing partial staging repository.]]
Read more comments on GitHub >

github_iconTop Results From Across the Web

MockitoExtension (junit-jupiter 4.9.0 API) - javadoc.io
This allows you to do setup work in the constructor and set your fields to final . Please refer to JUnit Jupiter's documentation...
Read more >
Mock inherited method in Mockito Java - Stack Overflow
Let's say I have the following: abstract class Parent { public abstract void implementMe(); protected final void doComplexStuff( /* a long parameter list ......
Read more >
Test Doubles in JUnit :: CC 410 Textbook
To create test doubles in JUnit, we'll rely on a separate library called Mockito. Mockito is a framework for creating mock objects in...
Read more >
Companion object MockitoStubs
This class is an implementation of the Answer interface allowing to pass functions as an answer. It does a bit of work for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found