Skip to content

Commit

Permalink
Add JUnit tests for Ui class and some methods in Parser class
Browse files Browse the repository at this point in the history
  • Loading branch information
zognin committed Aug 21, 2021
1 parent e3c595a commit 5f831ab
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/test/java/parser/ParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package parser;

import org.junit.Test;

import java.io.ByteArrayInputStream;

import static org.junit.Assert.assertEquals;

public class ParserTest {
@Test
public void detectExitCommand_exitCommand_true() {
String exitCommand = "bye";
System.setIn(new ByteArrayInputStream(exitCommand.getBytes()));
boolean isExitCommand = new Parser().detectExitCommand(exitCommand);

assertEquals(true, isExitCommand);
System.setIn(System.in);
}

@Test
public void detectExitCommand_nonExitCommand_false() {
String nonExitCommand = "hey";
System.setIn(new ByteArrayInputStream(nonExitCommand.getBytes()));
boolean isExitCommand = new Parser().detectExitCommand(nonExitCommand);

assertEquals(false, isExitCommand);
System.setIn(System.in);
}
}
9 changes: 9 additions & 0 deletions src/test/java/stub/message/MessageStub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package stub.message;

import ui.message.Message;

public class MessageStub extends Message {
public MessageStub() {
super("hello", ":)");
}
}
76 changes: 76 additions & 0 deletions src/test/java/ui/UiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package ui;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import stub.message.MessageStub;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.junit.Assert.assertEquals;

public class UiTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
private final PrintStream originalErr = System.err;

@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}

@After
public void restoreStreams() {
System.setOut(originalOut);
System.setErr(originalErr);
}

@Test
public void testShowWelcome() {
new Ui().showWelcome();

assertEquals(
"\t_________________________________________________\n" +
"\tHello! I'm Duke, what shall we do today? ٩(。•́‿•̀。)۶\n" +
"\t_________________________________________________\n",
outContent.toString()
);
}

@Test
public void testShowGoodbye() {
new Ui().showGoodbye();

assertEquals(
"\t_________________________________________________\n" +
"\tBye, see you again ヾ(=´・∀・`=)\n" +
"\t_________________________________________________\n",
outContent.toString()
);
}

@Test
public void testShowMessage() {
new Ui().showMessage(new MessageStub());

assertEquals("\t_________________________________________________\n" +
"\thello :)\n" +
"\t_________________________________________________\n",
outContent.toString()
);
}

@Test
public void testReadInputMessage() {
String inputMessage = "how are you";
System.setIn(new ByteArrayInputStream(inputMessage.getBytes()));
String inputReadByUi = new Ui().readInputMessage();

assertEquals(inputMessage, inputReadByUi);
System.setIn(System.in);
}
}

0 comments on commit 5f831ab

Please sign in to comment.