Skip to content

Instantly share code, notes, and snippets.

@grimreaper
Created May 3, 2020 21:25
Show Gist options
  • Save grimreaper/7bff011fb1c8499a50d36fab54afccb7 to your computer and use it in GitHub Desktop.
Save grimreaper/7bff011fb1c8499a50d36fab54afccb7 to your computer and use it in GitHub Desktop.
package example.strictness;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class CUTJUnitRunnerStrict {
@Mock
private UselessClass a;
@Before
public void setUp() {
when(a.returnItself(1)).thenReturn(2);
}
@Test
public void testOne() {
assertEquals(1, 1);
}
@Test
public void testTwo() {
assertEquals(2, a.returnItself(1));
}
}
package example.strictness;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoSession;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.quality.Strictness;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
public class CUTMockitoSession {
@Mock
private UselessClass a;
private MockitoSession mockito;
@Before
public void setUp() {
mockito = Mockito.mockitoSession()
.initMocks(this)
.strictness(Strictness.STRICT_STUBS)
.startMocking();
when(a.returnItself(1)).thenReturn(2);
}
@Test
public void testOne() {
assertEquals(1, 1);
}
@Test
public void testTwo() {
assertEquals(2, a.returnItself(1));
}
@After
public void tearDown() {
mockito.finishMocking();
}
}
public class CUTTestRule {
@Mock
private UselessClass a;
@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
@Before
public void setUp() {
when(a.returnItself(1)).thenReturn(2);
}
@Test
public void testOne() {
assertEquals(1, 1);
}
@Test
public void testTwo() {
assertEquals(2, a.returnItself(1));
}
}
package example.strictness;
class UselessClass {
public int returnItself(int argument) {
return argument;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment