0

The code is posted at link

now when i am trying to write the junit for first case i am getting the error

"need to replay the class B ".

but same junit is working for the second case.

my junit is

@RunWith(PowerMockRunner.class)
public class TestClass {

    @Test
    public void testDoSomeThing() {
        B b = createMock(B.class)
        expectNew(b.CallMe()).andReturns(xxx)
        A a=new A();

        replayAll();
        a.doSomething();
        verifyAll();
    }
}
Community
  • 1
  • 1
Anil Sharma
  • 558
  • 6
  • 18

2 Answers2

1

Here's a solution using EasyMock with PowerMock :

TestClass.java

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ A.class, B.class })
public class TestClass {

    @Test
    public void testDoSomeThing() throws Exception {
        /* Setup */
        B bMock = PowerMock.createMock(B.class);

        /* Mocks */
        PowerMock.expectNew(B.class).andReturn(bMock).atLeastOnce();
        bMock.callMe();

        /* Activate */
        PowerMock.replayAll();

        /* Test */
        A cut = new A();
        cut.doSomething();

        /* Asserts */
        PowerMock.verifyAll();
    }
}

A.java

public class A {

    B b = new B();

    public void doSomething() {
        b.callMe();
    }
}

B.java

public class B {

    public void callMe() {

    }
}
javaPlease42
  • 4,699
  • 7
  • 36
  • 65
0

You forgot to add

@PrepareForTest({A.class, B.class})

This annotation must have the classes you are mocking and the classes that will use these mocks.

cahen
  • 15,807
  • 13
  • 47
  • 78