Question

I have a method that I have to write the junit for

public String getSomething(String no)
  {
     ThirdPartyClass.Innerclass innerclass = new ThirdPartyClass.Innerclass(..);
     String result =  innerClass.getSomething(no);
  }

I know how to set the private fields in a class using the Whitebox. How can i mock the ThirdPartyClass and ThirdPartyClass.Innerclass

Was it helpful?

Solution

Two options here.

First and best is to not call new in the method. Pass in an InnerClass factory instance to the constructor of the class under test. Then mock this factory.

Second, use a mocking framework like Powermock which can intercept and mock constructor calls.

Mock Constructor

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top