4

I want to mock final class' constructor. Example code is below. it doesn't work. it throw runtime exception. Please help me how to mock final class' constructor

Thanks.

I know this question quest, it doesn't work. All classes are not final Given question. In my question contains final class.

This is container class

public class FooClass {

    public void someMethod(){
         FinalClass finalClass = new FinalClass("arg1");
         finalClass.printMsg();
    }
}

This is final class:

public final class FinalClass {

    public FinalClass() {
         this("some msg");
    }

    public FinalClass(String arg1){
         throw new RuntimeException("msg");
    }

    public void printMsg(){
         System.out.println("success");
    }

}

This is test class:

@RunWith( PowerMockRunner.class )
@PowerMockRunnerDelegate(MockitoJUnitRunner.class)
@PowerMockIgnore({"javax.management.*", "org.apache.http.conn.ssl.*",       "com.amazonaws.http.conn.ssl.*", "javax.net.ssl.*"})
@PrepareForTest({FinalClass.class})
public class FooTest {

    @InjectMocks
    FooClass fooClass;

    @Before
    public void before() throws MalformedURLException {
         MockitoAnnotations.initMocks(this);
    }

    @Test
    public void finalClass() throws Exception {
        FinalClass finalClassMock = PowerMockito.mock(FinalClass.class);
        PowerMockito.whenNew(FinalClass.class).withAnyArguments().thenReturn(finalClassMock);

        fooClass.someMethod();
    }

}

You said that question is same with my question. But do you try my question, that solution not work because of final class. Please read question carefully.

ramazankul
  • 452
  • 1
  • 4
  • 14
  • Please, examine my question. My question contains final class. – ramazankul Jan 23 '18 at 14:08
  • When you add "@PrepareForTest" annotation for called class into (for FooClass), it takes exception : java.lang.reflect.InvocationTargetException(java.lang.IllegalArgumentException: Cannot subclass final class) – ramazankul Jan 23 '18 at 14:15
  • Your advice use Powermock ( PowerMock.createMock(..)). Is there any way with Powermockito or mockito? Also, it is not example of final class contructor mocking – ramazankul Jan 23 '18 at 14:25
  • Ok I completely misread the question then – Nkosi Jan 23 '18 at 14:28
  • check this one https://www.codeproject.com/articles/806508/using-powermockito-to-mock-final-and-static-method – Nkosi Jan 23 '18 at 14:29
  • I checked out codeproject... but do you think is same with my question. It don't think. My question contains an outer class and mock inner class' contrurctor mock. Please, carefully read my question and help – ramazankul Jan 23 '18 at 14:49
  • Sorry, I don't see any solution of that when a class initialize, that return a mock object of a final class. You say dublicate question, but that question seems similar but actually not. Because, that question doesn't contain final class. Therefore it doesn't work. Please focus on final class mocking and constructor return that mock object. – ramazankul Jan 24 '18 at 06:09

0 Answers0