0

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

vjk
  • 2,163
  • 6
  • 28
  • 42
  • 1
    Has `ThirdPartyClass.InnerClass` been tested? Does it have well behaved results for several values of `no`? Then you can use the actual third party as part of your test harness. However, trying to do unit testing of shims or adapters is tough. In your example, it may actually be too trivial to attempt. – Bob Dalgleish Apr 24 '14 at 20:25
  • possible duplicate of [junit test class for the following code](http://stackoverflow.com/questions/12278593/junit-test-class-for-the-following-code) – Joe Apr 27 '14 at 08:31

1 Answers1

0

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

John B
  • 32,493
  • 6
  • 77
  • 98