Unit Testing

Mockito  spy vs. mock

There is couple important difference between the two.

1. With spy , if you are not careful , you may end up calling the real code , while your intention is to mock it.

Mockito.when(spy.methodToMock()).thenReturn(true);

This will end up calling the real method on spy.   if you want , not to call the real code,  use different syntax.

Mockito.doReturn(true).when(spy).methodToMock();

2.  Usage of assertThat([value], [matcher statement]);


assertThat(x, is(3));
assertThat(x, is(not(4)));
assertThat(responseString, either(containsString("color")).or(containsString("colour")));
assertThat(myList, hasItem("3"));

Comments

Popular posts from this blog

SQL

Analytics

DBeaver