0

如何在 Jukito 中绑定最终课程的模拟?

例如 :

public final class SomeFinalClass(){
     public SomeFinalClass(String someString){
     }
}

//测试类

@Runwith(JukitoRunner.class)
public class TestingClass(){

 @Inject
 private SomeFinalClass someFinalClassMock;

 public static class TestModule extends JukitoModule {
   @Override
    protected void configureTest() {
       // bind(SomeClient.class).in(TestSingleton.class);
    }
    @Provides
    public SomeFinalClass getSomkeFinalClass()  {
    return Mokito.mock(SomeFinalClass.class); //throws error
     }
  }
 }

有没有办法可以将 PowerMockito 与 JukitoRunner 一起使用?

4

1 回答 1

0

You can mock a final class if you're using Mockito 2. From Mockito 2 Wiki:

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());
于 2017-02-24T22:32:07.273 回答