Skip to content

Commit

Permalink
Upgrade all tests to JUnit 5
Browse files Browse the repository at this point in the history
JUnit 4 has been removed from the project and all tests updated to use
JUnit 5. Where possible calls to Mockito's `mock` has been altered to
use the `no-args` version, and ArgumentCaptor's `forClass` replaced with
`captor` to reduce verbosity.
  • Loading branch information
mc1arke committed Nov 17, 2024
1 parent 419f59f commit 69f747f
Show file tree
Hide file tree
Showing 61 changed files with 1,157 additions and 1,213 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ dependencies {
implementation('com.squareup.okhttp3:logging-interceptor:4.12.0')
testImplementation(platform('org.junit:junit-bom:5.11.3'))
testImplementation('org.junit.jupiter:junit-jupiter')
testImplementation('junit:junit:4.13.2')
testRuntimeOnly('org.junit.vintage:junit-vintage-engine')
}

sourceSets.test.runtimeClasspath = configurations.customTestRuntime + sourceSets.test.runtimeClasspath
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void shouldRedefinePluginBootstrapIsAvailableForWebLaunch() throws ReflectiveOpe

CommunityBranchAgent.premain("web", instrumentation);

ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.forClass(ClassFileTransformer.class);
ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.captor();
verify(instrumentation).retransformClasses(MultipleAlmFeature.class);
verify(instrumentation, times(4)).addTransformer(classFileTransformerArgumentCaptor.capture());

Expand All @@ -92,7 +92,7 @@ void shouldRedefineMultipleAlmFeatureClassForWebLaunch() throws ReflectiveOperat

CommunityBranchAgent.premain("web", instrumentation);

ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.forClass(ClassFileTransformer.class);
ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.captor();
verify(instrumentation).retransformClasses(MultipleAlmFeature.class);
verify(instrumentation, times(4)).addTransformer(classFileTransformerArgumentCaptor.capture());

Expand All @@ -117,7 +117,7 @@ void shouldRedefineSetActionClassForWebLaunch() throws ReflectiveOperationExcept

CommunityBranchAgent.premain("web", instrumentation);

ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.forClass(ClassFileTransformer.class);
ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.captor();
verify(instrumentation).retransformClasses(SetAction.class);
verify(instrumentation, times(4)).addTransformer(classFileTransformerArgumentCaptor.capture());

Expand Down Expand Up @@ -150,7 +150,7 @@ void shouldRedefinesUnsetActionClassForWebLaunch() throws IOException, Unmodifia
CommunityBranchAgent.premain("web", instrumentation);
DocumentationLinkGenerator documentationLinkGenerator = mock();

ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.forClass(ClassFileTransformer.class);
ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.captor();
verify(instrumentation).retransformClasses(UnsetAction.class);
verify(instrumentation, times(4)).addTransformer(classFileTransformerArgumentCaptor.capture());

Expand Down Expand Up @@ -180,7 +180,7 @@ void shouldSkipNonTargetClasForWebLaunch() throws UnmodifiableClassException, Cl

CommunityBranchAgent.premain("web", instrumentation);

ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.forClass(ClassFileTransformer.class);
ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.captor();
verify(instrumentation).retransformClasses(MultipleAlmFeature.class);
verify(instrumentation, times(4)).addTransformer(classFileTransformerArgumentCaptor.capture());

Expand All @@ -196,7 +196,7 @@ void shouldSkipNonTargetClassForCeLunch() throws UnmodifiableClassException, Cla

CommunityBranchAgent.premain("ce", instrumentation);

ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.forClass(ClassFileTransformer.class);
ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.captor();
verify(instrumentation).retransformClasses(PlatformEditionProvider.class);
verify(instrumentation, times(3)).addTransformer(classFileTransformerArgumentCaptor.capture());

Expand All @@ -212,7 +212,7 @@ void shouldRedefineTargetClassesForCeLaunch() throws ReflectiveOperationExceptio

CommunityBranchAgent.premain("ce", instrumentation);

ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.forClass(ClassFileTransformer.class);
ArgumentCaptor<ClassFileTransformer> classFileTransformerArgumentCaptor = ArgumentCaptor.captor();
verify(instrumentation).retransformClasses(MultipleAlmFeature.class);
verify(instrumentation).retransformClasses(PlatformEditionProvider.class);
verify(instrumentation, times(3)).addTransformer(classFileTransformerArgumentCaptor.capture());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* Copyright (C) 2021-2024 Michael Clarke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package com.github.mc1arke.sonarqube.plugin.almclient.azuredevops;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -29,27 +47,27 @@

