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

Allow @MockitoSettings to be inherited #1853

Closed
5 tasks done
fabianMendez opened this issue Jan 2, 2020 · 5 comments
Closed
5 tasks done

Allow @MockitoSettings to be inherited #1853

fabianMendez opened this issue Jan 2, 2020 · 5 comments

Comments

@fabianMendez
Copy link
Contributor

  • 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

Strictness actualStrictness = this.retrieveAnnotationFromTestClasses(context)
.map(MockitoSettings::strictness)
.orElse(strictness);

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

@TimvdLippe
Copy link
Contributor

Feel free to send a PR with a fix. Awesome issue description :)

@fabianMendez fabianMendez changed the title Allow @ExtendWith to be inherited Allow @MockitoSettings to be inherited Jan 2, 2020
fabianMendez pushed a commit to fabianMendez/mockito that referenced this issue Jan 2, 2020
fabianMendez pushed a commit to fabianMendez/mockito that referenced this issue Jan 2, 2020
fabianMendez added a commit to fabianMendez/mockito that referenced this issue Jan 2, 2020
fabianMendez added a commit to fabianMendez/mockito that referenced this issue Jan 2, 2020
@ngueno
Copy link

ngueno commented Feb 21, 2020

Hi @TimvdLippe

I am currently using the mockito-junit-jupiter v3.2.4 and I can see that @MockitoSettings still does not have inheritance support:

@ExtendWith(MockitoExtension.class)
@Retention(RUNTIME)
public @interface MockitoSettings {
   ...
}

Do you know when, or, if there is any plans to release a new version of mockito-junit-jupiter containing this improvement?

For now I am including the annotation on each test, but it would be great to refactor and keep it on just my AbstractTest class.

@TimvdLippe
Copy link
Contributor

New version should be pushing to Maven Central as we speak: 0a3064d

@ngueno
Copy link

ngueno commented 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.]]

@TimvdLippe
Copy link
Contributor

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/

epeee pushed a commit to epeee/mockito that referenced this issue Jun 22, 2020
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

3 participants