diff options
author | Klaudio Sinani <klaudio.sinani@sonarsource.com> | 2021-11-17 22:54:06 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-11-19 20:03:27 +0000 |
commit | a3d88ea27c35921647d7602755828ca73e15e865 (patch) | |
tree | 5626c38afab1ea00ab9897da431476c17b478bbe /sonar-ws | |
parent | 92f482f2aa43e4aa36e0fda377d13b9dc3282ff9 (diff) | |
download | sonarqube-a3d88ea27c35921647d7602755828ca73e15e865.tar.gz sonarqube-a3d88ea27c35921647d7602755828ca73e15e865.zip |
SONAR-15631 - Refactor UTs to stop using ExpectedException
Diffstat (limited to 'sonar-ws')
3 files changed, 36 insertions, 44 deletions
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java index 01cf250ef94..57bc699c994 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/BaseRequestTest.java @@ -22,21 +22,17 @@ package org.sonarqube.ws.client; import java.util.Arrays; import java.util.List; import org.assertj.core.data.MapEntry; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.sonarqube.ws.MediaTypes; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.data.MapEntry.entry; public class BaseRequestTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); - private FakeRequest underTest = new FakeRequest("api/foo"); @Test @@ -99,8 +95,8 @@ public class BaseRequestTest { @Test public void fail_if_null_param_key() { - expectedException.expect(IllegalArgumentException.class); - underTest.setParam(null, "val"); + assertThatThrownBy(() -> underTest.setParam(null, "val")) + .isInstanceOf(IllegalArgumentException.class); } @Test diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java index b95f9a8afb5..2cc8ba82f83 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/HttpConnectorTest.java @@ -38,18 +38,17 @@ import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.StringUtils; -import org.hamcrest.core.IsInstanceOf; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonarqube.ws.MediaTypes; import static java.nio.charset.StandardCharsets.UTF_8; import static okhttp3.Credentials.basic; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.sonarqube.ws.client.HttpConnector.newBuilder; @@ -58,8 +57,6 @@ public class HttpConnectorTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - @Rule - public ExpectedException expectedException = ExpectedException.none(); private MockWebServer server; private String serverUrl; @@ -326,10 +323,9 @@ public class HttpConnectorTest { @Test public void fail_if_malformed_URL() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Malformed URL: 'wrong URL'"); - - underTest = newBuilder().url("wrong URL").build(); + assertThatThrownBy(() -> newBuilder().url("wrong URL").build()) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Malformed URL: 'wrong URL'"); } @Test @@ -443,23 +439,26 @@ public class HttpConnectorTest { underTest = HttpConnector.newBuilder().url(serverUrl).build(); server.enqueue(new MockResponse().setBodyDelay(100, TimeUnit.MILLISECONDS).setBody("Hello delayed")); - expectedException.expect(IllegalStateException.class); - expectedException.expectCause(IsInstanceOf.instanceOf(SocketTimeoutException.class)); - - WsResponse call = underTest.call(new GetRequest("/").setTimeOutInMs(5)); - assertThat(call.content()).equals("Hello delayed"); + assertThatThrownBy(() -> { + WsResponse call = underTest.call(new GetRequest("/").setTimeOutInMs(5)); + assertThat(call.content()).equals("Hello delayed"); + }) + .isInstanceOf(IllegalStateException.class) + .hasCauseInstanceOf(SocketTimeoutException.class); } @Test public void override_timeout_on_post() { underTest = HttpConnector.newBuilder().url(serverUrl).build(); // Headers are not affected by setBodyDelay, let's throttle the answer - server.enqueue(new MockResponse().throttleBody(1,100, TimeUnit.MILLISECONDS).setBody("Hello delayed")); - - expectedException.expect(IllegalStateException.class); - expectedException.expectCause(IsInstanceOf.instanceOf(SocketTimeoutException.class)); - WsResponse call = underTest.call(new PostRequest("/").setTimeOutInMs(5)); - assertThat(call.content()).equals("Hello delayed"); + server.enqueue(new MockResponse().throttleBody(1, 100, TimeUnit.MILLISECONDS).setBody("Hello delayed")); + + assertThatThrownBy(() -> { + WsResponse call = underTest.call(new PostRequest("/").setTimeOutInMs(5)); + assertThat(call.content()).equals("Hello delayed"); + }) + .isInstanceOf(IllegalStateException.class) + .hasCauseInstanceOf(SocketTimeoutException.class); } @Test @@ -467,12 +466,14 @@ public class HttpConnectorTest { underTest = HttpConnector.newBuilder().url(serverUrl).build(); server.enqueue(new MockResponse().setResponseCode(301).setHeader("Location:", "/redirect")); // Headers are not affected by setBodyDelay, let's throttle the answer - server.enqueue(new MockResponse().throttleBody(1,100, TimeUnit.MILLISECONDS).setBody("Hello delayed")); - - expectedException.expect(IllegalStateException.class); - expectedException.expectCause(IsInstanceOf.instanceOf(SocketTimeoutException.class)); - WsResponse call = underTest.call(new PostRequest("/").setTimeOutInMs(5)); - assertThat(call.content()).equals("Hello delayed"); + server.enqueue(new MockResponse().throttleBody(1, 100, TimeUnit.MILLISECONDS).setBody("Hello delayed")); + + assertThatThrownBy(() -> { + WsResponse call = underTest.call(new PostRequest("/").setTimeOutInMs(5)); + assertThat(call.content()).equals("Hello delayed"); + }) + .isInstanceOf(IllegalStateException.class) + .hasCauseInstanceOf(SocketTimeoutException.class); } private void assertTlsAndClearTextSpecifications(HttpConnector underTest) { diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java index 047cfbfc205..850b9eb45fc 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/OkHttpClientBuilderTest.java @@ -21,17 +21,14 @@ package org.sonarqube.ws.client; import javax.net.ssl.SSLSocketFactory; import okhttp3.OkHttpClient; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.mock; public class OkHttpClientBuilderTest { - @Rule - public ExpectedException expectedException = ExpectedException.none(); private OkHttpClientBuilder underTest = new OkHttpClientBuilder(); @@ -75,17 +72,15 @@ public class OkHttpClientBuilderTest { @Test public void build_throws_IAE_if_connect_timeout_is_negative() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Connect timeout must be positive. Got -10"); - - underTest.setConnectTimeoutMs(-10); + assertThatThrownBy(() -> underTest.setConnectTimeoutMs(-10)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Connect timeout must be positive. Got -10"); } @Test public void build_throws_IAE_if_read_timeout_is_negative() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Read timeout must be positive. Got -10"); - - underTest.setReadTimeoutMs(-10); + assertThatThrownBy(() -> underTest.setReadTimeoutMs(-10)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Read timeout must be positive. Got -10"); } } |