Skip to content

Commit f5e6bfc

Browse files
committed
Add J-Unit Tests for Deadline.
Add JUnit tests to test Deadline. Check two methods for correctness, the toString method, and the method for generating the saved String format used for storing the task in the file. There could be instances where there is a typo that results in an incorrect String format. This might lead to bugs when the tasks are being stored in another .txt file and fetched the next time the app is open. J-Unit tests help to automate the testing. Let's add unit test to check for toString method correctness. a unit test to check the getSavedString format correctness and a unit test to check if Deadline throws Exception when no date is able to be parsed from its by String.
1 parent dcc58b4 commit f5e6bfc

5 files changed

Lines changed: 48 additions & 81 deletions

File tree

src/main/java/duke/CommandType.java

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/main/java/duke/Deadline.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* A Task with a deadline to complete.
88
*/
99
public class Deadline extends Task {
10-
protected String by;
10+
private String by;
1111
private LocalDate dateOfDeadline;
1212

1313
public Deadline(String description,String by) {

src/main/java/duke/Ui.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public String getUserCommand(){
2727
return input;
2828
}
2929

30-
30+
3131

3232
/**
3333
* Prints all the tasks stored on the TaskList.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package duke;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.fail;
7+
8+
public class DeadlineTest {
9+
@Test
10+
public void toStringFormat_dateIncluded_success() throws Exception {
11+
Deadline d = new Deadline("Read book", "2020-03-04 2359");
12+
assertEquals( "[D][ ] Read book (by: Mar 4 2020 2359)",d.toString());
13+
}
14+
@Test
15+
public void savedStringFormat_checkFormat_success() throws Exception {
16+
Deadline d = new Deadline("Do Homework", "2021-03-04");
17+
assertEquals("D | 0 | Do Homework | 2021-03-04", d.getSavedStringFormat());
18+
}
19+
@Test
20+
public void toStringFormat_checkFormat_throwsException() {
21+
try {
22+
Deadline d = new Deadline("do Something", "this date");
23+
fail(); // test should not reach this line.
24+
} catch (Exception e) {
25+
assertEquals("Sorry Unable to Parse date for Deadline. "
26+
+ "Did you try to do it in yyyy-mm-dd format?", e.getMessage());
27+
}
28+
}
29+
30+
31+
}

src/test/java/duke/DukeTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package duke;
2+
3+
4+
import org.junit.jupiter.api.DynamicTest;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class DukeTest {
10+
@Test
11+
public void dummyTest() {
12+
assertEquals(2,2);
13+
}
14+
15+
}

0 commit comments

Comments
 (0)