Final class layout with Mockito 2

I am removing Powermock from the project that I am currently working on, so I am trying to rewrite some existing unitary test with only Mockito (mockito-core-2.2.28).

When I run the test, I have the following error:

org.mockito.exceptions.base.MockitoException:

Cannot mock / spy class com.ExternalpackagePath.Externalclass

Mokito cannot scoff / snoop because:

  • final class

I know this question has already been asked ( How to make fun of the final class using mockito , Dummy objects calling the final classes static methods with Mockito ), but I did not find the answer I'm looking for.

Here is an excerpt of my code:

public class MyClassToTest extends TestCase { private MyClass myClass; @Mock private Externalclass ext; // This class is final, I would like to mock it @Override protected void setUp() throws Exception { MockitoAnnotations.initMocks(this); // <<<< The exception is thrown here ext = Mockito.mock(Externalclass.class); } } 

As mentioned in the Mockito documentation ( https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2 , Β§ Commit unmockable), I added the org.mockito.plugins.MockMaker file. This is my project tree:

  • Project
    • CSI
      • com.packagePath.myPackage
        • Myclass
    • test
      • com.packagePath.myPackage
        • myClassToTest
      • resources
        • Mockito extensions
          • org.mockito.plugins.MockMaker

I am also trying to put the resource directory in "src", in a subdirectory called "test", but the result is still the same.

I thought that mocking the finale was possible with Mockito v2. Does anyone have an idea of ​​what's missing here?

Thanks!

+6
source share
5 answers

Well, I found that this is not the case; it may be useful to other people. My project tree is wrong, I put org.mockito.plugins.MockMaker in the "mockito-extension" directory directly in "src". Now this is my tree:

  • Projet
    • CSI
      • com.packagePath.myPackage
        • myClass
      • Mockito extensions
        • org.mockito.plugins.MockMaker
  • test
    • com.packagePath.myPackage
      • myClassToTest
+7
source

It is strange that your solution seems to work.
According to their documentation on Github , he says.

Denial of final classes and methods is an incubation function of choice. It uses a combination of the toolkit and subclass of the Java agent to enable imitation of these types. Since it works differently with our current mechanism, and it has different limitations, and since we want to collect user experience and feedback, this function must be explicitly activated in order to be available; this can be done using the mockito extension mechanism by creating the src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker containing one line:

 mock-maker-inline 

After creating this file, Mockito automatically uses this new engine, and it can do:

  final class FinalClass { final String finalMethod() { return "something"; } } FinalClass concrete = new FinalClass(); FinalClass mock = mock(FinalClass.class); given(mock.finalMethod()).willReturn("not anymore"); assertThat(mock.finalMethod()).isNotEqualTo(concrete.finalMethod()); 

In the following steps, the team will offer a programmatic way to use this feature. We will identify and provide support for all unrelated scenarios. Stay tuned and let us know what you think of this feature!

Now my work structure looks like this.

enter image description here

+13
source

I could not get it to work with the configuration file; however, the Mockito team is so kind and also provides a pre-configured Mockito artifact that does not require configuration in the target project.

So, if you use Gradle and want to test your Kotlin code, just add this depending on the project:

 testCompile 'org.mockito:mockito-inline:2.8.9' testCompile('com.nhaarman:mockito-kotlin:1.5.0') { exclude group: 'org.jetbrains.kotlin' exclude group: 'org.mockito' } 
+7
source

You seem to have had a problem with classpath, just like me.

Your previous setup would also work, but it seems like

 project/test/resources 

was not on your way to classes.

I had the same problem when I tried to run this using IntelliJ. I just put the resource directory as the root of the test resources and it worked perfectly. Praise the gods of Mokito!

+1
source

This solution worked for me: Instead

 testCompile "org.mockito:mockito-android:2.9.0" 

in the gradle file, replace it

 testCompile group: 'org.mockito', name: 'mockito-inline', version: '2.9.0' 

and it will work.

0
source

Source: https://habr.com/ru/post/1012911/


All Articles