Skip to content

Commit

Permalink
Migrate to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
matchilling committed Jun 20, 2024
1 parent 57117ff commit 92d5186
Show file tree
Hide file tree
Showing 11 changed files with 606 additions and 659 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.chucknorris.integration_test.endpoint.joke;

import static io.restassured.RestAssured.given;

import io.chucknorris.integration_test.endpoint.AbstractEndpointTest;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;

class JokeByIdTest extends AbstractEndpointTest {

private final String jokeId = "c9h1endqtmm31b1auddvdg";
Expand All @@ -30,12 +30,13 @@ void shouldReturnJokeAsJson() {
.assertThat()
.header("Content-Type", Matchers.equalTo("application/json"))
.body("categories", Matchers.emptyIterable())
.body("created_at", Matchers.comparesEqualTo("2024-06-19 09:28:25.390787"))
.body("created_at", Matchers.isA(String.class))
.body("icon_url", Matchers.comparesEqualTo("https://api.chucknorris.io/img/avatar/chuck-norris.png"))
.body("id", Matchers.comparesEqualTo(jokeId))
.body("updated_at", Matchers.comparesEqualTo("2024-06-19 09:28:25.390787"))
.body("updated_at", Matchers.isA(String.class))
.body("url", Matchers.comparesEqualTo("https://api.chucknorris.io/jokes/c9h1endqtmm31b1auddvdg"))
.body("value", Matchers.comparesEqualTo("Chuck Norris once lost the remote, but maintained control of the TV by yelling at it in between bites of his \"Filet of Child\" sandwich."));
.body("value", Matchers.comparesEqualTo(
"Chuck Norris once lost the remote, but maintained control of the TV by yelling at it in between bites of his \"Filet of Child\" sandwich."));
}

@DisplayName("Should return joke by id as plain text")
Expand Down
3 changes: 2 additions & 1 deletion chucknorris-web/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ dependencies {
implementation "com.google.guava:guava:33.2.1-jre"

testImplementation "org.springframework.boot:spring-boot-starter-test"
testImplementation "junit:junit:4.12"
testImplementation "org.junit.jupiter:junit-jupiter-api:${Version.JUNIT}"
testImplementation "org.junit.jupiter:junit-jupiter:${Version.JUNIT}"
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package io.chucknorris.api.configuration;

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

import org.junit.Test;

public class HerokuDataSourcePropertiesTest {
class HerokuDataSourcePropertiesTest {

@Test
public void testShouldCreateDataSourcePostgres() {
void testShouldCreateDataSourcePostgres() {
// given:
String driverClassName = "org.postgresql.Driver";

Expand All @@ -18,11 +17,9 @@ public void testShouldCreateDataSourcePostgres() {
HerokuDataSourceProperties actual = new HerokuDataSourceProperties(driverClassName, uri);

// then:
assertEquals("org.postgresql.Driver", actual.getDriverClassName());
assertEquals("password", actual.getPassword());
assertEquals(
"jdbc:postgresql://ec2-00-000-00-000.eu-west-1.compute.amazonaws.com:5432/d409qm4ujtafvk",
actual.getUrl());
assertEquals("username", actual.getUsername());
Assertions.assertEquals("org.postgresql.Driver", actual.getDriverClassName());
Assertions.assertEquals("password", actual.getPassword());
Assertions.assertEquals("jdbc:postgresql://ec2-00-000-00-000.eu-west-1.compute.amazonaws.com:5432/d409qm4ujtafvk", actual.getUrl());
Assertions.assertEquals("username", actual.getUsername());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package io.chucknorris.api.feed;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import com.google.common.util.concurrent.AtomicDouble;
import io.chucknorris.api.feed.dailychuck.DailyChuck;
Expand All @@ -19,46 +22,33 @@
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.test.util.ReflectionTestUtils;

@RunWith(MockitoJUnitRunner.class)
public class FeedControllerTest {

private DailyChuck dailyChuck;

private DailyChuckIssue dailyChuckIssue;

@Mock
private DailyChuckService dailyChuckService;

@Mock
private DateUtil dateUtil;

private DailyChuckService dailyChuckService = Mockito.mock(DailyChuckService.class);
private DateUtil dateUtil = Mockito.mock(DateUtil.class);
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

@Mock
private EventService eventService;

@InjectMocks
private FeedController feedController;

@Mock
private JokeRepository jokeRepository;

@Mock
private MailchimpService mailchimpService;

private EventService eventService = Mockito.mock(EventService.class);
private JokeRepository jokeRepository = Mockito.mock(JokeRepository.class);
private MailchimpService mailchimpService = Mockito.mock(MailchimpService.class);
private MailingListStatistic mailingListStatistic;

private FeedController feedController = new FeedController(
dailyChuckService,
dateUtil,
eventService,
mailchimpService);

private String mailingListId;

@Before
@BeforeEach
public void setUp() throws ParseException {
dailyChuckIssue = new DailyChuckIssue();
dailyChuckIssue.setDate(dateFormat.parse("2019-01-01"));
Expand All @@ -83,23 +73,23 @@ public void setUp() throws ParseException {
when(mailchimpService.fetchListStats(mailingListId)).thenReturn(mailingListStatistic);
}

@Test
public void testDailyChuckJsonReturnsDailyChuckWithoutComposingANewIssueIfItHasAlreadyBeenIssued()
throws IOException, ParseException {
when(dailyChuckService.getDailyChuck()).thenReturn(dailyChuck);
when(dateUtil.now()).thenReturn(dateFormat.parse("2019-01-01"));
@Test
public void testDailyChuckJsonReturnsDailyChuckWithoutComposingANewIssueIfItHasAlreadyBeenIssued()
throws IOException, ParseException {
when(dailyChuckService.getDailyChuck()).thenReturn(dailyChuck);
when(dateUtil.now()).thenReturn(dateFormat.parse("2019-01-01"));

assertEquals(dailyChuck, feedController.dailyChuckJson());
Assertions.assertEquals(dailyChuck, feedController.dailyChuckJson());

verify(dailyChuckService, times(1)).getDailyChuck();
verifyNoMoreInteractions(dailyChuckService);
verify(dailyChuckService, times(1)).getDailyChuck();
verifyNoMoreInteractions(dailyChuckService);

verify(dateUtil, times(1)).now();
verifyNoMoreInteractions(dateUtil);
verify(dateUtil, times(1)).now();
verifyNoMoreInteractions(dateUtil);

verify(eventService, times(0)).publishEvent(any());
verifyNoMoreInteractions(eventService);
}
verify(eventService, times(0)).publishEvent(any());
verifyNoMoreInteractions(eventService);
}

@Test
public void testDailyChuckJsonReturnsDailyChuckWithComposingANewIssue()
Expand All @@ -110,7 +100,7 @@ public void testDailyChuckJsonReturnsDailyChuckWithComposingANewIssue()
when(dateUtil.now()).thenReturn(dateFormat.parse("2019-01-02"));
when(dailyChuckService.composeDailyChuckIssue(any())).thenReturn(newDailyChuckIssue);

assertEquals(dailyChuck, feedController.dailyChuckJson());
Assertions.assertEquals(dailyChuck, feedController.dailyChuckJson());

verify(dailyChuckService, times(1)).getDailyChuck();
verify(dailyChuckService, times(1)).composeDailyChuckIssue(any());
Expand All @@ -133,7 +123,7 @@ public void testDailyChuckRssReturnsDailyChuckWithoutComposingANewIssueIfItHasAl
when(dateUtil.now()).thenReturn(dateFormat.parse("2019-01-01"));
when(dailyChuckService.toRss(dailyChuck)).thenReturn(dailyChuckRss);

assertEquals(dailyChuckRss, feedController.dailyChuckRss());
Assertions.assertEquals(dailyChuckRss, feedController.dailyChuckRss());

verify(dailyChuckService, times(1)).getDailyChuck();
verify(dailyChuckService, times(1)).toRss(dailyChuck);
Expand All @@ -159,7 +149,7 @@ public void testDailyChuckRssReturnsDailyChuckWithComposingANewIssue()
when(dailyChuckService.composeDailyChuckIssue(any())).thenReturn(newDailyChuckIssue);
when(dailyChuckService.toRss(dailyChuck)).thenReturn(dailyChuckRss);

assertEquals(dailyChuckRss, feedController.dailyChuckRss());
Assertions.assertEquals(dailyChuckRss, feedController.dailyChuckRss());

verify(dailyChuckService, times(1)).getDailyChuck();
verify(dailyChuckService, times(1)).composeDailyChuckIssue(any());
Expand All @@ -180,7 +170,7 @@ public void testDailyChuckRssReturnsDailyChuckWithComposingANewIssue()
@Test
public void testDailyChuckStatsReturnsStats() {
MailingListStatistic response = feedController.dailyChuckStats();
assertEquals(response, mailingListStatistic);
Assertions.assertEquals(response, mailingListStatistic);

verify(mailchimpService, times(1)).fetchListStats(mailingListId);
verifyNoMoreInteractions(mailchimpService);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package io.chucknorris.api.feed.dailychuck;

import static org.junit.Assert.assertEquals;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class DailyChuckTest {

private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
private DailyChuck dailyChuck;
private DailyChuckIssue dailyChuckIssue;

@Before
@BeforeEach
public void setUp() throws ParseException {
dailyChuckIssue = new DailyChuckIssue();
dailyChuckIssue.setDate(dateFormat.parse("2019-01-01"));
Expand All @@ -26,22 +25,23 @@ public void setUp() throws ParseException {
}

@Test
public void testFindIssueByJokeIdReturnsNullDoesNotExist() {
assertEquals(dailyChuck.findIssueByJokeId("does-not-exist"), null);
void testFindIssueByJokeIdReturnsNullDoesNotExist() {
Assertions.assertNull(dailyChuck.findIssueByJokeId("does-not-exist"));
}

@Test
public void testFindIssueByJokeIdReturnsDailyChuckIssueIfDoesExist() {
assertEquals(dailyChuck.findIssueByJokeId("c5k7tulvqjs76evwb3brfg"), dailyChuckIssue);
void testFindIssueByJokeIdReturnsDailyChuckIssueIfDoesExist() {
Assertions.assertEquals(dailyChuck.findIssueByJokeId("c5k7tulvqjs76evwb3brfg"), dailyChuckIssue);
}

@Test
public void testFindIssueByDateReturnsNullDoesNotExist() throws ParseException {
assertEquals(dailyChuck.findIssueByDate(dateFormat.parse("2019-01-02")), null);
void testFindIssueByDateReturnsNullDoesNotExist() throws ParseException {
Assertions.assertNull(dailyChuck.findIssueByDate(dateFormat.parse("2019-01-02")));
}

@Test
public void testFindIssueByDateDailyChuckIssueIfDoesExist() throws ParseException {
assertEquals(dailyChuck.findIssueByDate(dateFormat.parse("2019-01-01")), dailyChuckIssue);
void testFindIssueByDateDailyChuckIssueIfDoesExist() throws ParseException {
Assertions.assertEquals(
dailyChuck.findIssueByDate(dateFormat.parse("2019-01-01")), dailyChuckIssue);
}
}
Loading

0 comments on commit 92d5186

Please sign in to comment.