mardi 29 décembre 2015

Mockito Spy - partial mocking not working?

My scenario is pretty simple. Trying to use partial mocks, according to last answer on this and the documentation of Mockito itself. My test is:

@Test
public void test() {
    ClassUnderTest realObject = new ClassUnderTest();
    ClassUnderTest spy = spy(realObject);
    when(spy.methodB()).thenThrow(new Exception("Testing"));


    spy.methodA();
}

and the class under test is:

import org.apache.commons.lang3.NotImplementedException;

public class ClassUnderTest {

    int methodB(){
        throw new NotImplementedException("Not implemented");
    }

    public int methodA(){
        methodB();
        return 0;
    }

}

I would expect that my spying object would call the method B raising the "Testing" exception while actually the real method is called throwing the "Not implemented" exception. It behaves like I don't have a partial mock behavior in place

Why is that? What am I missing?

Aucun commentaire:

Enregistrer un commentaire