Mockito cannot mock

1,738 views
Skip to first unread message

Gabor Pinter

unread,
Jul 11, 2014, 3:27:18 AM7/11/14
to moc...@googlegroups.com
Hi,

I used to use Mockito last year, I liked it, but now it just cannot mock anything. Do I miss something?
I am using Eclipse Kepler. Trying to run by Ctrl + F11 on the open test file

(Sorry, but I can't seem to find code formatting... )

Files

src/main/java/   cannotmock/SampleClass.java
src/test/java/   cannotmock/SampleClassTest.java


pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test.mockito</groupId>
  <artifactId>cannotmock</artifactId>
  <version>0.1</version>
  <dependencies>
      <dependency>
          <groupId>org.mockito</groupId>
          <artifactId>mockito-all</artifactId>
          <version>1.9.5</version>
      </dependency>
      <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
      </dependency>
  </dependencies>
</project>



SampleClassTest.java

package cannotmock;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

public class SampleClassTest {
    @Before
    public void setUp() throws Exception {
        Mockito.mock(SampleClass.class);
    }

    @Test
    public void test() {
    }

}

Failure Trace

org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class cannotmock.SampleClass
Mockito can only mock visible & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.



Patroklos Papapetrou

unread,
Jul 11, 2014, 5:29:53 AM7/11/14
to moc...@googlegroups.com
Can you please send also the SampleClass source code?

Patroklos Papapetrou

Linkedin | Twitter | Author Speaker | Blog | Google+ | GitHub |




--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/d/optout.

Tomek Kaczanowski

unread,
Jul 11, 2014, 5:30:32 AM7/11/14
to moc...@googlegroups.com
is your SampleClass visible & non-final?
> --
> You received this message because you are subscribed to the Google Groups
> "mockito" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mockito+u...@googlegroups.com.
> To post to this group, send email to moc...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mockito.
> For more options, visit https://groups.google.com/d/optout.



--
Regards / Pozdrawiam
Tomek Kaczanowski
http://practicalunittesting.com

Russell Bateman

unread,
Jul 11, 2014, 8:59:24 AM7/11/14
to moc...@googlegroups.com
Gabor,

We would need to see the code to SampleClass. However, ...

You don't want to mock the very class, according to the name of your test class, that you're putting under test. Instead, you mock a class or classes that SampleClass code uses that do things you don't want to test specifically and that would be too hard to test (like database or wire access) so you can just concentrate on testing the behavior of SampleClass itself. There's a certain zen to this if you're new to it. Take a look at this tutorial; I think it will become clear:

http://www.vogella.com/tutorials/Mockito/article.html

Cheers,

Russ

Gabor Pinter

unread,
Jul 11, 2014, 2:10:10 PM7/11/14
to moc...@googlegroups.com
Thank you for your help!
Here is the sample class. It's nothing, basically.


SampleClass

package cannotmock;
public class SampleClass {
    public static void main(String[] args) {

Gabor Pinter

unread,
Jul 11, 2014, 2:22:21 PM7/11/14
to moc...@googlegroups.com
Thank you Tomek for your response.
SampleClass is visible (or at least it is in the same maven project as the Mockito test), it is definitely not final.
Also, it is not sealed. Or at least my pom does not ask for it.

How can I check if my class is visible to the Mockito test? The .java and the generated .class files are under the same Maven project as the test is. But I don't know where Mockito is looking for it. Eclpse + maven  complicates the problem.

Gabor Pinter

unread,
Jul 11, 2014, 2:27:57 PM7/11/14
to moc...@googlegroups.com
Hi Russ,

Thank you for your help and the link. I have already read that page (a great one) , and Mockito was working for me for a while. I was testing classes that controlled workflow. It was great.

My problem is that now Mockito does not seem work even with the simplest example. Or I am missing something very obvious.... but with Eclipse + maven so many things can go wrong...

Gabor

Brice Dutheil

unread,
Jul 11, 2014, 2:33:41 PM7/11/14
to moc...@googlegroups.com
Can you show the whole stacktrace, I have no problem here for these classes.
Are you using OSGI ? For example developing eclipse plugins.


-- Brice

Gabor Pinter

unread,
Jul 12, 2014, 5:47:10 AM7/12/14
to moc...@googlegroups.com
Brice, thank you for your time.

I am not using OSGi. I am running things on the command line - leaving out Eclipse.

I am pasting the results of a "mvn test" run in the command line.




[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building cannotmock 0.1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.3:resources (default-resources) @ cannotmock ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ cannotmock ---
[INFO] Compiling 1 source file to /home/kinoko/tmp/MockitoTest/cannotmock/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.3:testResources (default-testResources) @ cannotmock ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ cannotmock ---
[INFO] Compiling 1 source file to /home/kinoko/tmp/MockitoTest/cannotmock/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ cannotmock ---
[INFO] Surefire report directory: /home/kinoko/tmp/MockitoTest/cannotmock/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running cannotmock.SampleClassTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.311 sec <<< FAILURE!

Results :

Tests in error:
  test(cannotmock.SampleClassTest):

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.704s
[INFO] Finished at: Sat Jul 12 18:43:27 JST 2014
[INFO] Final Memory: 13M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.10:test (default-test) on project cannotmock: There are test failures.
[ERROR]
[ERROR] Please refer to /home/kinoko/tmp/MockitoTest/cannotmock/target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

Gabor Pinter

unread,
Jul 12, 2014, 6:09:03 AM7/12/14
to moc...@googlegroups.com
[Solved]

After "rm -Rf  .m2/repository/org/mockito" my mockito test project builds both from command line and from Eclipse.

The problem was that Mockito was signed! For webstart I signed all jars in the repo.

I guess an extra line in the Mockito FAQ would be useful for the occasional programmer like me that says that no mocking if Mockito is signed.
Yes, I know, one does not bundle Mockito with a deployment, so why on Earth would it be signed...  but still, this piece of info can be useful.

***

Still don't quite understand why signed Mockito could mock List.class but not my classes... anyhow, it works now.

Thank you all for your help! It helped me a lot.





Brice Dutheil

unread,
Jul 12, 2014, 10:38:47 AM7/12/14
to moc...@googlegroups.com

Good that you found out the reason why it’s not working.

I don’t think we sign mockito. Maybe you installed through another repo, or another way. I think redhat is doing something like signing jars, so you may endup in this sort of situation.

Also this maven output isn’t really helpful as it doesn’t show the stacktrace ;)
Use instead mvn test -Dsurefire.useFile=false -DtrimStackTrace=false

Cheers,

— Brice

Reply all
Reply to author
Forward
0 new messages