class AzureDevopsRestClientTest {

private final ObjectMapper objectMapper = mock(ObjectMapper.class);
private final CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
private final ObjectMapper objectMapper = mock();
private final CloseableHttpClient closeableHttpClient = mock();

@Test
void checkErrorThrownOnNonSuccessResponseStatus() throws IOException {
AzureDevopsRestClient underTest = new AzureDevopsRestClient("http://url.test/api", "token", objectMapper, () -> closeableHttpClient);

CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
CloseableHttpResponse closeableHttpResponse = mock();
StatusLine statusLine = mock();
when(statusLine.getStatusCode()).thenReturn(500);
when(closeableHttpResponse.getStatusLine()).thenReturn(statusLine);
when(closeableHttpClient.execute(any())).thenReturn(closeableHttpResponse);
when(objectMapper.writeValueAsString(any())).thenReturn("json");

GitPullRequestStatus gitPullRequestStatus = mock(GitPullRequestStatus.class);
GitPullRequestStatus gitPullRequestStatus = mock();
assertThatThrownBy(() -> underTest.submitPullRequestStatus("project", "repo", 101, gitPullRequestStatus))
.isExactlyInstanceOf(IllegalStateException.class)
.hasMessage("An unexpected response code was returned from the Azure Devops API - Expected: 200, Got: 500")
.hasNoCause();

ArgumentCaptor<HttpUriRequest> requestArgumentCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);
ArgumentCaptor<HttpUriRequest> requestArgumentCaptor = ArgumentCaptor.captor();
verify(closeableHttpClient).execute(requestArgumentCaptor.capture());

RequestBuilder request = RequestBuilder.copy(requestArgumentCaptor.getValue());
Expand All @@ -63,16 +81,16 @@ void checkErrorThrownOnNonSuccessResponseStatus() throws IOException {
void checkSubmitPullRequestStatusSubmitsCorrectContent() throws IOException {
AzureDevopsRestClient underTest = new AzureDevopsRestClient("http://url.test/api", "token", objectMapper, () -> closeableHttpClient);

CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
CloseableHttpResponse closeableHttpResponse = mock();
StatusLine statusLine = mock();
when(statusLine.getStatusCode()).thenReturn(200);
when(closeableHttpResponse.getStatusLine()).thenReturn(statusLine);
when(closeableHttpClient.execute(any())).thenReturn(closeableHttpResponse);
when(objectMapper.writeValueAsString(any())).thenReturn("json");

underTest.submitPullRequestStatus("project Id With Spaces", "repository Name With Spaces", 123, new GitPullRequestStatus(GitStatusState.SUCCEEDED, "description", new GitStatusContext("name", "genre"), "url"));

ArgumentCaptor<HttpUriRequest> requestArgumentCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);
ArgumentCaptor<HttpUriRequest> requestArgumentCaptor = ArgumentCaptor.captor();
verify(closeableHttpClient).execute(requestArgumentCaptor.capture());

RequestBuilder request = RequestBuilder.copy(requestArgumentCaptor.getValue());
Expand All @@ -86,16 +104,16 @@ void checkSubmitPullRequestStatusSubmitsCorrectContent() throws IOException {
void checkAddCommentToThreadSubmitsCorrectContent() throws IOException {
AzureDevopsRestClient underTest = new AzureDevopsRestClient("http://test.url", "authToken", objectMapper, () -> closeableHttpClient);

CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
CloseableHttpResponse closeableHttpResponse = mock();
StatusLine statusLine = mock();
when(statusLine.getStatusCode()).thenReturn(200);
when(closeableHttpResponse.getStatusLine()).thenReturn(statusLine);
when(closeableHttpClient.execute(any())).thenReturn(closeableHttpResponse);
when(objectMapper.writeValueAsString(any())).thenReturn("json");

underTest.addCommentToThread("projectId", "repository Name", 123, 321, new CreateCommentRequest("comment"));

ArgumentCaptor<HttpUriRequest> requestArgumentCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);
ArgumentCaptor<HttpUriRequest> requestArgumentCaptor = ArgumentCaptor.captor();
verify(closeableHttpClient).execute(requestArgumentCaptor.capture());

RequestBuilder request = RequestBuilder.copy(requestArgumentCaptor.getValue());
Expand All @@ -109,18 +127,18 @@ void checkAddCommentToThreadSubmitsCorrectContent() throws IOException {
void checkRetrievePullRequestReturnsCorrectContent() throws IOException {
AzureDevopsRestClient underTest = new AzureDevopsRestClient("http://test.url", "authToken", objectMapper, () -> closeableHttpClient);

CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
CloseableHttpResponse closeableHttpResponse = mock();
StatusLine statusLine = mock();
when(statusLine.getStatusCode()).thenReturn(200);
when(closeableHttpResponse.getStatusLine()).thenReturn(statusLine);
when(closeableHttpResponse.getEntity()).thenReturn(new StringEntity("content", StandardCharsets.UTF_8));
when(closeableHttpClient.execute(any())).thenReturn(closeableHttpResponse);
PullRequest pullRequest = mock(PullRequest.class);
PullRequest pullRequest = mock();
when(objectMapper.readValue(any(String.class), eq(PullRequest.class))).thenReturn(pullRequest);

PullRequest result = underTest.retrievePullRequest("projectId", "repository Name", 123);

ArgumentCaptor<HttpUriRequest> requestArgumentCaptor = ArgumentCaptor.forClass(HttpUriRequest.class);
ArgumentCaptor<HttpUriRequest> requestArgumentCaptor = ArgumentCaptor.captor();
verify(closeableHttpClient).execute(requestArgumentCaptor.capture());

RequestBuilder request = RequestBuilder.copy(requestArgumentCaptor.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ void setup() {
@Test
void testUploadReport() throws IOException {
// given
CodeInsightsReport report = mock(CodeInsightsReport.class);
Call call = mock(Call.class);
Response response = mock(Response.class);
ArgumentCaptor<Request> captor = ArgumentCaptor.forClass(Request.class);
CodeInsightsReport report = mock();
Call call = mock();
Response response = mock();
ArgumentCaptor<Request> captor = ArgumentCaptor.captor();

when(client.newCall(any())).thenReturn(call);
when(call.execute()).thenReturn(response);
Expand All @@ -95,9 +95,9 @@ void testUploadReport() throws IOException {
@Test
void testDeleteReport() throws IOException {
// given
Call call = mock(Call.class);
Response response = mock(Response.class);
ArgumentCaptor<Request> captor = ArgumentCaptor.forClass(Request.class);
Call call = mock();
Response response = mock();
ArgumentCaptor<Request> captor = ArgumentCaptor.captor();

when(client.newCall(any())).thenReturn(call);
when(call.execute()).thenReturn(response);
Expand All @@ -115,11 +115,11 @@ void testDeleteReport() throws IOException {
@Test
void testUploadAnnotations() throws IOException {
// given
CodeInsightsAnnotation annotation = mock(CloudAnnotation.class);
CloudAnnotation annotation = mock();
Set<CodeInsightsAnnotation> annotations = Sets.newHashSet(annotation);
Call call = mock(Call.class);
Response response = mock(Response.class);
ArgumentCaptor<Request> captor = ArgumentCaptor.forClass(Request.class);
Call call = mock();
Response response = mock();
ArgumentCaptor<Request> captor = ArgumentCaptor.captor();

when(client.newCall(any())).thenReturn(call);
when(call.execute()).thenReturn(response);
Expand Down Expand Up @@ -151,10 +151,10 @@ void testUploadLimit() {
@Test
void testUploadReportFailsWithMessage() throws IOException {
// given
CodeInsightsReport report = mock(CodeInsightsReport.class);
Call call = mock(Call.class);
Response response = mock(Response.class);
ResponseBody responseBody = mock(ResponseBody.class);
CodeInsightsReport report = mock();
Call call = mock();
Response response = mock();
ResponseBody responseBody = mock();

when(client.newCall(any())).thenReturn(call);
when(call.execute()).thenReturn(response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020-2021 Michael Clarke
* Copyright (C) 2020-2024 Michael Clarke
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand All @@ -18,15 +18,16 @@
*/
package com.github.mc1arke.sonarqube.plugin.almclient.bitbucket;

import com.github.mc1arke.sonarqube.plugin.almclient.bitbucket.model.server.ErrorResponse;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class BitbucketExceptionTest {
import org.junit.jupiter.api.Test;

import com.github.mc1arke.sonarqube.plugin.almclient.bitbucket.model.server.ErrorResponse;

class BitbucketExceptionTest {

@Test
public void verifyMessageReturnedWhenErrorResponseContainsNoErrors() {
void verifyMessageReturnedWhenErrorResponseContainsNoErrors() {
BitbucketException testCase = new BitbucketException(401, new ErrorResponse(null));
assertThat(testCase.getMessage()).isEqualTo("Bitbucket responded with an error status (401)");
}
Expand Down
Loading

0 comments on commit 69f747f

Please sign in to comment.