Permalink
Browse files

Migrate first mockito test to JUnit Jupiter

  • Loading branch information...
schnatterer committed Apr 1, 2018
1 parent 6197ae9 commit b2a7b189ef0b8d0f83e4e131d402e92239fbeaf5
Showing with 92 additions and 73 deletions.
  1. +80 −72 cli/src/test/java/info/schnatterer/colander/cli/ColanderCliTest.java
  2. +12 −1 pom.xml
@@ -24,21 +24,22 @@
package info.schnatterer.colander.cli;
import info.schnatterer.colander.Colander;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Arrays;
import java.util.HashMap;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class ColanderCliTest {
@ExtendWith(MockitoExtension.class)
class ColanderCliTest {
@Spy
private ColanderCli cli;
@@ -52,84 +53,91 @@
@Mock
private Arguments args;
@Before
public void before() throws Exception {
doReturn(builder).when(cli).createColanderBuilder(any());
when(builder.rinse()).thenReturn(result);
}
@Test
public void execute() throws Exception {
assertEquals("Exit status", ExitStatus.SUCCESS, execute("a/b.ical"));
}
@Test
public void executeHelp() throws Exception {
assertEquals("Exit status", ExitStatus.SUCCESS, execute("--help"));
verifyZeroInteractions(builder, result);
}
@Test
public void executeErrorArgs() throws Exception {
assertEquals("Exit status", ExitStatus.ERROR_ARGS, execute("--wtf"));
}
@Test
public void executeErrorParsing() throws Exception {
when(builder.rinse()).thenThrow(new RuntimeException("Mocked exception"));
assertEquals("Exit status", ExitStatus.ERROR_PARSING, execute("a/b.ical"));
}
@Test
public void startColander() throws Exception {
String expectedInput = "in";
String expectedOutput = "out";
when(args.getInputFile()).thenReturn(expectedInput);
when(args.getOutputFile()).thenReturn(expectedOutput);
when(args.isRemoveDuplicateEvents()).thenReturn(true);
when(args.isRemoveEmptyEvents()).thenReturn(true);
when(args.getRemoveSummaryContains()).thenReturn(Arrays.asList("a", "b"));
when(args.getRemoveDescriptionContains()).thenReturn(Arrays.asList("y", "z"));
when(args.getReplaceInSummary()).thenReturn(new HashMap<String, String>() {{
@Nested
class PositiveTests {
@BeforeEach
void before() throws Exception {
doReturn(builder).when(cli).createColanderBuilder(any());
when(builder.rinse()).thenReturn(result);
}
@Test
void positiveExecute() throws Exception {
assertEquals("Exit status", ExitStatus.SUCCESS, execute("a/b.ical"));
}
@Test
void executeErrorParsing() throws Exception {
when(builder.rinse()).thenThrow(new RuntimeException("Mocked exception"));
assertEquals("Exit status", ExitStatus.ERROR_PARSING, execute("a/b.ical"));
}
@Test
void startColander() throws Exception {
String expectedInput = "in";
String expectedOutput = "out";
when(args.getInputFile()).thenReturn(expectedInput);
when(args.getOutputFile()).thenReturn(expectedOutput);
when(args.isRemoveDuplicateEvents()).thenReturn(true);
when(args.isRemoveEmptyEvents()).thenReturn(true);
when(args.getRemoveSummaryContains()).thenReturn(Arrays.asList("a", "b"));
when(args.getRemoveDescriptionContains()).thenReturn(Arrays.asList("y", "z"));
when(args.getReplaceInSummary()).thenReturn(new HashMap<String, String>() {{
put("a", "b");
put("c", "d");
}});
when(args.getReplaceInDescription()).thenReturn(new HashMap<String, String>() {{
when(args.getReplaceInDescription()).thenReturn(new HashMap<String, String>() {{
put("1", "2");
put("3", "4");
}});
assertEquals("Exit status", ExitStatus.SUCCESS, cli.startColander(args));
verify(cli).createColanderBuilder(expectedInput);
verify(builder).removeDuplicateEvents();
verify(builder).removeEmptyEvents();
verify(builder).replaceInSummary("a", "b");
verify(builder).replaceInSummary("c", "d");
verify(builder).replaceInDescription("1", "2");
verify(builder).replaceInDescription("3", "4");
verify(builder).removeSummaryContains("a");
verify(builder).removeSummaryContains("b");
verify(builder).removeDescriptionContains("y");
verify(builder).removeDescriptionContains("z");
verify(result).toFile(expectedOutput);
assertEquals("Exit status", ExitStatus.SUCCESS, cli.startColander(args));
verify(cli).createColanderBuilder(expectedInput);
verify(builder).removeDuplicateEvents();
verify(builder).removeEmptyEvents();
verify(builder).replaceInSummary("a", "b");
verify(builder).replaceInSummary("c", "d");
verify(builder).replaceInDescription("1", "2");
verify(builder).replaceInDescription("3", "4");
verify(builder).removeSummaryContains("a");
verify(builder).removeSummaryContains("b");
verify(builder).removeDescriptionContains("y");
verify(builder).removeDescriptionContains("z");
verify(result).toFile(expectedOutput);
}
@Test
void startColanderEmptyArgs() throws Exception {
assertEquals("Exit status", ExitStatus.SUCCESS, cli.startColander(args));
verify(cli).createColanderBuilder(null);
verify(builder, never()).removeDuplicateEvents();
verify(builder, never()).removeEmptyEvents();
verify(builder, never()).replaceInSummary(anyString(), anyString());
verify(builder, never()).removeSummaryContains(anyString());
verify(builder, never()).removeDescriptionContains(anyString());
verify(result).toFile(null);
}
}
@Test
public void startColanderEmptyArgs() throws Exception {
assertEquals("Exit status", ExitStatus.SUCCESS, cli.startColander(args));
verify(cli).createColanderBuilder(null);
verify(builder, never()).removeDuplicateEvents();
verify(builder, never()).removeEmptyEvents();
verify(builder, never()).replaceInSummary(anyString(), anyString());
verify(builder, never()).removeSummaryContains(anyString());
verify(builder, never()).removeDescriptionContains(anyString());
verify(result).toFile(null);
@Nested
class NoOpTests {
@Test
void executeHelp() throws Exception {
assertEquals("Exit status", ExitStatus.SUCCESS, execute("--help"));
verifyZeroInteractions(builder, result);
}
@Test
void executeErrorArgs() throws Exception {
assertEquals("Exit status", ExitStatus.ERROR_ARGS, execute("--wtf"));
}
}
private ExitStatus execute(String... args) {
return cli.execute(args);
}
}
View
13 pom.xml
@@ -47,6 +47,7 @@
<logback.version>1.1.8</logback.version>
<jacoco.version>0.7.8</jacoco.version>
<junit5.version>5.1.0</junit5.version>
<mockito.version>2.17.0</mockito.version>
</properties>
<modules>
@@ -85,7 +86,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.6.2</version>
<version>${mockito.version}</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
@@ -99,6 +100,11 @@
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
@@ -127,6 +133,11 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
</dependency>
</dependencies>
<build>

0 comments on commit b2a7b18

Please sign in to comment.