lundi 10 avril 2017

EasyMock - verify void method call

I have the following classes:

public class A {
    private Field field;
    public A(Field field){
        this.field = field;
    }
    public add(int size){
        field.addBall(new Ball(size));
    }
}

public interface Things {
    List<Ball> ballList = new LinkedList<>();
    public addBall(Ball b){
        ballList.add(b);
    }
}

And I want to test add() method of A class. To be more specific I want to test if addBall() method of class Things is called. This test fails saying: Expectation failure on verify : Things.addBall(...) : expected: 1 ,actual: 0;

public class TestA {
        private Things thing;
        private A a;
        @Before
        public void setUp() {
            thing = EasyMock.createNiceMock(Things.class);
            a = new A(thing);
        }

        @After
        public void tearDown() {
        }
    @Test
        public void addTest(){
            thing.addBall(new Ball(345));
            EasyMock.expectLastCall();
            EasyMock.replay(cache);
            a.add(345);
        EasyMock.verify(cache);
        }
}

What is the right approach? What is wrong whit this test?

Aucun commentaire:

Enregistrer un commentaire