lundi 19 octobre 2015

Mockito - Accessing private/autowired fields for verify()

So I am new to Mockito for testing facades. So basically I want to check, if a method of Service gets called once.

Here would be a simplified example

My Service

public class Service {
    public int myMethod(int index, int number) {
        if (index<4){
            index = index + number;
        }
        return index;
    }
}

My Facade:

public class Facade {

    private Service service;

    public void method(){
        int i = service.myMethod(4, 2);
    }

}

and finally my Test:

public class FacadeTest {
    @InjectMocks
    private Facade classUnderTest;

    @Mock (name="service")
    private Service service;

    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void test(){
        verify(classUnderTest, times(1)).service.myMethod(4,2);
    }
}

I know it is possible to use Getter/Setter-methods in my Facade to return the Service, but I want to do it, without doing that.

Is it possible, in the way I want to, without doing any change to the Facade?

And is there a difference, when I have a Spring-project and used @Autowired for the service variable inside the Facade?

Thanks!

Aucun commentaire:

Enregistrer un commentaire