Skip to content

Commit d13a1da

Browse files
Upgrade to JUnit 5
1 parent daa51f8 commit d13a1da

File tree

5 files changed

+68
-65
lines changed

5 files changed

+68
-65
lines changed

pom.xml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@
5858

5959
<dependencies>
6060
<dependency>
61-
<groupId>junit</groupId>
62-
<artifactId>junit</artifactId>
63-
<version>4.13.2</version>
61+
<groupId>org.junit.jupiter</groupId>
62+
<artifactId>junit-jupiter</artifactId>
63+
<version>5.9.2</version>
6464
</dependency>
6565
<dependency>
6666
<groupId>org.mockito</groupId>
6767
<artifactId>mockito-core</artifactId>
68-
<version>4.6.1</version>
68+
<version>5.1.1</version>
6969
<scope>test</scope>
7070
</dependency>
7171
<dependency>
@@ -190,6 +190,11 @@
190190
<tagNameFormat>@{project.version}</tagNameFormat>
191191
</configuration>
192192
</plugin>
193+
<plugin>
194+
<groupId>org.apache.maven.plugins</groupId>
195+
<artifactId>maven-surefire-plugin</artifactId>
196+
<version>3.0.0-M8</version>
197+
</plugin>
193198
</plugins>
194199
</build>
195200
</project>

src/test/java/engineer/nightowl/sonos/api/BaseTestSetup.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package engineer.nightowl.sonos.api;
22

3-
import org.junit.BeforeClass;
3+
import org.junit.jupiter.api.BeforeAll;
44

