1

How does one use EasyMock in order to test static functions that cannot be overridden? I have one large test suite class, and I partially mock an object 'A' inside of my test suite. As i mock my object 'A', is there any way to expect these static method calls which take in arguments?

For the sake of the code, classes A and B have to stay in their current position and cannot be rearranged due to outside dependencies. Class 'A' makes a call onto bar() from class 'B'. I need to be able to mock method foo() or method bar(), however they are static and take in arguments.

Class in question:

class A extends B {
    public static void foo(args...) {
        ...
        bar(args...);
    }
}

class B {
    public static void bar(args...) {
        ....
    }
}
void.massive
  • 115
  • 2
  • 6
  • Possible duplicate of [How do I mock static methods in a class with easymock?](https://stackoverflow.com/questions/3162551/how-do-i-mock-static-methods-in-a-class-with-easymock) – O.O.Balance Mar 21 '18 at 13:57

2 Answers2

0

I think that you cannot do it with easymock.

See a similar question here:

How do I mock static methods in a class with easymock?

Piotr
  • 504
  • 4
  • 6
0

Here you go. But reading the PowerMock documentation should have given you the same answer in 5 minutes.

@RunWith(PowerMockRunner.class)
@PrepareForTest({ B.class})
public class MyTest {

  @Test
  public void test() {
    mockStatic(B.class); // Mock static methods on B
    B.bar(4); // Record a static call to B.bar expecting 4 in argument

    replay(B.class); // Go in replay mode

    A.foo(4); // Call foo that will then call bar(4)

    verify(B.class); // Verify that B.bar(4) was indeed called 
  }

}
Henri
  • 5,551
  • 1
  • 22
  • 29