First, we will create a new MockPublisherRepositoryTests test class in the src/test/java/com/example/bookpub directory at the root of our project with the following content:
import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.reset;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
public class MockPublisherRepositoryTests {
@MockBean
private PublisherRepository repository;
@Before
public void setupPublisherRepositoryMock() {
given(repository.count()).willReturn(5L);
}
@Test
public void publishersExist() {
assertThat(repository.count()).isEqualTo(5L);
}
@After
public void resetPublisherRepositoryMock() {
reset(repository);
}
}
Execute the tests by running ./gradlew clean test and the tests should get passed