Skip to content

Commit

Permalink
Merge pull request #478 from alphagov/junit5
Browse files Browse the repository at this point in the history
Convert to JUnit 5
  • Loading branch information
alexbishop1 authored Nov 9, 2020
2 parents 5ae11c1 + 44a602b commit 1be8a35
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 62 deletions.
21 changes: 17 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<hamcrest.version>2.2</hamcrest.version>
<jmh.version>1.26</jmh.version>
<jackson.version>2.11.3</jackson.version>
<mockito.version>3.6.0</mockito.version>
<pay-java-commons.version>1.0.20201103173727</pay-java-commons.version>
</properties>

Expand Down Expand Up @@ -67,6 +68,11 @@
<artifactId>dropwizard-sentry</artifactId>
<version>2.0.12-1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>

<!-- testing -->
<dependency>
Expand All @@ -90,7 +96,7 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.6.0</version>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -122,9 +128,16 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package uk.gov.pay.card.db;

import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.pay.card.db.loader.BinRangeDataLoader;
import uk.gov.pay.card.model.CardType;
import uk.gov.pay.card.model.CardInformation;
Expand All @@ -12,20 +12,20 @@
import java.util.Collections;
import java.util.Optional;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.pay.card.db.loader.BinRangeDataLoader.BinRangeDataLoaderFactory;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class RangeSetCardInformationStoreTest {

private CardInformationStore cardInformationStore;

@After
@AfterEach
public void tearDown() {
cardInformationStore.destroy();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package uk.gov.pay.card.db.loader;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.Test;
import uk.gov.pay.card.db.CardInformationStore;
import uk.gov.pay.card.db.loader.BinRangeDataLoader.BinRangeDataLoaderFactory;
import uk.gov.pay.card.model.CardInformation;
import uk.gov.pay.card.model.PrepaidStatus;

import java.net.URL;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
Expand All @@ -18,9 +17,6 @@

public class BinRangeDataLoaderTest {

@Rule
public final ExpectedException exception = ExpectedException.none();

@Test
public void shouldLoadWorldpayBinRangesFromURL() throws Exception {

Expand Down Expand Up @@ -51,9 +47,8 @@ public void shouldThrowExceptionWhenURLDoesNotExist() throws Exception {
BinRangeDataLoader worldpayBinRangeLoader = BinRangeDataLoaderFactory.worldpay(new URL("file:///nonexistent/path"));

CardInformationStore cardInformationStore = mock(CardInformationStore.class);
exception.expect(BinRangeDataLoader.DataLoaderException.class);

worldpayBinRangeLoader.loadDataTo(cardInformationStore);
assertThrows(BinRangeDataLoader.DataLoaderException.class, () -> worldpayBinRangeLoader.loadDataTo(cardInformationStore));

verifyZeroInteractions(cardInformationStore);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package uk.gov.pay.card.db.loader;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.Test;
import uk.gov.pay.card.model.PrepaidStatus;

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

public class WorldpayPrepaidParserTest {

@Rule
public final ExpectedException exception = ExpectedException.none();
public class WorldpayPrepaidParserTest {

@Test
public void shouldReturnPrepaidForY() {
Expand Down
21 changes: 11 additions & 10 deletions src/test/java/uk/gov/pay/card/it/resources/CardIdResourceITest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package uk.gov.pay.card.it.resources;

import io.dropwizard.testing.junit.DropwizardAppRule;
import io.dropwizard.testing.junit5.DropwizardAppExtension;
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
import io.restassured.response.ValidatableResponse;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import uk.gov.pay.card.app.CardApi;
import uk.gov.pay.card.app.config.CardConfiguration;

Expand All @@ -15,26 +16,26 @@

/**
* @deprecated The usefulness of many of these tests is unclear - move them to pay-cardid-data?
*
* <p>
* These tests used to make assertions about data in pay-cardid-data, but that lives in a private repo.
*
* <p>
* We want to be able to test pay-cardid in a CI system that doesn't have access to private repos,
* so the data needed for the tests to pass has been redacted to reduce its sensitivity and inlined
* in src/test/resources/card-id-resource-integration-test
*
* <p>
* The overall effect is that a lot of these tests are now just checking that "the tests do what the tests do",
* whereas they used to make some (fairly loose) assertions that the bin data in pay-cardid-data was correct.
*
* <p>
* If we care about testing pay-cardid-data we should set up CI for that repo separately in a system that
* has access to private repos and move the tests that use card data there.
*
* <p>
* A test that checks the general success case and the tests for the error conditions without relying on specific
* card details are probably still valid and should stay.
*/
@ExtendWith(DropwizardExtensionsSupport.class)
public class CardIdResourceITest {

@Rule
public final DropwizardAppRule<CardConfiguration> app = new DropwizardAppRule<>(
private static final DropwizardAppExtension<CardConfiguration> app = new DropwizardAppExtension<>(
CardApi.class
, resourceFilePath("config/config.yaml")
, config("server.applicationConnectors[0].port", "0")
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/uk/gov/pay/card/model/CardInformationTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package uk.gov.pay.card.model;

import org.junit.Test;
import org.junit.jupiter.api.Test;

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

public class CardInformationTest {

Expand Down
32 changes: 19 additions & 13 deletions src/test/java/uk/gov/pay/card/model/CardTypeTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package uk.gov.pay.card.model;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.Test;

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

public class CardTypeTest {

@Rule
public final ExpectedException thrown = ExpectedException.none();

@Test
public void shouldFindCardClass() {
CardInformation cardInformation = new CardInformation("A", "C", "A", 1L, 2L);
Expand All @@ -19,15 +15,25 @@ public void shouldFindCardClass() {

@Test
public void shouldThrowNullPointerException_WhenNullValuePassed_forCardClass() {
thrown.expect(java.lang.NullPointerException.class);
thrown.expectMessage("Value cannot be null for paymentGatewayRepresentation");
new CardInformation("A", null, "A", 1L, 2L);
try {
new CardInformation("A", null, "A", 1L, 2L);
fail("The constructor was expected to throw an exception");
} catch (NullPointerException e) {
assertEquals("Value cannot be null for paymentGatewayRepresentation", e.getMessage());
} catch (Throwable e) {
fail("An unexpected exception was thrown by the constructor: " + e);
}
}

@Test
public void shouldThrowIllegalArgumentException_WhenInvalidValuePassed_forCardClass() {
thrown.expect(java.lang.IllegalArgumentException.class);
thrown.expectMessage("No enum found for value [I do not exist]");
new CardInformation("A", "I do not exist", "A", 1L, 2L);
try {
new CardInformation("A", "I do not exist", "A", 1L, 2L);
fail("The constructor was expected to throw an exception");
} catch (IllegalArgumentException e) {
assertEquals("No enum found for value [I do not exist]", e.getMessage());
} catch (Throwable e) {
fail("An unexpected exception was thrown by the constructor: " + e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import com.fasterxml.jackson.databind.ObjectWriter;
import com.jayway.jsonassert.JsonAssert;
import io.dropwizard.setup.Environment;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

import javax.ws.rs.core.Response;
import java.util.SortedMap;
Expand All @@ -22,7 +22,7 @@
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class HealthCheckResourceTest {
@Mock
private Environment environment;
Expand All @@ -32,7 +32,7 @@ public class HealthCheckResourceTest {

private HealthCheckResource resource;

@Before
@BeforeEach
public void setup() {
when(environment.healthChecks()).thenReturn(healthCheckRegistry);
resource = new HealthCheckResource(environment);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/uk/gov/pay/card/service/CardServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package uk.gov.pay.card.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.pay.card.db.CardInformationStore;
import uk.gov.pay.card.model.CardInformation;

Expand All @@ -13,7 +13,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class CardServiceTest {
@Mock
CardInformationStore cardInformationStore;
Expand Down

0 comments on commit 1be8a35

Please sign in to comment.