Migrated tests to JUnit 5 and removed PowerMock dependency

As a replacement for the static mock of the URL class, the tests have
been rewritten using ByteBuddy to redefine the calling method.
This commit is contained in:
Stefan Kalscheuer 2018-08-03 17:52:44 +02:00
parent 0d0859bd3a
commit c9444bbc97
5 changed files with 146 additions and 152 deletions

35
pom.xml
View File

@ -58,9 +58,9 @@
<version>2.9.6</version> <version>2.9.6</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit</artifactId> <artifactId>junit-jupiter-engine</artifactId>
<version>4.12</version> <version>5.2.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
@ -70,27 +70,15 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.powermock</groupId> <groupId>net.bytebuddy</groupId>
<artifactId>powermock-module-junit4</artifactId> <artifactId>byte-buddy</artifactId>
<version>2.0.0-beta.5</version> <version>1.8.15</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.powermock</groupId> <groupId>net.bytebuddy</groupId>
<artifactId>powermock-api-mockito2</artifactId> <artifactId>byte-buddy-agent</artifactId>
<version>2.0.0-beta.5</version> <version>1.8.15</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.21.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
@ -118,6 +106,11 @@
</archive> </archive>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins> </plugins>
</build> </build>
<profiles> <profiles>

View File

@ -307,9 +307,18 @@ public class UraClient implements Serializable {
urlStr += "&" + PAR_CIRCLE + "=" + String.join(",", query.circle); urlStr += "&" + PAR_CIRCLE + "=" + String.join(",", query.circle);
} }
URL url = new URL(urlStr); return request(urlStr);
}
return url.openStream(); /**
* Open given URL as InputStream.
*
* @param url The URL.
* @return Input Stream of results.
* @throws IOException Error opening conneciton or reading data.
*/
private InputStream request(String url) throws IOException {
return new URL(url).openStream();
} }
/** /**

View File

@ -18,22 +18,23 @@ package de.stklcode.pubtrans.ura;
import de.stklcode.pubtrans.ura.model.Stop; import de.stklcode.pubtrans.ura.model.Stop;
import de.stklcode.pubtrans.ura.model.Trip; import de.stklcode.pubtrans.ura.model.Trip;
import org.junit.Test; import net.bytebuddy.ByteBuddy;
import org.junit.runner.RunWith; import net.bytebuddy.agent.ByteBuddyAgent;
import org.powermock.api.mockito.PowerMockito; import net.bytebuddy.dynamic.loading.ClassReloadingStrategy;
import org.powermock.core.classloader.annotations.PrepareForTest; import org.junit.jupiter.api.Test;
import org.powermock.modules.junit4.PowerMockRunner; import org.junit.jupiter.api.BeforeAll;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import static net.bytebuddy.implementation.MethodDelegation.to;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
/** /**
@ -43,83 +44,95 @@ import static org.hamcrest.core.Is.is;
* *
* @author Stefan Kalscheuer * @author Stefan Kalscheuer
*/ */
@RunWith(PowerMockRunner.class)
@PrepareForTest({ UraClient.class, URL.class })
public class UraClientTest { public class UraClientTest {
@Test // Mocked resource URL and exception message.
public void getStopsTest() throws Exception { private static String mockResource = null;
/* Mock the HTTP call */ private static String mockException = null;
URL mockURL = PowerMockito.mock(URL.class);
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V2_stops.txt"));
/* List stops and verify some values */ @BeforeAll
public static void initByteBuddy() {
// Install ByteBuddy Agent.
ByteBuddyAgent.install();
new ByteBuddy().redefine(UraClient.class)
.method(named("request"))
.intercept(to(UraClientTest.class))
.make()
.load(UraClient.class.getClassLoader(), ClassReloadingStrategy.fromInstalledAgent());
}
@Test
public void getStopsTest() {
// Mock the HTTP call.
mockHttpToFile("instant_V2_stops.txt");
// List stops and verify some values.
List<Stop> stops = new UraClient("mocked").getStops(); List<Stop> stops = new UraClient("mocked").getStops();
assertThat(stops, hasSize(10)); assertThat(stops, hasSize(10));
assertThat(stops.get(0).getId(), is("100210")); assertThat(stops.get(0).getId(), is("100210"));
assertThat(stops.get(1).getName(), is("Brockenberg")); assertThat(stops.get(1).getName(), is("Brockenberg"));
assertThat(stops.get(2).getState(), is(0));; assertThat(stops.get(2).getState(), is(0));
assertThat(stops.get(3).getLatitude(), is(50.7578775)); assertThat(stops.get(3).getLatitude(), is(50.7578775));
assertThat(stops.get(4).getLongitude(), is(6.0708663)); assertThat(stops.get(4).getLongitude(), is(6.0708663));
/* Test exception handling */ // Test Exception handling.
PowerMockito.when(mockURL.openStream()).thenReturn(new InputStream() { mockHttpToException("Provoked Exception 1");
@Override
public int read() throws IOException {
throw new IOException("Provoked exception 1.");
}
});
try { try {
new UraClient("mocked").getStops(); new UraClient("mocked").getStops();
} catch (RuntimeException e) { } catch (RuntimeException e) {
assertThat(e, is(instanceOf(IllegalStateException.class))); assertThat(e, is(instanceOf(IllegalStateException.class)));
assertThat(e.getCause(), is(instanceOf(IOException.class))); assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), is("Provoked exception 1.")); assertThat(e.getCause().getMessage(), is("Provoked Exception 1"));
} }
} }
public static InputStream request(String originalURL) throws IOException {
if (mockResource == null && mockException != null) {
IOException e = new IOException(mockException);
mockException = null;
throw e;
}
InputStream res = UraClientTest.class.getResourceAsStream(mockResource);
mockResource = null;
return res;
}
@Test @Test
public void getStopsForLineTest() throws Exception { public void getStopsForLineTest() {
/* Mock the HTTP call */ // Mock the HTTP call.
URL mockURL = PowerMockito.mock(URL.class); mockHttpToFile("instant_V2_stops_line.txt");
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V2_stops_line.txt"));
/* List stops and verify some values */ // List stops and verify some values.
List<Stop> stops = new UraClient("mocked").forLines("33").getStops(); List<Stop> stops = new UraClient("mocked").forLines("33").getStops();
assertThat(stops, hasSize(47)); assertThat(stops, hasSize(47));
assertThat(stops.get(0).getId(), is("100000")); assertThat(stops.get(0).getId(), is("100000"));
assertThat(stops.get(1).getName(), is("Kuckelkorn")); assertThat(stops.get(1).getName(), is("Kuckelkorn"));
assertThat(stops.get(2).getState(), is(0));; assertThat(stops.get(2).getState(), is(0));
assertThat(stops.get(3).getLatitude(), is(50.7690688)); assertThat(stops.get(3).getLatitude(), is(50.7690688));
assertThat(stops.get(4).getIndicator(), is("H.1")); assertThat(stops.get(4).getIndicator(), is("H.1"));
assertThat(stops.get(5).getLongitude(), is(6.2314072)); assertThat(stops.get(5).getLongitude(), is(6.2314072));
} }
@Test @Test
public void getStopsForPositionTest() throws Exception { public void getStopsForPositionTest() {
/* Mock the HTTP call */ // Mock the HTTP call.
URL mockURL = PowerMockito.mock(URL.class); mockHttpToFile("instant_V1_stops_circle.txt");
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_stops_circle.txt"));
/* List stops and verify some values */ // List stops and verify some values.
List<Stop> stops = new UraClient("mocked") List<Stop> stops = new UraClient("mocked")
.forPosition(51.51009, -0.1345734, 200) .forPosition(51.51009, -0.1345734, 200)
.getStops(); .getStops();
assertThat(stops, hasSize(13)); assertThat(stops, hasSize(13));
assertThat(stops.get(0).getId(), is("156")); assertThat(stops.get(0).getId(), is("156"));
assertThat(stops.get(1).getName(), is("Piccadilly Circus")); assertThat(stops.get(1).getName(), is("Piccadilly Circus"));
assertThat(stops.get(2).getState(), is(0));; assertThat(stops.get(2).getState(), is(0));
assertThat(stops.get(3).getLatitude(), is(51.509154)); assertThat(stops.get(3).getLatitude(), is(51.509154));
assertThat(stops.get(4).getLongitude(), is(-0.134172)); assertThat(stops.get(4).getLongitude(), is(-0.134172));
assertThat(stops.get(5).getIndicator(), is(nullValue())); assertThat(stops.get(5).getIndicator(), is(nullValue()));
PowerMockito.when(mockURL.openStream()) mockHttpToFile("instant_V1_stops_circle_name.txt");
.thenReturn(getClass().getResourceAsStream("instant_V1_stops_circle_name.txt"));
stops = new UraClient("mocked") stops = new UraClient("mocked")
.forStopsByName("Piccadilly Circus") .forStopsByName("Piccadilly Circus")
.forPosition(51.51009, -0.1345734, 200) .forPosition(51.51009, -0.1345734, 200)
@ -129,21 +142,17 @@ public class UraClientTest {
} }
@Test @Test
public void getTripsForDestinationNamesTest() throws Exception { public void getTripsForDestinationNamesTest() {
/* Mock the HTTP call */ // Mock the HTTP call.
URL mockURL = PowerMockito.mock(URL.class); mockHttpToFile("instant_V1_trips_destination.txt");
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_destination.txt"));
/* List stops and verify some values */ // List stops and verify some values.
List<Trip> trips = new UraClient("mocked").forDestinationNames("Piccadilly Circus").getTrips(); List<Trip> trips = new UraClient("mocked").forDestinationNames("Piccadilly Circus").getTrips();
assertThat(trips, hasSize(9)); assertThat(trips, hasSize(9));
assertThat(trips.stream().filter(t -> !t.getDestinationName().equals("Piccadilly Cir")).findAny(), assertThat(trips.stream().filter(t -> !t.getDestinationName().equals("Piccadilly Cir")).findAny(),
is(Optional.empty())); is(Optional.empty()));
PowerMockito.when(mockURL.openStream()) mockHttpToFile("instant_V1_trips_stop_destination.txt");
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_destination.txt"));
trips = new UraClient("mocked") trips = new UraClient("mocked")
.forStops("156") .forStops("156")
.forDestinationNames("Marble Arch") .forDestinationNames("Marble Arch")
@ -156,38 +165,31 @@ public class UraClientTest {
} }
@Test @Test
public void getTripsTowardsTest() throws Exception { public void getTripsTowardsTest() {
/* Mock the HTTP call */ // Mock the HTTP call.
URL mockURL = PowerMockito.mock(URL.class); mockHttpToFile("instant_V1_trips_towards.txt");
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_towards.txt"));
/* List stops and verify some values */ /* List stops and verify some values */
List<Trip> trips = new UraClient("mocked").towards("Marble Arch").getTrips(); List<Trip> trips = new UraClient("mocked").towards("Marble Arch").getTrips();
assertThat(trips, hasSize(10)); assertThat(trips, hasSize(10));
PowerMockito.when(mockURL.openStream()) mockHttpToFile("instant_V1_trips_stop_towards.txt");
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_towards.txt"));
trips = new UraClient("mocked").forStops("156").towards("Marble Arch").getTrips(); trips = new UraClient("mocked").forStops("156").towards("Marble Arch").getTrips();
assertThat(trips, hasSize(17)); assertThat(trips, hasSize(17));
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("156")).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("156")).findAny(), is(Optional.empty()));
} }
@Test @Test
public void getTripsTest() throws Exception { public void getTripsTest() {
/* Mock the HTTP call */ // Mock the HTTP call.
URL mockURL = PowerMockito.mock(URL.class); mockHttpToFile("instant_V1_trips_all.txt");
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_all.txt"));
/* Get trips without filters and verify some values */ // Get trips without filters and verify some values.
List<Trip> trips = new UraClient("mocked").getTrips(); List<Trip> trips = new UraClient("mocked").getTrips();
assertThat(trips, hasSize(10)); assertThat(trips, hasSize(10));
assertThat(trips.get(0).getId(), is("27000165015001")); assertThat(trips.get(0).getId(), is("27000165015001"));
assertThat(trips.get(1).getLineID(), is("55")); assertThat(trips.get(1).getLineID(), is("55"));
assertThat(trips.get(2).getLineName(), is("28"));; assertThat(trips.get(2).getLineName(), is("28"));
assertThat(trips.get(3).getDirectionID(), is(1)); assertThat(trips.get(3).getDirectionID(), is(1));
assertThat(trips.get(4).getDestinationName(), is("Verlautenheide Endstr.")); assertThat(trips.get(4).getDestinationName(), is("Verlautenheide Endstr."));
assertThat(trips.get(5).getDestinationText(), is("Aachen Bushof")); assertThat(trips.get(5).getDestinationText(), is("Aachen Bushof"));
@ -196,15 +198,15 @@ public class UraClientTest {
assertThat(trips.get(8).getVisitID(), is(30)); assertThat(trips.get(8).getVisitID(), is(30));
assertThat(trips.get(9).getStop().getId(), is("100002")); assertThat(trips.get(9).getStop().getId(), is("100002"));
/* Repeat test for API V2 */ // Repeat test for API V2.
PowerMockito.when(mockURL.openStream()) mockHttpToFile("instant_V2_trips_all.txt");
.thenReturn(getClass().getResourceAsStream("instant_V2_trips_all.txt"));
/* Get trips without filters and verify some values */ // Get trips without filters and verify some values.
trips = new UraClient("mocked").getTrips(); trips = new UraClient("mocked").getTrips();
assertThat(trips, hasSize(10)); assertThat(trips, hasSize(10));
assertThat(trips.get(0).getId(), is("27000165015001")); assertThat(trips.get(0).getId(), is("27000165015001"));
assertThat(trips.get(1).getLineID(), is("55")); assertThat(trips.get(1).getLineID(), is("55"));
assertThat(trips.get(2).getLineName(), is("28"));; assertThat(trips.get(2).getLineName(), is("28"));
assertThat(trips.get(3).getDirectionID(), is(1)); assertThat(trips.get(3).getDirectionID(), is(1));
assertThat(trips.get(4).getDestinationName(), is("Verlautenheide Endstr.")); assertThat(trips.get(4).getDestinationName(), is("Verlautenheide Endstr."));
assertThat(trips.get(5).getDestinationText(), is("Aachen Bushof")); assertThat(trips.get(5).getDestinationText(), is("Aachen Bushof"));
@ -213,37 +215,28 @@ public class UraClientTest {
assertThat(trips.get(8).getVisitID(), is(30)); assertThat(trips.get(8).getVisitID(), is(30));
assertThat(trips.get(9).getStop().getId(), is("100002")); assertThat(trips.get(9).getStop().getId(), is("100002"));
/* Get limited number of trips */ // Get limited number of trips.
PowerMockito.when(mockURL.openStream()) mockHttpToFile("instant_V1_trips_all.txt");
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_all.txt"));
trips = new UraClient("mocked").getTrips(5); trips = new UraClient("mocked").getTrips(5);
assertThat(trips, hasSize(5)); assertThat(trips, hasSize(5));
/* Test exception handling */ // Test mockException handling.
PowerMockito.when(mockURL.openStream()).thenReturn(new InputStream() { mockHttpToException("Provoked mockException 2");
@Override
public int read() throws IOException {
throw new IOException("Provoked exception 1.");
}
});
try { try {
new UraClient("mocked").getTrips(); new UraClient("mocked").getTrips();
} catch (RuntimeException e) { } catch (RuntimeException e) {
assertThat(e, is(instanceOf(IllegalStateException.class))); assertThat(e, is(instanceOf(IllegalStateException.class)));
assertThat(e.getCause(), is(instanceOf(IOException.class))); assertThat(e.getCause(), is(instanceOf(IOException.class)));
assertThat(e.getCause().getMessage(), is("Provoked exception 1.")); assertThat(e.getCause().getMessage(), is("Provoked mockException 2"));
} }
} }
@Test @Test
public void getTripsForStopTest() throws Exception { public void getTripsForStopTest() {
/* Mock the HTTP call */ // Mock the HTTP call.
URL mockURL = PowerMockito.mock(URL.class); mockHttpToFile("instant_V1_trips_stop.txt");
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop.txt"));
/* Get trips for stop ID 100000 (Aachen Bushof) and verify some values */ // Get trips for stop ID 100000 (Aachen Bushof) and verify some values.
List<Trip> trips = new UraClient("mocked") List<Trip> trips = new UraClient("mocked")
.forStops("100000") .forStops("100000")
.getTrips(); .getTrips();
@ -251,12 +244,11 @@ public class UraClientTest {
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("100000")).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("100000")).findAny(), is(Optional.empty()));
assertThat(trips.get(0).getId(), is("27000158010001")); assertThat(trips.get(0).getId(), is("27000158010001"));
assertThat(trips.get(1).getLineID(), is("7")); assertThat(trips.get(1).getLineID(), is("7"));
assertThat(trips.get(2).getLineName(), is("25"));; assertThat(trips.get(2).getLineName(), is("25"));
assertThat(trips.get(3).getStop().getIndicator(), is("H.15")); assertThat(trips.get(3).getStop().getIndicator(), is("H.15"));
/* Get trips for stop name "Uniklinik" and verify some values */ // Get trips for stop name "Uniklinik" and verify some values.
PowerMockito.when(mockURL.openStream()) mockHttpToFile("instant_V1_trips_stop_name.txt");
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_name.txt"));
trips = new UraClient("mocked") trips = new UraClient("mocked")
.forStopsByName("Uniklinik") .forStopsByName("Uniklinik")
.getTrips(); .getTrips();
@ -265,19 +257,16 @@ public class UraClientTest {
is(Optional.empty())); is(Optional.empty()));
assertThat(trips.get(0).getId(), is("92000043013001")); assertThat(trips.get(0).getId(), is("92000043013001"));
assertThat(trips.get(1).getLineID(), is("5")); assertThat(trips.get(1).getLineID(), is("5"));
assertThat(trips.get(2).getVehicleID(), is("317"));; assertThat(trips.get(2).getVehicleID(), is("317"));
assertThat(trips.get(3).getDirectionID(), is(1)); assertThat(trips.get(3).getDirectionID(), is(1));
} }
@Test @Test
public void getTripsForLine() throws Exception { public void getTripsForLine() {
/* Mock the HTTP call */ // Mock the HTTP call.
URL mockURL = PowerMockito.mock(URL.class); mockHttpToFile("instant_V1_trips_line.txt");
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_line.txt"));
/* Get trips for line ID 3 and verify some values */ // Get trips for line ID 3 and verify some values.
List<Trip> trips = new UraClient("mocked") List<Trip> trips = new UraClient("mocked")
.forLines("3") .forLines("3")
.getTrips(); .getTrips();
@ -285,12 +274,11 @@ public class UraClientTest {
assertThat(trips.stream().filter(t -> !t.getLineID().equals("3")).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getLineID().equals("3")).findAny(), is(Optional.empty()));
assertThat(trips.get(0).getId(), is("27000154004001")); assertThat(trips.get(0).getId(), is("27000154004001"));
assertThat(trips.get(1).getLineID(), is("3")); assertThat(trips.get(1).getLineID(), is("3"));
assertThat(trips.get(2).getLineName(), is("3.A"));; assertThat(trips.get(2).getLineName(), is("3.A"));
assertThat(trips.get(3).getStop().getIndicator(), is("H.4 (Pontwall)")); assertThat(trips.get(3).getStop().getIndicator(), is("H.4 (Pontwall)"));
/* Get trips for line name "3.A" and verify some values */ // Get trips for line name "3.A" and verify some values.
PowerMockito.when(mockURL.openStream()) mockHttpToFile("instant_V1_trips_line_name.txt");
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_name.txt"));
trips = new UraClient("mocked") trips = new UraClient("mocked")
.forLinesByName("3.A") .forLinesByName("3.A")
.getTrips(); .getTrips();
@ -298,12 +286,11 @@ public class UraClientTest {
assertThat(trips.stream().filter(t -> !t.getLineName().equals("3.A")).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getLineName().equals("3.A")).findAny(), is(Optional.empty()));
assertThat(trips.get(0).getId(), is("92000288014001")); assertThat(trips.get(0).getId(), is("92000288014001"));
assertThat(trips.get(1).getLineID(), is("3")); assertThat(trips.get(1).getLineID(), is("3"));
assertThat(trips.get(2).getLineName(), is("3.A"));; assertThat(trips.get(2).getLineName(), is("3.A"));
assertThat(trips.get(3).getStop().getName(), is("Aachen Gartenstraße")); assertThat(trips.get(3).getStop().getName(), is("Aachen Gartenstraße"));
/* Get trips for line 3 with direction 1 and verify some values */ // Get trips for line 3 with direction 1 and verify some values.
PowerMockito.when(mockURL.openStream()) mockHttpToFile("instant_V1_trips_line_direction.txt");
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_direction.txt"));
trips = new UraClient("mocked") trips = new UraClient("mocked")
.forLines("412") .forLines("412")
.forDirection(2) .forDirection(2)
@ -312,9 +299,8 @@ public class UraClientTest {
assertThat(trips.stream().filter(t -> !t.getLineID().equals("412")).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getLineID().equals("412")).findAny(), is(Optional.empty()));
assertThat(trips.stream().filter(t -> !t.getDirectionID().equals(2)).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getDirectionID().equals(2)).findAny(), is(Optional.empty()));
/* Test lineID and direction in different order */ // Test lineID and direction in different order.
PowerMockito.when(mockURL.openStream()) mockHttpToFile("instant_V1_trips_line_direction.txt");
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_line_direction.txt"));
trips = new UraClient("mocked") trips = new UraClient("mocked")
.forDirection(2) .forDirection(2)
.forLines("412") .forLines("412")
@ -325,14 +311,11 @@ public class UraClientTest {
} }
@Test @Test
public void getTripsForStopAndLine() throws Exception { public void getTripsForStopAndLine() {
/* Mock the HTTP call */ // Mock the HTTP call.
URL mockURL = PowerMockito.mock(URL.class); mockHttpToFile("instant_V1_trips_stop_line.txt");
PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockURL);
PowerMockito.when(mockURL.openStream())
.thenReturn(getClass().getResourceAsStream("instant_V1_trips_stop_line.txt"));
/* Get trips for line ID 25 and 25 at stop 100000 and verify some values */ // Get trips for line ID 25 and 25 at stop 100000 and verify some values.
List<Trip> trips = new UraClient("mocked") List<Trip> trips = new UraClient("mocked")
.forLines("25", "35") .forLines("25", "35")
.forStops("100000") .forStops("100000")
@ -343,7 +326,16 @@ public class UraClientTest {
assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("100000")).findAny(), is(Optional.empty())); assertThat(trips.stream().filter(t -> !t.getStop().getId().equals("100000")).findAny(), is(Optional.empty()));
assertThat(trips.get(0).getId(), is("27000078014001")); assertThat(trips.get(0).getId(), is("27000078014001"));
assertThat(trips.get(1).getLineID(), is("25")); assertThat(trips.get(1).getLineID(), is("25"));
assertThat(trips.get(3).getLineName(), is("35"));; assertThat(trips.get(3).getLineName(), is("35"));
assertThat(trips.get(5).getStop().getIndicator(), is("H.12")); assertThat(trips.get(5).getStop().getIndicator(), is("H.12"));
} }
private static void mockHttpToFile(String newResourceFile) {
mockResource = newResourceFile;
}
private static void mockHttpToException(String newException) {
mockException = newException;
}
} }

View File

@ -16,7 +16,7 @@
package de.stklcode.pubtrans.ura.model; package de.stklcode.pubtrans.ura.model;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -26,7 +26,7 @@ import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.fail;
/** /**
* Unit test for the Stop metamodel. * Unit test for the Stop metamodel.

View File

@ -16,7 +16,7 @@
package de.stklcode.pubtrans.ura.model; package de.stklcode.pubtrans.ura.model;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -26,7 +26,7 @@ import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.fail;
/** /**
* Unit test for the Trip metamodel. * Unit test for the Trip metamodel.