0

How to bind mock of final class in Jukito ?

For example :

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

//Testing class

@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
     }
  }
 }

Is there a way i can use PowerMockito with JukitoRunner ?

sidss
  • 923
  • 1
  • 12
  • 20

1 Answers1

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());
Khaled
  • 644
  • 1
  • 8
  • 14