Skip to content

Commit

Permalink
Rework tests to use mock() instead of spy()
Browse files Browse the repository at this point in the history
  • Loading branch information
pehala committed Nov 3, 2020
1 parent 1509460 commit 4e701ba
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 39 deletions.
21 changes: 7 additions & 14 deletions client/src/main/java/org/moparforia/client/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ public class Launcher implements Callable<Void> {
private int port;

@CommandLine.Option(names = {"--lang", "-l"},
description = "Sets language of the game, available values:\n ${COMPLETION-CANDIDATES}")
private Language lang = Language.EN_US;
description = "Sets language of the game, available values:\n ${COMPLETION-CANDIDATES}",
defaultValue = "en_us")
private Language lang;

@CommandLine.Option(names = {"--verbose", "-v"}, description = "Set if you want verbose information")
private static boolean verbose = false;
Expand All @@ -48,19 +49,11 @@ public static boolean isUsingCustomServer() {
return true;//instance.serverBox.isSelected();
}

public static void main(String... args) throws Exception {
public static void main(String... args) {
Launcher launcher = new Launcher();
try {
CommandLine.ParseResult parseResult = new CommandLine(launcher)
.setCaseInsensitiveEnumValuesAllowed(true)
.parseArgs(args);
if (!CommandLine.printHelpIfRequested(parseResult)) {
launcher.call();
}
} catch (CommandLine.ParameterException ex) { // command line arguments could not be parsed
System.err.println(ex.getMessage());
ex.getCommandLine().usage(System.err);
}
new CommandLine(launcher)
.setCaseInsensitiveEnumValuesAllowed(true)
.execute(args);
}

public boolean showSettingDialog(JFrame frame, String server, int port) throws ParseException {
Expand Down
24 changes: 15 additions & 9 deletions client/src/test/java/org/moparforia/client/LauncherCLITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,25 @@ class LauncherCLITest {
private StringWriter stdErr;

@BeforeEach
void setUp() throws ParseException, IOException {
void setUp() throws Exception {
// Mock game
launcher = spy(new Launcher());
lenient().doReturn(mock(Game.class)).when(launcher).launchGame(any(JFrame.class), anyString(), anyInt(), any(), anyBoolean());
launcher = mock(Launcher.class, withSettings()
.lenient()
.withoutAnnotations());

// Mock creating JFrame
lenient().doReturn(mock(JFrame.class)).when(launcher).createFrame();
// Use real methods
doCallRealMethod().when(launcher).call();
doCallRealMethod().when(launcher).setPort(anyInt());
doCallRealMethod().when(launcher).setHostname(anyString());

// Mock settings dialog
lenient().doAnswer((invocaton) -> {
doReturn(mock(JFrame.class)).when(launcher).createFrame();
doAnswer((invocaton) -> {
launcher.setPort(invocaton.getArgument(2));
launcher.setHostname(invocaton.getArgument(1));
return true;
}).when(launcher).showSettingDialog(any(JFrame.class), anyString(), anyInt());


cmd = new CommandLine(launcher).setCaseInsensitiveEnumValuesAllowed(true);

stdOut = new StringWriter();
Expand Down Expand Up @@ -117,10 +121,12 @@ void testOnlyHostname() {
@Test
void testDefaultValues() {
assertEquals(0, cmd.execute());
verify(launcher).launchGame(any(),
verify(launcher).launchGame(
any(),
eq(Launcher.DEFAULT_SERVER),
eq(Launcher.DEFAULT_PORT),
eq(Launcher.Language.EN_US),
eq(false));
eq(false)
);
}
}
30 changes: 18 additions & 12 deletions server/src/main/java/org/moparforia/server/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@
Converter.class
}
)
public class Launcher implements Callable<Void> {
public class Launcher implements Callable<Integer> {

public static final String DEFAULT_HOST = "0.0.0.0";
public static final int DEFAULT_PORT = 4242;

@CommandLine.Option(names = {"--hostname", "-ip"},
description = "Sets server hostname")
private String host = DEFAULT_HOST;

@CommandLine.Option(names = {"--port", "-p"},
description = "Sets server port")
private int port = DEFAULT_PORT;
public static final String DEFAULT_PORT = "4242";

@CommandLine.Option(
names = {"--hostname", "-ip"},
description = "Sets server hostname",
defaultValue = DEFAULT_HOST
)
private String host;

@CommandLine.Option(
names = {"--port", "-p"},
description = "Sets server port",
defaultValue = DEFAULT_PORT
)
private int port;

public static void main(String... args) {
Launcher launcher = new Launcher();
Expand All @@ -35,9 +41,9 @@ public static void main(String... args) {
}

@Override
public Void call() {
public Integer call() {
getServer(host, port).start();
return null;
return 0;
}

public Server getServer(String host, int port) {
Expand Down
14 changes: 10 additions & 4 deletions server/src/test/java/org/moparforia/server/LauncherCLITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.jupiter.MockitoExtension;
import picocli.CommandLine;

Expand All @@ -20,6 +21,7 @@
*/
@ExtendWith(MockitoExtension.class)
class LauncherCLITest {
private static final int DEFAULT_PORT = Integer.parseInt(Launcher.DEFAULT_PORT);
private Launcher launcher;

private CommandLine cmd;
Expand All @@ -29,8 +31,12 @@ class LauncherCLITest {
@BeforeEach
void setUp() {
// Mock Launcher instance
launcher = spy(new Launcher());
lenient().doReturn(mock(Server.class)).when(launcher).getServer(anyString(), anyInt());
launcher = mock(Launcher.class, withSettings()
.lenient()
.withoutAnnotations());

doReturn(mock(Server.class)).when(launcher).getServer(anyString(), anyInt());
when(launcher.call()).thenCallRealMethod();

cmd = new CommandLine(launcher);
cmd.setCaseInsensitiveEnumValuesAllowed(true);
Expand Down Expand Up @@ -77,12 +83,12 @@ void testOnlyPort() {
@Test
void testOnlyHostname() {
assertEquals(0, cmd.execute("-ip", "127.127.127.127"));
verify(launcher).getServer(eq("127.127.127.127"), eq(Launcher.DEFAULT_PORT));
verify(launcher).getServer(eq("127.127.127.127"), eq(DEFAULT_PORT));
}

@Test
void testDefaultValues() {
assertEquals(0, cmd.execute());
verify(launcher).getServer(eq(Launcher.DEFAULT_HOST), eq(Launcher.DEFAULT_PORT));
verify(launcher).getServer(eq(Launcher.DEFAULT_HOST), eq(DEFAULT_PORT));
}
}

0 comments on commit 4e701ba

Please sign in to comment.