Skip to content

Commit

Permalink
[skip ci] add task controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sergi-Jr committed Aug 16, 2024
1 parent a81d880 commit 0071f8a
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/test/java/sergi/example/api/TaskControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package sergi.example.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import sergi.example.task.Task;
import sergi.example.task.dal.TaskRepository;
import utils.InitData;

import java.util.Optional;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@AutoConfigureMockMvc
class TaskControllerTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private InitData init;

@Autowired
private TaskRepository taskRepository;

@Test
@Order(1)
void testStartTracking() throws Exception {
Task task = init.task();
taskRepository.save(task);

var request = put("/tasks/" + task.getId() + "/start-tracking");
mockMvc.perform(request).andExpect(status().isOk());

Optional<Task> opActual = taskRepository.findById(task.getId());
assertThat(opActual).isNotNull();

Task actual = opActual.get();
assertThat(actual.getStartedAt()).isNotNull();
}

@Test
@Order(2)
void testEndTracking() throws Exception {
Task task = init.task();
taskRepository.save(task);

var request = put("/tasks/" + task.getId() + "/end-tracking");
mockMvc.perform(request).andExpect(status().isOk());

Optional<Task> opActual = taskRepository.findById(task.getId());
assertThat(opActual).isNotNull();

Task actual = opActual.get();
assertThat(actual.getFinishedAt()).isNotNull();
}
}

0 comments on commit 0071f8a

Please sign in to comment.