Use JUnit 5 for tests (#2968)

This commit is contained in:
BrennanConroy 2018-09-18 09:45:29 -07:00 committed by GitHub
parent 8f1ecc8bba
commit 70ea1268a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 99 additions and 140 deletions

View File

@ -16,7 +16,9 @@ repositories {
}
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testCompile 'org.junit.jupiter:junit-jupiter-params:5.3.1'
testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
implementation "org.java-websocket:Java-WebSocket:1.3.8"
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
@ -41,6 +43,10 @@ spotless {
}
}
test {
useJUnitPlatform()
}
task sourceJar(type: Jar) {
classifier "sources"
from sourceSets.main.allJava

View File

@ -3,9 +3,9 @@
package com.microsoft.aspnet.signalr;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class HandshakeProtocolTest {

View File

@ -3,22 +3,17 @@
package com.microsoft.aspnet.signalr;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.Test;
public class HubConnectionTest {
private static final String RECORD_SEPARATOR = "\u001e";
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@Test
public void checkHubConnectionState() throws Exception {
Transport mockTransport = new MockTransport();
@ -47,14 +42,12 @@ public class HubConnectionTest {
@Test
public void hubConnectionReceiveHandshakeResponseWithError() throws Exception {
exceptionRule.expect(HubException.class);
exceptionRule.expectMessage("Requested protocol 'messagepack' is not available.");
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
hubConnection.start();
mockTransport.receiveMessage("{\"error\":\"Requested protocol 'messagepack' is not available.\"}" + RECORD_SEPARATOR);
Throwable exception = assertThrows(HubException.class, () -> mockTransport.receiveMessage("{\"error\":\"Requested protocol 'messagepack' is not available.\"}" + RECORD_SEPARATOR));
assertEquals("Error in handshake Requested protocol 'messagepack' is not available.", exception.getMessage());
}
@Test
@ -67,7 +60,7 @@ public class HubConnectionTest {
hubConnection.on("inc", action);
hubConnection.on("inc", action);
assertEquals(0.0, value.get(), 0);
assertEquals(Double.valueOf(0), value.get());
hubConnection.start();
@ -80,7 +73,7 @@ public class HubConnectionTest {
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(2, value.get(), 0);
assertEquals(Double.valueOf(2), value.get());
}
@Test
@ -92,7 +85,7 @@ public class HubConnectionTest {
hubConnection.on("inc", action);
assertEquals(0.0, value.get(), 0);
assertEquals(Double.valueOf(0), value.get());
hubConnection.start();
String message = mockTransport.getSentMessages()[0];
@ -104,10 +97,10 @@ public class HubConnectionTest {
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(1, value.get(), 0);
assertEquals(Double.valueOf(1), value.get());
hubConnection.remove("inc");
assertEquals(1, value.get(), 0);
assertEquals(Double.valueOf(1), value.get());
}
@Test
@ -120,7 +113,7 @@ public class HubConnectionTest {
hubConnection.on("inc", action);
hubConnection.remove("inc");
assertEquals(0.0, value.get(), 0);
assertEquals(Double.valueOf(0), value.get());
hubConnection.start();
String message = mockTransport.getSentMessages()[0];
@ -132,7 +125,7 @@ public class HubConnectionTest {
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that the handler was removed.
assertEquals(0.0, value.get(), 0);
assertEquals(Double.valueOf(0), value.get());
}
@Test
@ -146,7 +139,7 @@ public class HubConnectionTest {
hubConnection.on("inc", action);
hubConnection.on("inc", secondAction);
assertEquals(0.0, value.get(), 0);
assertEquals(Double.valueOf(0), value.get());
hubConnection.start();
String message = mockTransport.getSentMessages()[0];
@ -157,14 +150,14 @@ public class HubConnectionTest {
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
assertEquals(3, value.get(), 0);
assertEquals(Double.valueOf(3), value.get());
hubConnection.remove("inc");
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirm that another invocation doesn't change anything because the handlers have been removed.
assertEquals(3, value.get(), 0);
assertEquals(Double.valueOf(3), value.get());
}
@Test
@ -176,7 +169,7 @@ public class HubConnectionTest {
Subscription subscription = hubConnection.on("inc", action);
assertEquals(0.0, value.get(), 0);
assertEquals(Double.valueOf(0), value.get());
hubConnection.start();
String message = mockTransport.getSentMessages()[0];
@ -188,7 +181,7 @@ public class HubConnectionTest {
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(1, value.get(), 0);
assertEquals(Double.valueOf(1), value.get());
subscription.unsubscribe();
try {
@ -197,7 +190,7 @@ public class HubConnectionTest {
assertEquals("There are no callbacks registered for the method 'inc'.", ex.getMessage());
}
assertEquals(1, value.get(), 0);
assertEquals(Double.valueOf(1), value.get());
}
@Test
@ -209,7 +202,7 @@ public class HubConnectionTest {
Subscription subscription = hubConnection.on("inc", action);
assertEquals(0.0, value.get(), 0);
assertEquals(Double.valueOf(0), value.get());
hubConnection.start();
String message = mockTransport.getSentMessages()[0];
@ -221,7 +214,7 @@ public class HubConnectionTest {
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(1, value.get(), 0);
assertEquals(Double.valueOf(1), value.get());
subscription.unsubscribe();
subscription.unsubscribe();
@ -231,7 +224,7 @@ public class HubConnectionTest {
assertEquals("There are no callbacks registered for the method 'inc'.", ex.getMessage());
}
assertEquals(1, value.get(), 0);
assertEquals(Double.valueOf(1), value.get());
}
@Test
@ -245,7 +238,7 @@ public class HubConnectionTest {
Subscription subscription = hubConnection.on("inc", action);
Subscription secondSubscription = hubConnection.on("inc", secondAction);
assertEquals(0.0, value.get(), 0);
assertEquals(Double.valueOf(0), value.get());
hubConnection.start();
String message = mockTransport.getSentMessages()[0];
@ -256,12 +249,12 @@ public class HubConnectionTest {
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(3, value.get(), 0);
assertEquals(Double.valueOf(3), value.get());
// This removes the first handler so when "inc" is invoked secondAction should still run.
subscription.unsubscribe();
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
assertEquals(5, value.get(), 0);
assertEquals(Double.valueOf(5), value.get());
}
@Test
@ -274,7 +267,7 @@ public class HubConnectionTest {
Subscription sub = hubConnection.on("inc", action);
sub.unsubscribe();
assertEquals(0.0, value.get(), 0);
assertEquals(Double.valueOf(0), value.get());
hubConnection.start();
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
@ -286,7 +279,7 @@ public class HubConnectionTest {
}
// Confirming that the handler was removed.
assertEquals(0, value.get(), 0);
assertEquals(Double.valueOf(0), value.get());
}
@Test
@ -300,25 +293,24 @@ public class HubConnectionTest {
hubConnection.on("add", action, Double.class);
hubConnection.on("add", action, Double.class);
assertEquals(0, value.get(), 0);
assertEquals(Double.valueOf(0), value.get());
hubConnection.start();
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR);
mockTransport.receiveMessage("{\"type\":1,\"target\":\"add\",\"arguments\":[12]}" + RECORD_SEPARATOR);
hubConnection.send("add", 12);
// Confirming that our handler was called and the correct message was passed in.
assertEquals(24, value.get(), 0);
assertEquals(Double.valueOf(24), value.get());
}
// We're using AtomicReference<Double> in the send tests instead of int here because Gson has trouble deserializing to Integer
@Test
public void sendWithNoParamsTriggersOnHandler() throws Exception {
AtomicReference<Double> value = new AtomicReference<Double>(0.0);
AtomicReference<Integer> value = new AtomicReference<>(0);
MockTransport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
hubConnection.on("inc", () ->{
assertEquals(0.0, value.get(), 0);
assertEquals(Integer.valueOf(0), value.get());
value.getAndUpdate((val) -> val + 1);
});
@ -327,7 +319,7 @@ public class HubConnectionTest {
mockTransport.receiveMessage("{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(1, value.get(), 0);
assertEquals(Integer.valueOf(1), value.get());
}
@Test
@ -373,7 +365,7 @@ public class HubConnectionTest {
// Confirming that our handler was called and the correct message was passed in.
assertEquals("Hello World", value1.get());
assertEquals(12, value2.get(), 0);
assertEquals(Double.valueOf(12), value2.get());
}
@Test
@ -473,7 +465,7 @@ public class HubConnectionTest {
assertEquals("B", value2.get());
assertEquals("C", value3.get());
assertTrue(value4.get());
assertEquals(12, value5.get(), 0);
assertEquals(Double.valueOf(12), value5.get());
}
@Test
@ -513,7 +505,7 @@ public class HubConnectionTest {
assertEquals("B", value2.get());
assertEquals("C", value3.get());
assertTrue(value4.get());
assertEquals(12, value5.get(), 0);
assertEquals(Double.valueOf(12), value5.get());
assertEquals("D", value6.get());
}
@ -557,7 +549,7 @@ public class HubConnectionTest {
assertEquals("B", value2.get());
assertEquals("C", value3.get());
assertTrue(value4.get());
assertEquals(12, value5.get(), 0);
assertEquals(Double.valueOf(12), value5.get());
assertEquals("D", value6.get());
assertEquals("E", value7.get());
}
@ -604,7 +596,7 @@ public class HubConnectionTest {
assertEquals("B", value2.get());
assertEquals("C", value3.get());
assertTrue(value4.get());
assertEquals(12, value5.get(), 0);
assertEquals(Double.valueOf(12), value5.get());
assertEquals("D", value6.get());
assertEquals("E", value7.get());
assertEquals("F", value8.get());
@ -649,7 +641,7 @@ public class HubConnectionTest {
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport, true);
hubConnection.on("inc", () ->{
assertEquals(0.0, value.get(), 0);
assertEquals(Double.valueOf(0), value.get());
value.getAndUpdate((val) -> val + 1);
});
@ -661,7 +653,7 @@ public class HubConnectionTest {
mockTransport.receiveMessage("{}" + RECORD_SEPARATOR + "{\"type\":1,\"target\":\"inc\",\"arguments\":[]}" + RECORD_SEPARATOR);
// Confirming that our handler was called and that the counter property was incremented.
assertEquals(1, value.get(), 0);
assertEquals(Double.valueOf(1), value.get());
}
@Test
@ -740,14 +732,12 @@ public class HubConnectionTest {
@Test
public void cannotSendBeforeStart() throws Exception {
exceptionRule.expect(HubException.class);
exceptionRule.expectMessage("The 'send' method cannot be called if the connection is not active");
Transport mockTransport = new MockTransport();
HubConnection hubConnection = new HubConnection("http://example.com", mockTransport);
assertEquals(HubConnectionState.DISCONNECTED, hubConnection.getConnectionState());
hubConnection.send("inc");
Throwable exception = assertThrows(HubException.class, () -> hubConnection.send("inc"));
assertEquals("The 'send' method cannot be called if the connection is not active", exception.getMessage());
}
private class MockTransport implements Transport {

View File

@ -3,9 +3,9 @@
package com.microsoft.aspnet.signalr;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class HubExceptionTest {
@Test

View File

@ -3,15 +3,13 @@
package com.microsoft.aspnet.signalr;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.Test;
public class JsonHubProtocolTest {
@ -110,47 +108,40 @@ public class JsonHubProtocolTest {
assertEquals(42, messageResult);
}
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@Test
public void parseSingleUnsupportedStreamItemMessage() throws Exception {
exceptionRule.expect(UnsupportedOperationException.class);
exceptionRule.expectMessage("The message type STREAM_ITEM is not supported yet.");
String stringifiedMessage = "{\"type\":2,\"Id\":1,\"Item\":42}\u001E";
TestBinder binder = new TestBinder(null);
HubMessage[] messages = jsonHubProtocol.parseMessages(stringifiedMessage, binder);
Throwable exception = assertThrows(UnsupportedOperationException.class, () -> jsonHubProtocol.parseMessages(stringifiedMessage, binder));
assertEquals("The message type STREAM_ITEM is not supported yet.", exception.getMessage());
}
@Test
public void parseSingleUnsupportedStreamInvocationMessage() throws Exception {
exceptionRule.expect(UnsupportedOperationException.class);
exceptionRule.expectMessage("The message type STREAM_INVOCATION is not supported yet.");
String stringifiedMessage = "{\"type\":4,\"Id\":1,\"target\":\"test\",\"arguments\":[42]}\u001E";
TestBinder binder = new TestBinder(new StreamInvocationMessage("1", "test", new Object[] { 42 }));
HubMessage[] messages = jsonHubProtocol.parseMessages(stringifiedMessage, binder);
Throwable exception = assertThrows(UnsupportedOperationException.class, () -> jsonHubProtocol.parseMessages(stringifiedMessage, binder));
assertEquals("The message type STREAM_INVOCATION is not supported yet.", exception.getMessage());
}
@Test
public void parseSingleUnsupportedCancelInvocationMessage() throws Exception {
exceptionRule.expect(UnsupportedOperationException.class);
exceptionRule.expectMessage("The message type CANCEL_INVOCATION is not supported yet.");
String stringifiedMessage = "{\"type\":5,\"invocationId\":123}\u001E";
TestBinder binder = new TestBinder(null);
HubMessage[] messages = jsonHubProtocol.parseMessages(stringifiedMessage, binder);
Throwable exception = assertThrows(UnsupportedOperationException.class, () -> jsonHubProtocol.parseMessages(stringifiedMessage, binder));
assertEquals("The message type CANCEL_INVOCATION is not supported yet.", exception.getMessage());
}
@Test
public void parseSingleUnsupportedCompletionMessage() throws Exception {
exceptionRule.expect(UnsupportedOperationException.class);
exceptionRule.expectMessage("The message type COMPLETION is not supported yet.");
String stringifiedMessage = "{\"type\":3,\"invocationId\":123}\u001E";
TestBinder binder = new TestBinder(null);
HubMessage[] messages = jsonHubProtocol.parseMessages(stringifiedMessage, binder);
Throwable exception = assertThrows(UnsupportedOperationException.class, () -> jsonHubProtocol.parseMessages(stringifiedMessage, binder));
assertEquals("The message type COMPLETION is not supported yet.", exception.getMessage());
}
@Test

View File

@ -3,9 +3,9 @@
package com.microsoft.aspnet.signalr;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class NegotiateResponseTest {

View File

@ -3,39 +3,28 @@
package com.microsoft.aspnet.signalr;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@RunWith(Parameterized.class)
public class ResolveNegotiateUrlTest {
private String url;
private String resolvedUrl;
public ResolveNegotiateUrlTest(String url, String resolvedUrl) {
this.url = url;
this.resolvedUrl = resolvedUrl;
private static Stream<Arguments> protocols() {
return Stream.of(
Arguments.of("http://example.com/hub/", "http://example.com/hub/negotiate"),
Arguments.of("http://example.com/hub", "http://example.com/hub/negotiate"),
Arguments.of("http://example.com/endpoint?q=my/Data", "http://example.com/endpoint/negotiate?q=my/Data"),
Arguments.of("http://example.com/endpoint/?q=my/Data", "http://example.com/endpoint/negotiate?q=my/Data"),
Arguments.of("http://example.com/endpoint/path/more?q=my/Data", "http://example.com/endpoint/path/more/negotiate?q=my/Data"));
}
@Parameterized.Parameters
public static Collection protocols() {
return Arrays.asList(new String[][]{
{"http://example.com/hub/", "http://example.com/hub/negotiate"},
{"http://example.com/hub", "http://example.com/hub/negotiate"},
{"http://example.com/endpoint?q=my/Data", "http://example.com/endpoint/negotiate?q=my/Data"},
{"http://example.com/endpoint/?q=my/Data", "http://example.com/endpoint/negotiate?q=my/Data"},
{"http://example.com/endpoint/path/more?q=my/Data", "http://example.com/endpoint/path/more/negotiate?q=my/Data"},});
}
@Test
public void checkNegotiateUrl() {
String urlResult = Negotiate.resolveNegotiateUrl(this.url);
assertEquals(this.resolvedUrl, urlResult);
@ParameterizedTest
@MethodSource("protocols")
public void checkNegotiateUrl(String url, String resolvedUrl) {
String urlResult = Negotiate.resolveNegotiateUrl(url);
assertEquals(resolvedUrl, urlResult);
}
}

View File

@ -3,20 +3,14 @@
package com.microsoft.aspnet.signalr;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class WebSocketTransportTest {
@Rule
public ExpectedException expectedEx = ExpectedException.none();
@Test
public void WebsocketThrowsIfItCantConnect() throws Exception {
expectedEx.expect(Exception.class);
expectedEx.expectMessage("There was an error starting the Websockets transport");
Transport transport = new WebSocketTransport("www.notarealurl12345.fake", new NullLogger());
transport.start();
Throwable exception = assertThrows(Exception.class, () -> transport.start());
assertEquals("There was an error starting the Websockets transport.", exception.getMessage());
}
}

View File

@ -3,39 +3,28 @@
package com.microsoft.aspnet.signalr;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@RunWith(Parameterized.class)
public class WebSocketTransportUrlFormatTest {
private String url;
private String expectedUrl;
public WebSocketTransportUrlFormatTest(String url, String expectedProtocol) {
this.url = url;
this.expectedUrl = expectedProtocol;
private static Stream<Arguments> protocols() {
return Stream.of(
Arguments.of("http://example.com", "ws://example.com"),
Arguments.of("https://example.com", "wss://example.com"),
Arguments.of("ws://example.com", "ws://example.com"),
Arguments.of("wss://example.com", "wss://example.com"));
}
@Parameterized.Parameters
public static Collection protocols() {
return Arrays.asList(new String[][]{
{"http://example.com", "ws://example.com"},
{"https://example.com", "wss://example.com"},
{"ws://example.com", "ws://example.com"},
{"wss://example.com", "wss://example.com"}});
}
@Test
public void checkWebsocketUrlProtocol() throws URISyntaxException {
WebSocketTransport webSocketTransport = new WebSocketTransport(this.url, new NullLogger());
assertEquals(this.expectedUrl, webSocketTransport.getUrl().toString());
@ParameterizedTest
@MethodSource("protocols")
public void checkWebsocketUrlProtocol(String url, String expectedUrl) throws URISyntaxException {
WebSocketTransport webSocketTransport = new WebSocketTransport(url, new NullLogger());
assertEquals(expectedUrl, webSocketTransport.getUrl().toString());
}
}