Ashish
Ashish

Reputation: 47

How to assert in EasyMock if a method is called only once on a Object used in a method of a Mocked Object?

I have a class to test using EasyMock:

public class Application {
    public void doSomething (AnotherObject o) {
        o.getA().perform();
    }   
}

In my JUnit test for Application I need to assert that perform() is called on an object returned by o.getA() and is called only once.

Does AnotherObject passed to doSomething() have to be mocked?

Is there a way to do this using EasyMock?

Upvotes: 0

Views: 1247

Answers (1)

Henri
Henri

Reputation: 5721

You need to mock A and record that behavior. AnotherObject could be a mock or not. You just need getA to return the mock of A.

Then you will have something like:

A a = createMock(A.class);
a.perform(); // this record one and only one call to perform()
replay(a);

AnotherObject another = new AnotherObject();
another.setA(a);

Application app = new Application()
app.doSomething(another);

verify(a); // this makes sure perform was call once instead of not at all

Upvotes: 4

Related Questions