Skip to content

Commit 9334858

Browse files
author
Christopher Kilding
committed
Use AssertJ in tests
1 parent b9f489e commit 9334858

File tree

11 files changed

+158
-58
lines changed

11 files changed

+158
-58
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
<version>4.12</version>
3737
<scope>test</scope>
3838
</dependency>
39+
<dependency>
40+
<groupId>org.assertj</groupId>
41+
<artifactId>assertj-core</artifactId>
42+
<version>3.10.0</version>
43+
<scope>test</scope>
44+
</dependency>
3945
<dependency>
4046
<groupId>com.xebialabs.restito</groupId>
4147
<artifactId>restito</artifactId>

src/test/java/com/github/loadtest4j/drivers/wrk/QueryStringTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.util.LinkedHashMap;
99
import java.util.Map;
1010

11-
import static org.junit.Assert.assertEquals;
11+
import static org.assertj.core.api.Assertions.assertThat;
1212

1313
@Category(UnitTest.class)
1414
public class QueryStringTest {
@@ -17,7 +17,7 @@ public class QueryStringTest {
1717
public void testQueryString() {
1818
final QueryString queryString = new QueryString(Collections.singletonMap("foo", "1"));
1919

20-
assertEquals("?foo=1", queryString.toString());
20+
assertThat(queryString).hasToString("?foo=1");
2121
}
2222

2323
@Test
@@ -29,6 +29,6 @@ public void testQueryStringWithMultipleParams() {
2929

3030
final QueryString queryString = new QueryString(queryParams);
3131

32-
assertEquals("?foo=1&bar=2", queryString.toString());
32+
assertThat(queryString).hasToString("?foo=1&bar=2");
3333
}
3434
}

src/test/java/com/github/loadtest4j/drivers/wrk/WrkFactoryTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
import java.util.Map;
1313
import java.util.Set;
1414

15-
import static org.junit.Assert.assertEquals;
16-
import static org.junit.Assert.assertNotNull;
17-
import static org.junit.Assert.assertTrue;
15+
import static org.assertj.core.api.Assertions.assertThat;
1816

1917
@Category(UnitTest.class)
2018
public class WrkFactoryTest {
@@ -32,9 +30,7 @@ public void testGetMandatoryProperties() {
3230

3331
final Set<String> mandatoryProperties = sut.getMandatoryProperties();
3432

35-
assertEquals(2, mandatoryProperties.size());
36-
assertTrue(mandatoryProperties.contains("duration"));
37-
assertTrue(mandatoryProperties.contains("url"));
33+
assertThat(mandatoryProperties).containsExactly("duration", "url");
3834
}
3935

4036
@Test(expected = UnsupportedOperationException.class)
@@ -54,6 +50,6 @@ public void testCreate() {
5450

5551
final Driver driver = sut.create(properties);
5652

57-
assertNotNull(driver);
53+
assertThat(driver).isInstanceOf(Wrk.class);
5854
}
5955
}

src/test/java/com/github/loadtest4j/drivers/wrk/WrkReportTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.io.IOException;
1111
import java.net.URI;
1212

13-
import static org.junit.Assert.assertEquals;
13+
import static org.assertj.core.api.Assertions.assertThat;
1414

1515
@Category(IntegrationTest.class)
1616
public class WrkReportTest {
@@ -20,7 +20,7 @@ public void testSave() {
2020

2121
final URI uri = wrkReport.save(new Output());
2222

23-
assertEquals("file", uri.getScheme());
23+
assertThat(uri).hasScheme("file");
2424
}
2525

2626
@Test(expected = LoadTesterException.class)

src/test/java/com/github/loadtest4j/drivers/wrk/WrkResponseTimeTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import java.time.Duration;
99
import java.util.Collections;
1010

11-
import static org.junit.Assert.assertEquals;
11+
import static org.assertj.core.api.Assertions.assertThat;
1212

1313
@Category(UnitTest.class)
1414
public class WrkResponseTimeTest {
@@ -17,35 +17,35 @@ public class WrkResponseTimeTest {
1717
public void testGetPercentile() {
1818
final ResponseTime responseTime = new WrkResponseTime(Collections.singletonMap(50, 1000L));
1919

20-
assertEquals(Duration.ofMillis(1), responseTime.getPercentile(50));
20+
assertThat(responseTime.getPercentile(50)).isEqualTo(Duration.ofMillis(1));
2121
}
2222

2323
@Test
2424
public void testGetMaxPercentile() {
2525
final ResponseTime responseTime = new WrkResponseTime(Collections.singletonMap(100, 1000L));
2626

27-
assertEquals(Duration.ofMillis(1), responseTime.getPercentile(100));
27+
assertThat(responseTime.getPercentile(100)).isEqualTo(Duration.ofMillis(1));
2828
}
2929

3030
@Test
3131
public void testGetMinPercentile() {
3232
final ResponseTime responseTime = new WrkResponseTime(Collections.singletonMap(0, 1000L));
3333

34-
assertEquals(Duration.ofMillis(1), responseTime.getPercentile(0));
34+
assertThat(responseTime.getPercentile(0)).isEqualTo(Duration.ofMillis(1));
3535
}
3636

3737
@Test(expected = IllegalArgumentException.class)
3838
public void testGetTooLowPercentile() {
3939
final ResponseTime responseTime = new WrkResponseTime(Collections.singletonMap(-1, 1000L));
4040

41-
assertEquals(Duration.ofMillis(1), responseTime.getPercentile(-1));
41+
responseTime.getPercentile(-1);
4242
}
4343

4444
@Test(expected = IllegalArgumentException.class)
4545
public void testGetTooHighPercentile() {
4646
final ResponseTime responseTime = new WrkResponseTime(Collections.singletonMap(101, 1000L));
4747

48-
assertEquals(Duration.ofMillis(1), responseTime.getPercentile(101));
48+
responseTime.getPercentile(-1);
4949
}
5050

5151
@Test(expected = NullPointerException.class)

src/test/java/com/github/loadtest4j/drivers/wrk/WrkTest.java

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.github.loadtest4j.drivers.wrk;
22

33
import com.github.loadtest4j.drivers.wrk.junit.IntegrationTest;
4+
import com.github.loadtest4j.loadtest4j.LoadTesterException;
45
import com.github.loadtest4j.loadtest4j.driver.Driver;
56
import com.github.loadtest4j.loadtest4j.driver.DriverRequest;
67
import com.github.loadtest4j.loadtest4j.driver.DriverResult;
7-
import com.github.loadtest4j.loadtest4j.LoadTesterException;
88
import com.xebialabs.restito.server.StubServer;
99
import org.glassfish.grizzly.http.util.HttpStatus;
1010
import org.junit.After;
@@ -18,14 +18,13 @@
1818
import java.util.Arrays;
1919
import java.util.Collections;
2020
import java.util.List;
21-
import java.util.Optional;
2221
import java.util.logging.Level;
2322
import java.util.logging.Logger;
2423

24+
import static com.github.loadtest4j.drivers.wrk.junit.DriverResultAssert.assertThat;
2525
import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
2626
import static com.xebialabs.restito.semantics.Action.status;
2727
import static com.xebialabs.restito.semantics.Condition.*;
28-
import static org.junit.Assert.*;
2928

3029
@Category(IntegrationTest.class)
3130
public class WrkTest {
@@ -73,13 +72,12 @@ public void testRun() {
7372
final DriverResult result = driver.run(requests);
7473

7574
// Then
76-
assertTrue(result.getOk() > 0);
77-
assertEquals(0, result.getKo());
78-
assertTrue(result.getActualDuration().toMillis() >= EXPECTED_DURATION.toMillis());
79-
assertTrue(result.getResponseTime().getPercentile(100).toMillis() > 0);
80-
final Optional<String> reportUrl = result.getReportUrl();
81-
assertTrue(reportUrl.isPresent());
82-
assertTrue(reportUrl.get().startsWith("file:///"));
75+
assertThat(result)
76+
.hasOkGreaterThan(0)
77+
.hasKo(0)
78+
.hasReportUrlWithScheme("file")
79+
.hasActualDurationGreaterThan(EXPECTED_DURATION)
80+
.hasMaxResponseTimeGreaterThan(Duration.ZERO);
8381
}
8482

8583
@Test
@@ -96,8 +94,9 @@ public void testRunWithMultipleRequests() {
9694
final DriverResult result = driver.run(requests);
9795

9896
// Then
99-
assertTrue(result.getOk() > 0);
100-
assertEquals(0, result.getKo());
97+
assertThat(result)
98+
.hasOkGreaterThan(0)
99+
.hasKo(0);
101100
}
102101

103102
@Test
@@ -117,8 +116,9 @@ public void testRunWithJsonPost() {
117116
final DriverResult result = driver.run(requests);
118117

119118
// Then
120-
assertTrue(result.getOk() > 0);
121-
assertEquals(0, result.getKo());
119+
assertThat(result)
120+
.hasOkGreaterThan(0)
121+
.hasKo(0);
122122
}
123123

124124
@Test
@@ -140,8 +140,9 @@ public void testRunWithEdgeCaseRequest() {
140140
final DriverResult result = driver.run(requests);
141141

142142
// Then
143-
assertTrue(result.getOk() > 0);
144-
assertEquals(0, result.getKo());
143+
assertThat(result)
144+
.hasOkGreaterThan(0)
145+
.hasKo(0);
145146
}
146147

147148
@Test
@@ -158,8 +159,9 @@ public void testRunWithErrors() {
158159
final DriverResult result = driver.run(requests);
159160

160161
// Then
161-
assertEquals(0, result.getOk());
162-
assertTrue(result.getKo() > 0);
162+
assertThat(result)
163+
.hasOk(0)
164+
.hasKoGreaterThan(0);
163165
}
164166

165167
@Test
@@ -182,14 +184,11 @@ public void testRunWithNoRequests() {
182184
// Given
183185
final Driver driver = sut();
184186

187+
// Expect
188+
thrown.expect(LoadTesterException.class);
189+
thrown.expectMessage("No requests were specified for the load test.");
190+
185191
// When
186-
try {
187-
driver.run(Collections.emptyList());
188-
189-
fail("This should not work.");
190-
} catch (LoadTesterException e) {
191-
// Then
192-
assertEquals("No requests were specified for the load test.", e.getMessage());
193-
}
192+
driver.run(Collections.emptyList());
194193
}
195194
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.github.loadtest4j.drivers.wrk.junit;
2+
3+
import com.github.loadtest4j.loadtest4j.driver.DriverResult;
4+
import org.assertj.core.api.AbstractAssert;
5+
6+
import java.time.Duration;
7+
import java.util.Objects;
8+
9+
public class DriverResultAssert extends AbstractAssert<DriverResultAssert, DriverResult> {
10+
11+
private DriverResultAssert(DriverResult driverResult) {
12+
super(driverResult, DriverResultAssert.class);
13+
}
14+
15+
public static DriverResultAssert assertThat(DriverResult actual) {
16+
return new DriverResultAssert(actual);
17+
}
18+
19+
public DriverResultAssert hasOk(long ok) {
20+
isNotNull();
21+
22+
if (ok != actual.getOk()) {
23+
failWithMessage("Expected # OK requests to be <%d> but was <%d>", ok, actual.getOk());
24+
}
25+
26+
return this;
27+
}
28+
29+
public DriverResultAssert hasOkGreaterThan(long ok) {
30+
isNotNull();
31+
32+
if (actual.getOk() < ok) {
33+
failWithMessage("Expected # OK requests to be greater than <%d> but it was not", ok);
34+
}
35+
36+
return this;
37+
}
38+
39+
public DriverResultAssert hasKo(long ko) {
40+
isNotNull();
41+
42+
if (ko != actual.getKo()) {
43+
failWithMessage("Expected # KO requests to be <%d> but was <%d>", ko, actual.getKo());
44+
}
45+
46+
return this;
47+
}
48+
49+
public DriverResultAssert hasKoGreaterThan(long ko) {
50+
isNotNull();
51+
52+
if (actual.getKo() < ko) {
53+
failWithMessage("Expected # KO requests to be greater than <%d> but it was not", ko);
54+
}
55+
56+
return this;
57+
}
58+
59+
public DriverResultAssert hasActualDurationGreaterThan(Duration actualDuration) {
60+
isNotNull();
61+
62+
if (actual.getActualDuration().compareTo(actualDuration) < 1) {
63+
failWithMessage("Expected actual duration to be greater than <%s> but was <%s>", actualDuration, actual.getActualDuration());
64+
}
65+
66+
return this;
67+
}
68+
69+
public DriverResultAssert hasMaxResponseTimeGreaterThan(Duration responseTime) {
70+
isNotNull();
71+
72+
if (actual.getResponseTime().getPercentile(100).compareTo(responseTime) < 1) {
73+
failWithMessage("Expected max response time to be greater than <%s> but was <%s>", responseTime, actual.getResponseTime().getPercentile(100));
74+
}
75+
76+
return this;
77+
}
78+
79+
public DriverResultAssert hasReportUrlWithScheme(String scheme) {
80+
isNotNull();
81+
82+
if (!actual.getReportUrl().isPresent()) {
83+
failWithMessage("Expected report URL to be present but was absent");
84+
}
85+
86+
if (!actual.getReportUrl().get().startsWith(scheme)) {
87+
failWithMessage("Expected report URL scheme to be <%s> but it was not", scheme);
88+
}
89+
90+
return this;
91+
}
92+
93+
public DriverResultAssert hasResponseTimePercentile(int percentile, Duration responseTime) {
94+
isNotNull();
95+
96+
if (!Objects.equals(responseTime, actual.getResponseTime().getPercentile(percentile))) {
97+
failWithMessage("Expected response time p%d to be <%s> but was <%s>", percentile, responseTime, actual.getResponseTime().getPercentile(percentile));
98+
}
99+
100+
return this;
101+
}
102+
}

src/test/java/com/github/loadtest4j/drivers/wrk/shell/ArgumentBuilderTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import org.junit.Test;
55
import org.junit.experimental.categories.Category;
66

7-
import java.util.Arrays;
8-
import java.util.Collections;
97
import java.util.List;
108

11-
import static org.junit.Assert.assertEquals;
9+
import static org.assertj.core.api.Assertions.assertThat;
1210

1311
@Category(UnitTest.class)
1412
public class ArgumentBuilderTest {
@@ -18,7 +16,7 @@ public void testAddArgument() {
1816

1917
final List<String> args = sut.addArgument("foo").build();
2018

21-
assertEquals(Collections.singletonList("foo"), args);
19+
assertThat(args).containsExactly("foo");
2220
}
2321

2422
@Test
@@ -27,6 +25,6 @@ public void testAddNamedArgument() {
2725

2826
final List<String> args = sut.addNamedArgument("--foo", "bar").build();
2927

30-
assertEquals(Arrays.asList("--foo", "bar"), args);
28+
assertThat(args).containsExactly("--foo", "bar");
3129
}
3230
}

0 commit comments

Comments
 (0)