I am trying to write test case for Threads, which is written by someone else.
I don't have liberty of changing the existing code.I have gone through various threads in this forum but nothing seems to be working for this scenario. Please suggest.
My code is like below:
Class X implements Runnable{
Y y;
public X(){}
public X(Y y){
this.y = y;
}
public void run(){
Y.staticVoidMethod(A a, B b, boolean c);
}
}
Then in another class class Main
below line is written:
class Main{
public static void main1(){
Y y=new Y();
new Thread(new X(y)).start();
}
}
My Test class is like below:
class Test {
Thread thread;
X x;
Y y = spy(new Y());
@BeforeMethod(alwaysRun = true)
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
PowerMockito.mockStatic(Y.class);
x= spy(new X(y));
thread = spy(new Thread(x));
whenNew(X.class).withAnyArguments().thenReturn(x);
whenNew(Thread.class).withArguments(x).thenReturn(thread); // I have also tried ......withArguments(X.class).thenReturn(thread)
}
@Test
public void test1(){
Main.main1();
//none of the below is working
verify(thread).start();
verify(x).run();
assertTrue(thread.isAlive());
}
}