Mock final class with Mockito 2

前端 未结 7 834
醉酒成梦
醉酒成梦 2020-12-01 12:31

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

When

相关标签:
7条回答
  • 2020-12-01 12:39

    I had the same issue that you described. For me, the solution was to create a file named org.mockito.plugin.MockMaker in /test/java/resources/mockito-extensions/ directory and write the following line: mock-maker-inline.

    So MockMaker is actually the file extension (no txt, properties or any other extension needed).

    0 讨论(0)
  • 2020-12-01 12:41

    If you have multiple modules in project check out if they also have some references to Mockito. For me the problem was deprecated and unnecessary definition in some other small and forgotten library module:

    testCompile 'org.mockito:mockito-all:1.10.19'
    

    Removing this unnecessary declaration solved the problem for me

    0 讨论(0)
  • 2020-12-01 12:43

    You seem to have had a classpath issue, just like I did.

    Your previous setup would have also worked, but it seems like

    project/test/resources
    

    was not in your classpath.

    I had the same issue when I tried to run this with IntelliJ. I simply marked the resources directory as a Test Resources Root and it worked fine. Praise the gods of Mockito!

    0 讨论(0)
  • 2020-12-01 12:45

    Well, I found what's wrong here, it maybe useful for other people. My project tree is wrong, I put the org.mockito.plugins.MockMaker in a directory "mockito-extension" directly in "src". This is my tree now:

    • projet
      • src
        • com.packagePath.myPackage
          • myClass
        • mockito-extensions
          • org.mockito.plugins.MockMaker
    • test
      • com.packagePath.myPackage
        • myClassToTest
    0 讨论(0)
  • 2020-12-01 12:49

    I couldn't get it working with the configuration file either; however, the Mockito team is so kind and also provides a pre-configured Mockito artifact that requires no configuration in the target project.

    As a convenience, the Mockito team provides an artifact where this mock maker is preconfigured. Instead of using the mockito-core artifact, include the mockito-inline artifact in your project. Note that this artifact is likely to be discontinued once mocking of final classes and methods gets integrated into the default mock maker.

    So, if you use Gradle and want to test your Kotlin code, just add this to your project's dependencies:

    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'
    }
    
    0 讨论(0)
  • 2020-12-01 12:51

    Weird that your solution seems to work.
    According to their documentation on Github it says.

    Mocking of final classes and methods is an incubating, opt-in feature. It uses a combination of Java agent instrumentation and subclassing in order to enable mockability of these types. As this works differently to our current mechanism and this one has different limitations and as we want to gather experience and user feedback, this feature had to be explicitly activated to be available ; it can be done via the mockito extension mechanism by creating the file src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker containing a single line:

    mock-maker-inline
    

    After you created this file, Mockito will automatically use this new engine and one 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 subsequent milestones, the team will bring a programmatic way of using this feature. We will identify and provide support for all unmockable scenarios. Stay tuned and please let us know what you think of this feature!

    My working structure now looks like this.

    0 讨论(0)
提交回复
热议问题