Skip to content

Commit

Permalink
A-JUnit: Add JUnit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanshew committed Aug 27, 2024
1 parent 14dc131 commit 53f8d02
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repositories {

dependencies {
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.10.0'
testImplementation 'org.mockito:mockito-core:4.0.0'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.10.0'
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/java/nathanbot/tasks/DeadlineTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

import nathanbot.tasks.Deadline;

public class DeadlineTest {

@Test
public void testToString() {
LocalDateTime dateTime = LocalDateTime.of(2023, 10, 5, 14, 30);
Deadline deadline = new Deadline("Submit assignment", dateTime);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd MMM yyyy hh:mma");
String expected = "[D][ ] Submit assignment (by: " + dateTime.format(dateTimeFormatter) + ")";
assertEquals(expected, deadline.toString());
}
}
20 changes: 20 additions & 0 deletions src/test/java/nathanbot/tasks/EventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

import nathanbot.tasks.Event;

public class EventTest {

@Test
public void testToString() {
LocalDateTime startTime = LocalDateTime.of(2023, 10, 5, 14, 30);
LocalDateTime endTime = LocalDateTime.of(2024, 10, 5, 14, 30);
Event event = new Event("Submit assignment", startTime, endTime);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd MMM yyyy hh:mma");
String expected = "[E][ ] Submit assignment (from: " + startTime.format(dateTimeFormatter) + " to: " + endTime.format(dateTimeFormatter) + ")";
assertEquals(expected, event.toString());
}
}
53 changes: 53 additions & 0 deletions src/test/java/nathanbot/tasks/TaskListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.ArrayList;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import nathanbot.storage.Storage;
import nathanbot.tasks.Task;
import nathanbot.tasks.TaskList;

public class TaskListTest {
// built with Copilot's assistance
private Storage storage;
private TaskList taskList;
private Task task1;
private Task task2;

@BeforeEach
public void setUp() {
storage = mock(Storage.class);
task1 = mock(Task.class);
task2 = mock(Task.class);

when(storage.loadTasksFromFile()).thenReturn(new ArrayList<>(Arrays.asList(task1, task2)));

taskList = new TaskList(storage);
}

@Test
public void testConstructorLoadsTasks() {
assertEquals(2, taskList.listLength());
}

@Test
public void testAddTask() {
Task newTask = mock(Task.class);
taskList.addTask(newTask);

assertEquals(3, taskList.listLength());
}

@Test
public void testToString() {
when(task1.toString()).thenReturn("Task 1");
when(task2.toString()).thenReturn("Task 2");

String expected = "1. Task 1\n2. Task 2\n";
assertEquals(expected, taskList.toString());
}
}
14 changes: 14 additions & 0 deletions src/test/java/nathanbot/tasks/ToDoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

import nathanbot.tasks.ToDo;

public class ToDoTest {

@Test
public void testToString() {
ToDo toDo = new ToDo("Submit assignment");
String expected = "[T][ ] Submit assignment";
assertEquals(expected, toDo.toString());
}
}

0 comments on commit 53f8d02

Please sign in to comment.