BAEL-2512 - Add a new section in Mockito Spy article

This commit is contained in:
Jon Cook 2019-03-03 15:18:14 +01:00
parent ea3fd07d97
commit 9838dca33a
1 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package org.baeldung.mockito.misusing;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.exceptions.misusing.NotAMockException;
import org.mockito.internal.progress.ThreadSafeMockingProgress;
public class MockitoMisusingUnitTest {
@After
public void tearDown() {
ThreadSafeMockingProgress.mockingProgress().reset();
}
@Test
public void givenNotASpy_whenDoReturn_thenThrowNotAMock() {
try {
List<String> list = new ArrayList<String>();
Mockito.doReturn(100, Mockito.withSettings().lenient())
.when(list)
.size();
fail("Should have thrown a NotAMockException because 'list' is not a mock!");
} catch (NotAMockException e) {
assertThat(e.getMessage(), containsString("Argument passed to when() is not a mock!"));
}
}
}