55
public class BaseTestSetup
66
{
77
protected static final SonosApiConfiguration configuration = new SonosApiConfiguration();
88

99
protected final SonosApiClient apiClient = new SonosApiClient(configuration);
1010

11-
@BeforeClass
12-
public static void setup()
11+
@BeforeAll
12+
static void setup()
1313
{
1414
configuration.setApiKey("testApiKey");
1515
configuration.setApiSecret("testApiSecret");

src/test/java/engineer/nightowl/sonos/api/resource/AuthorizeResourceTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,41 @@
33
import engineer.nightowl.sonos.api.BaseTestSetup;
44
import org.apache.http.NameValuePair;
55
import org.apache.http.client.utils.URLEncodedUtils;
6-
import org.junit.Assert;
7-
import org.junit.Test;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
89

910
import java.net.URI;
1011
import java.net.URISyntaxException;
1112
import java.nio.charset.StandardCharsets;
1213
import java.util.HashMap;
1314
import java.util.List;
1415

15-
public class AuthorizeResourceTest extends BaseTestSetup
16+
class AuthorizeResourceTest extends BaseTestSetup
1617
{
1718
private final AuthorizeResource authorizeResource = new AuthorizeResource(apiClient);
1819

1920
@Test
2021
public void testGetAuthorizeCodeUri() throws URISyntaxException
2122
{
2223
final URI uri = authorizeResource.getAuthorizeCodeUri("https://localhost");
23-
Assert.assertEquals("https", uri.getScheme());
24+
assertEquals("https", uri.getScheme());
2425
}
2526

2627
@Test
2728
public void testGetAuthorizeCodeWithState() throws URISyntaxException
2829
{
2930
final String state = authorizeResource.generateStateValue();
3031
final URI uri = authorizeResource.getAuthorizeCodeUri("https://localhost", state);
31-
Assert.assertEquals("https", uri.getScheme());
32+
assertEquals("https", uri.getScheme());
3233
uri.getQuery();
3334

3435
final List<NameValuePair> params = URLEncodedUtils.parse(uri, StandardCharsets.UTF_8);
3536
final HashMap<String, String> map = new HashMap<>();
3637
params.forEach(p -> map.put(p.getName(), p.getValue()));
3738

38-
Assert.assertEquals(state, map.get("state"));
39-
Assert.assertEquals(configuration.getApiKey(), map.get("client_id"));
39+
assertEquals(state, map.get("state"));
40+
assertEquals(configuration.getApiKey(), map.get("client_id"));
4041
}
4142

4243
}

src/test/java/engineer/nightowl/sonos/api/resource/BaseResourceTest.java

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,26 @@
1818
import org.apache.http.impl.client.CloseableHttpClient;
1919
import org.apache.http.message.BasicHeader;
2020
import org.apache.http.message.BasicStatusLine;
21-
import org.junit.Assert;
22-
import org.junit.BeforeClass;
23-
import org.junit.Rule;
24-
import org.junit.Test;
25-
import org.mockito.junit.MockitoJUnit;
26-
import org.mockito.junit.MockitoRule;
27-
import org.mockito.quality.Strictness;
21+
import org.junit.jupiter.api.BeforeAll;
22+
import org.junit.jupiter.api.Test;
2823

2924
import java.io.IOException;
3025

26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertThrows;
28+
import static org.junit.jupiter.api.Assertions.fail;
3129
import static org.mockito.ArgumentMatchers.any;
3230
import static org.mockito.Mockito.mock;
3331
import static org.mockito.Mockito.when;
3432

3533
public class BaseResourceTest
3634
{
37-
@Rule
38-
public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
39-
4035
private static BaseResource baseResource;
4136
private static SonosApiClient client;
4237
private static SonosApiConfiguration configuration;
4338
private static CloseableHttpClient mockedClient;
4439

45-
@BeforeClass
40+
@BeforeAll
4641
public static void setUp() throws Exception
4742
{
4843
client = mock(SonosApiClient.class);
@@ -56,18 +51,18 @@ public static void setUp() throws Exception
5651
}
5752

5853
@Test
59-
public void getTypeFromHeader() throws SonosApiClientException
54+
void getTypeFromHeader() throws SonosApiClientException
6055
{
6156
final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
6257
when(response.getFirstHeader(BaseResource.SONOS_TYPE_HEADER))
6358
.thenReturn(new BasicHeader(BaseResource.SONOS_TYPE_HEADER, "homeTheaterOptions"));
6459
final SonosType type = baseResource.getTypeFromHeader(response);
6560

66-
Assert.assertEquals(SonosType.homeTheaterOptions, type);
61+
assertEquals(SonosType.homeTheaterOptions, type);
6762
}
6863

6964
@Test
70-
public void getInvalidTypeFromHeader() throws SonosApiClientException
65+
void getInvalidTypeFromHeader() throws SonosApiClientException
7166
{
7267
final CloseableHttpResponse response = mock(CloseableHttpResponse.class);
7368
when(response.getFirstHeader(BaseResource.SONOS_TYPE_HEADER))
@@ -76,16 +71,16 @@ public void getInvalidTypeFromHeader() throws SonosApiClientException
7671
try
7772
{
7873
baseResource.getTypeFromHeader(response);
79-
Assert.fail("Did not fail as expected");
74+
fail("Did not fail as expected");
8075
}
8176
catch(SonosApiClientException sace)
8277
{
83-
Assert.assertEquals(IllegalArgumentException.class, sace.getCause().getClass());
78+
assertEquals(IllegalArgumentException.class, sace.getCause().getClass());
8479
}
8580
}
8681

8782
@Test
88-
public void testThatMainApiCallWorks() throws IOException, SonosApiClientException, SonosApiError
83+
void testThatMainApiCallWorks() throws IOException, SonosApiClientException, SonosApiError
8984
{
9085
// Test data
9186
final SonosHomeTheaterOptions options = new SonosHomeTheaterOptions();
@@ -104,11 +99,11 @@ public void testThatMainApiCallWorks() throws IOException, SonosApiClientExcepti
10499
final SonosHomeTheaterOptions responseOptions = baseResource.getFromApi(SonosHomeTheaterOptions.class,
105100
"token123", "some/test");
106101

107-
Assert.assertEquals(options, responseOptions);
102+
assertEquals(options, responseOptions);
108103
}
109104

110105
@Test
111-
public void testSonosNotDeclaringTypeStillWorks() throws IOException, SonosApiClientException, SonosApiError
106+
void testSonosNotDeclaringTypeStillWorks() throws IOException, SonosApiClientException, SonosApiError
112107
{
113108
// Test data
114109
final SonosHomeTheaterOptions options = new SonosHomeTheaterOptions();
@@ -129,11 +124,11 @@ public void testSonosNotDeclaringTypeStillWorks() throws IOException, SonosApiCl
129124
final SonosHomeTheaterOptions responseOptions = baseResource.getFromApi(SonosHomeTheaterOptions.class,
130125
"token123", "some/test");
131126

132-
Assert.assertEquals(options, responseOptions);
127+
assertEquals(options, responseOptions);
133128
}
134129

135130
@Test
136-
public void testAuthErrorThrown() throws IOException, SonosApiClientException, SonosApiError
131+
void testAuthErrorThrown() throws IOException, SonosApiClientException, SonosApiError
137132
{
138133
final CloseableHttpResponse mockedResponse = mock(CloseableHttpResponse.class);
139134
final StatusLine sl = new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 401, null);
@@ -142,16 +137,16 @@ public void testAuthErrorThrown() throws IOException, SonosApiClientException, S
142137
try
143138
{
144139
baseResource.getFromApi(SonosHomeTheaterOptions.class, "token123", "some/test");
145-
Assert.fail("Did not handle error response correctly");
140+
fail("Did not handle error response correctly");
146141
}
147142
catch (final SonosApiClientException e)
148143
{
149-
Assert.assertEquals("Invalid token", e.getMessage());
144+
assertEquals("Invalid token", e.getMessage());
150145
}
151146
}
152147

153148
@Test
154-
public void testApiErrorHandledCorrectly() throws IOException, SonosApiClientException, SonosApiError
149+
void testApiErrorHandledCorrectly() throws IOException, SonosApiClientException, SonosApiError
155150
{
156151
// Test data
157152
final SonosApiError error = new SonosApiError();
@@ -171,16 +166,16 @@ public void testApiErrorHandledCorrectly() throws IOException, SonosApiClientExc
171166
try
172167
{
173168
baseResource.getFromApi(SonosHomeTheaterOptions.class, "token123", "some/test");
174-
Assert.fail("Did not handle error response correctly");
169+
fail("Did not handle error response correctly");
175170
}
176171
catch (final SonosApiError e)
177172
{
178-
Assert.assertEquals(SonosErrorCode.ERROR_NOT_CAPABLE, e.getErrorCode());
173+
assertEquals(SonosErrorCode.ERROR_NOT_CAPABLE, e.getErrorCode());
179174
}
180175
}
181176

182177
@Test
183-
public void testApiMismatchHandledCorrectly() throws IOException, SonosApiClientException, SonosApiError
178+
void testApiMismatchHandledCorrectly() throws IOException, SonosApiClientException, SonosApiError
184179
{
185180
// Test data
186181
final SonosHomeTheaterOptions options = new SonosHomeTheaterOptions();
@@ -201,55 +196,55 @@ public void testApiMismatchHandledCorrectly() throws IOException, SonosApiClient
201196
try
202197
{
203198
baseResource.getFromApi(SonosHomeTheaterOptions.class, "token123", "some/test");
204-
Assert.fail("Did not handle error response correctly");
199+
fail("Did not handle error response correctly");
205200
}
206201
catch (final SonosApiClientException e)
207202
{
208-
Assert.assertEquals("Sonos declared SonosAudioClip as the response type, but the integration requested SonosHomeTheaterOptions", e.getMessage());
203+
assertEquals("Sonos declared SonosAudioClip as the response type, but the integration requested SonosHomeTheaterOptions", e.getMessage());
209204
}
210205
}
211206

212207
@Test
213-
public void getStandardRequest() throws SonosApiClientException
208+
void getStandardRequest() throws SonosApiClientException
214209
{
215210
final HttpGet req = baseResource.getStandardRequest(HttpGet.class, "token123", "/some/path");
216-
Assert.assertEquals(HttpGet.METHOD_NAME,
211+
assertEquals(HttpGet.METHOD_NAME,
217212
req.getMethod());
218213

219-
Assert.assertEquals("Bearer token123",
214+
assertEquals("Bearer token123",
220215
req.getFirstHeader("Authorization").getValue());
221216

222-
Assert.assertEquals(
217+
assertEquals(
223218
"/control/api/some/path",
224219
req.getURI().getPath());
225220
}
226221

227222
@Test
228-
public void testValidateNotNullNoFieldName() throws SonosApiClientException
223+
void testValidateNotNullNoFieldName() throws SonosApiClientException
229224
{
230225
baseResource.validateNotNull("nonEmptyString");
231226
}
232227

233-
@Test(expected=SonosApiClientException.class)
234-
public void testValidateNotNullBothNulls() throws SonosApiClientException
228+
@Test
229+
void testValidateNotNullBothNulls()
235230
{
236-
baseResource.validateNotNull(null, null);
231+
assertThrows(SonosApiClientException.class, () -> baseResource.validateNotNull(null, null));
237232
}
238233

239-
@Test(expected=SonosApiClientException.class)
240-
public void testValidateNotNullObjectNull() throws SonosApiClientException
234+
@Test
235+
void testValidateNotNullObjectNull()
241236
{
242-
baseResource.validateNotNull(null, "sonos-api-java");
237+
assertThrows(SonosApiClientException.class, () -> baseResource.validateNotNull(null, "sonos-api-java"));
243238
}
244239

245-
@Test(expected=SonosApiClientException.class)
246-
public void testValidateNotNullEmptyObject() throws SonosApiClientException
240+
@Test
241+
void testValidateNotNullEmptyObject()
247242
{
248-
baseResource.validateNotNull("", "sonos-api-java");
243+
assertThrows(SonosApiClientException.class, () -> baseResource.validateNotNull("", "sonos-api-java"));
249244
}
250245

251246
@Test
252-
public void testValidateNotNullNonEmptyObject() throws SonosApiClientException
247+
void testValidateNotNullNonEmptyObject() throws SonosApiClientException
253248
{
254249
baseResource.validateNotNull("nonEmptyString", "sonos-api-java");
255250
}

src/test/java/engineer/nightowl/sonos/api/util/SonosCallbackHelperTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
import engineer.nightowl.sonos.api.exception.SonosApiClientException;
44
import org.apache.http.entity.ContentType;
5-
import org.junit.Assert;
6-
import org.junit.Test;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertFalse;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
79

810
import java.security.NoSuchAlgorithmException;
911
import java.util.HashMap;
1012
import java.util.Map;
1113

12-
public class SonosCallbackHelperTest
14+
class SonosCallbackHelperTest
1315
{
1416
final String apiKey = "sonos";
1517
final String apiSecret = "secret";
@@ -43,14 +45,14 @@ private Map<String, String> getInvalidHeaders()
4345
}
4446

4547
@Test
46-
public void testVerifySignatureWithValidHeaders() throws SonosApiClientException, NoSuchAlgorithmException
48+
void testVerifySignatureWithValidHeaders() throws SonosApiClientException, NoSuchAlgorithmException
4749
{
48-
Assert.assertTrue(SonosCallbackHelper.verifySignature(getValidHeaders(), apiKey, apiSecret));
50+
assertTrue(SonosCallbackHelper.verifySignature(getValidHeaders(), apiKey, apiSecret));
4951
}
5052

5153
@Test
52-
public void testVerifySignatureWithInvalidHeaders() throws SonosApiClientException, NoSuchAlgorithmException
54+
void testVerifySignatureWithInvalidHeaders() throws SonosApiClientException, NoSuchAlgorithmException
5355
{
54-
Assert.assertFalse(SonosCallbackHelper.verifySignature(getInvalidHeaders(), apiKey, apiSecret));
56+
assertFalse(SonosCallbackHelper.verifySignature(getInvalidHeaders(), apiKey, apiSecret));
5557
}
5658
}

0 commit comments

Comments
 (0)