Skip to content

Commit 96fe041

Browse files
committed
Add JUnit tests (Complete A-JUnit)
1 parent e0eb53a commit 96fe041

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

src/main/java/duke/task/EventTask.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public EventTask(String description, String eventDate) {
2626
*
2727
* @return Date of event.
2828
*/
29-
public LocalDate getEventDateDate() throws DateTimeParseException {
29+
public LocalDate getEventDateDate() {
3030
String[] eventDateArr = this.eventDate.split(" ");
3131
LocalDate eventDateDate = LocalDate.parse(eventDateArr[0]);
3232
return eventDateDate;
@@ -37,7 +37,7 @@ public LocalDate getEventDateDate() throws DateTimeParseException {
3737
*
3838
* @return Time of event.
3939
*/
40-
public LocalTime getEventDateTime() throws DateTimeParseException {
40+
public LocalTime getEventDateTime() {
4141
String[] eventDateArr = this.eventDate.split(" ");
4242
LocalTime eventDateTime = LocalTime.parse(eventDateArr[1]);
4343
return eventDateTime;

src/test/duke/ParserTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package duke;
2+
3+
import duke.command.AddCommand;
4+
import duke.command.Command;
5+
import duke.exception.DukeException;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
class ParserTest {
11+
12+
@Test
13+
void parse_validCommand_success() {
14+
assertDoesNotThrow(() -> {
15+
Command command = Parser.parse("todo homework");
16+
});
17+
}
18+
19+
@Test
20+
void parse_invalidCommand_exceptionThrown() {
21+
assertThrows(DukeException.class, () -> {
22+
Command command = Parser.parse("blah");
23+
});
24+
}
25+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package duke.task;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.time.LocalDate;
6+
import java.time.LocalTime;
7+
import java.time.format.DateTimeParseException;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class DeadlineTaskTest {
12+
13+
@Test
14+
void getDeadlineDate_validDate_success() {
15+
DeadlineTask deadlineTask = new DeadlineTask("homework", "2021-01-01 19:00");
16+
LocalDate validDate = LocalDate.parse("2021-01-01");
17+
assertEquals(deadlineTask.getDeadlineDate(), validDate);
18+
}
19+
20+
@Test
21+
void getDeadlineDate_invalidDate_exceptionThrown() {
22+
assertThrows(DateTimeParseException.class, () -> {
23+
DeadlineTask deadlineTask = new DeadlineTask("homework", "01-01-2021 19:00");
24+
deadlineTask.getDeadlineDate();
25+
});
26+
}
27+
28+
@Test
29+
void getDeadlineTime_validTime_success() {
30+
DeadlineTask deadlineTask = new DeadlineTask("homework", "2021-01-01 19:00");
31+
LocalTime validTime = LocalTime.parse("19:00");
32+
assertEquals(deadlineTask.getDeadlineTime(), validTime);
33+
}
34+
35+
@Test
36+
void getDeadlineTime_invalidTime_exceptionThrown() {
37+
assertThrows(DateTimeParseException.class, () -> {
38+
DeadlineTask deadlineTask = new DeadlineTask("homework", "01-01-2021 25:00");
39+
deadlineTask.getDeadlineTime();
40+
});
41+
}
42+
43+
@Test
44+
void toString_validInputs_success() {
45+
assertEquals(new DeadlineTask("homework", "2021-01-01 19:00").toString(),
46+
"[D][ ] homework (by: Jan 1 2021, 7:00PM)");
47+
}
48+
}

src/test/duke/task/TaskTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package duke.task;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class TaskTest {
8+
9+
@Test
10+
void markAsDone_validInput_success() {
11+
Task task = new DeadlineTask("homework", "2021-01-01 19:00");
12+
task.markAsDone();
13+
assertEquals(task.isDone, true);
14+
}
15+
16+
@Test
17+
void getStatusIcon_validInput_success() {
18+
Task task = new DeadlineTask("homework", "2021-01-01 19:00");
19+
assertEquals(task.getStatusIcon(), " ");
20+
}
21+
22+
}

0 commit comments

Comments
 (0)