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

Why the mockito is not compatible with jacoco? #969

Closed
Nymph-fan opened this issue Feb 27, 2017 · 10 comments
Closed

Why the mockito is not compatible with jacoco? #969

Nymph-fan opened this issue Feb 27, 2017 · 10 comments
Labels

Comments

@Nymph-fan
Copy link

Hello:
I'm glad to use mockito ,but when I use jacoco to count the coverage of unit test. The coverage always be 0.Is there any way to solve it?

@Nymph-fan Nymph-fan changed the title Why doos mockito does not compatible with jacoco? Why the mockito is not compatible with jacoco? Feb 27, 2017
@TimvdLippe
Copy link
Contributor

Mockito should be compatible with jacoco. What is the version you are loading and could you create a minimal reproducible gist?

@Nymph-fan
Copy link
Author

Nymph-fan commented Feb 27, 2017

Thanks for reply. In maven configuration I use

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.9.0</version>
    <scope>test</scope>
 </dependency>

the source code is

public HelloWorld() {
}
public String testMethod1() {
    return "Hello World";
}
public int addMethod(int a, int b) {
    return a + b;
}

the test code is

@Test
public void testMethod1(){
    HelloWorld mock=mock(HelloWorld.class);
    when(mock.testMethod1()).thenReturn("Hello World");
    String resutlt=mock.testMethod1();
    Assert.assertEquals(resutlt,"Hello World");
}

then I use jacoco-maven-plugin to Statistical coverage,but the result of coverage is zero


EDITED by @bric3 : reformatted with proper markdown

@TimvdLippe
Copy link
Contributor

Version 1.9.0 is quite old. Could you upgrade to a more recent version, preferably 2.7.12 or if you can not, 1.10.19. Also you are using mockito-all which is deprecated. Please use mockito-core instead.

@bric3
Copy link
Contributor

bric3 commented Feb 27, 2017

@Nymph-fan Note this may depend on the jacoco version as well.

@bric3 bric3 added the question label Feb 27, 2017
@Nymph-fan
Copy link
Author

@bric3 @TimvdLippe I update mockito-core version to 1.10.19, jacoco-maven-plugin to 0.7.7。But the coverage is still 0. So I think jacoco can't count the coverage of unit test through mock frame or method

@bric3
Copy link
Contributor

bric3 commented Mar 1, 2017

@Nymph-fan I cannot say what's wrong, however we don't support mockito 1.x anymore, the mock generation engine was CGLIB and may have issues regarding coverage. Mockito 2.x now uses ByteBuddy instead.

@TimvdLippe
Copy link
Contributor

Since 1.x is not supported anymore and we have not received any issues with Mockito 2.x + Jacoco, I am going to close this issue. If you do encounter any issues with 2.x, please reopen. Thank you for your time!

@HanumanGokavarapu
Copy link

Even with mockito2.x+jacoco we are not getting coverage ,Coudl you please provide sample projecst

@Helias-Criouet
Copy link

For me the issue was in the Jacoco gradle task. The classDirectories needed to point to the ${buildDir}/intermediates/classes folder.

@linusjf
Copy link

linusjf commented Jun 22, 2020

I'm using Mockito 2.0.2-beta and no jacoco coverage is counted via the Maven Surefire plugin.
Same with mockito core 3.3.3.

Update:

Able to do it by using spy() to create a copy of object. That actually invokes the method on the original object.

https://www.baeldung.com/mockito-void-methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants