Skip to content

Commit

Permalink
Refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
masoudarvishian committed May 13, 2024
1 parent b9aad28 commit 9304a1b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.Duration;
Expand All @@ -21,6 +22,9 @@
import java.time.ZonedDateTime;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.catchThrowable;

@SpringBootTest
public class JobServiceTests {

Expand Down Expand Up @@ -55,9 +59,13 @@ public void start_date_cannot_be_in_the_past() {
LocalDate startDate = LocalDate.now().minusDays(10);
LocalDate endDate = LocalDate.now().plusDays(2);

// when - then
Assertions.assertThrows(InvalidStartDateException.class, () ->
jobService.createJob(UUID.randomUUID(), startDate, endDate));
// When
Throwable throwable = catchThrowable(() ->
jobService.createJob(UUID.randomUUID(), startDate, endDate)
);

// Then
assertThat(throwable).isInstanceOf(InvalidStartDateException.class);
}

@Test
Expand All @@ -66,9 +74,11 @@ public void start_date_should_not_be_equal_to_end_date() {
LocalDate startDate = LocalDate.now();
LocalDate endDate = LocalDate.now();

// when - then
Assertions.assertThrows(IllegalArgumentException.class, () ->
jobService.createJob(UUID.randomUUID(), startDate, endDate));
// when
Throwable throwable = catchThrowable(() -> jobService.createJob(UUID.randomUUID(), startDate, endDate));

// then
assertThat(throwable).isInstanceOf(IllegalArgumentException.class);
}

@Test
Expand All @@ -77,9 +87,11 @@ public void the_end_date_should_be_after_the_start_date() {
LocalDate startDate = LocalDate.now().plusDays(1);
LocalDate endDate = LocalDate.now();

// when - then
Assertions.assertThrows(InvalidEndDateException.class, () ->
jobService.createJob(UUID.randomUUID(), startDate, endDate));
// when
Throwable throwable = catchThrowable(() -> jobService.createJob(UUID.randomUUID(), startDate, endDate));

// then
assertThat(throwable).isInstanceOf(InvalidEndDateException.class);
}

@Test
Expand Down Expand Up @@ -137,8 +149,10 @@ public void a_company_can_only_cancel_its_own_jobs() {
Job job = jobService.createJob(UUID.randomUUID(), startDate, endDate);
UUID companyId = UUID.randomUUID();

// when - then
Assertions.assertThrows(InvalidActionException.class, () ->
jobService.cancelJob(companyId, job.getId()));
// when
Throwable throwable = catchThrowable(() -> jobService.cancelJob(companyId, job.getId()));

// then
assertThat(throwable).isInstanceOf(InvalidActionException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.util.Optional;
import java.util.UUID;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.catchThrowable;

@SpringBootTest
public class ShiftServiceTests {

Expand Down Expand Up @@ -56,9 +58,11 @@ public void a_company_can_only_cancel_its_own_shifts() {
Shift firstShift = job.getShifts().get(0);
UUID companyId = UUID.randomUUID();

// when - then
Assertions.assertThrows(InvalidActionException.class, () ->
shiftService.cancelShift(companyId, firstShift.getId()));
// when
Throwable throwable = catchThrowable(() -> shiftService.cancelShift(companyId, firstShift.getId()));

// then
assertThat(throwable).isInstanceOf(InvalidActionException.class);
}

@Test
Expand Down

0 comments on commit 9304a1b

Please sign in to comment.