Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate tests to JUnit5 #634

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>5.6</version>
<version>5.7</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -328,6 +328,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.atlassian.httpclient.apache.httpcomponents;

import static org.junit.jupiter.api.Assertions.assertEquals;

import com.atlassian.httpclient.api.Response;
import com.atlassian.httpclient.api.factory.HttpClientOptions;
import com.atlassian.sal.api.ApplicationProperties;
Expand Down Expand Up @@ -28,17 +30,14 @@
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.Callback;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

@WithJenkins
public class ApacheAsyncHttpClientTest {

@Rule
public JenkinsRule j = new JenkinsRule();

private final ConnectionFactory connectionFactory = new HttpConnectionFactory();

private Server server;
Expand All @@ -55,15 +54,15 @@ public void prepare(Handler handler) throws Exception {
server.start();
}

@After
public void dispose() throws Exception {
@AfterEach
void dispose() throws Exception {
if (server != null) {
server.stop();
}
}

@Test
public void simple_get() throws Exception {
void simple_get(JenkinsRule r) throws Exception {
TestHandler testHandler = new TestHandler();
prepare(testHandler);

Expand All @@ -74,12 +73,12 @@ public void simple_get() throws Exception {
.newRequest("http://localhost:" + connector.getLocalPort() + "/foo")
.get()
.get(10, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatusCode());
Assert.assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
assertEquals(200, response.getStatusCode());
assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
}

@Test
public void simple_post() throws Exception {
void simple_post(JenkinsRule r) throws Exception {
TestHandler testHandler = new TestHandler();
prepare(testHandler);

Expand All @@ -92,13 +91,13 @@ public void simple_post() throws Exception {
.setContentType("text")
.post()
.get(10, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatusCode());
Assert.assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
Assert.assertEquals("FOO", testHandler.postReceived);
assertEquals(200, response.getStatusCode());
assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
assertEquals("FOO", testHandler.postReceived);
}

@Test
public void simple_get_with_non_proxy_host() throws Exception {
void simple_get_with_non_proxy_host(JenkinsRule r) throws Exception {
ProxyTestHandler testHandler = new ProxyTestHandler();
prepare(testHandler);

Expand All @@ -109,12 +108,12 @@ public void simple_get_with_non_proxy_host() throws Exception {
null, buildApplicationProperties(), new NoOpThreadLocalContextManager(), new HttpClientOptions());

Response response = httpClient.newRequest("http://www.apache.org").get().get(30, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatusCode());
// Assert.assertEquals( CONTENT_RESPONSE, IOUtils.toString( response.getEntityStream() ) );
assertEquals(200, response.getStatusCode());
// assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
}

@Test
public void simple_get_with_proxy() throws Exception {
void simple_get_with_proxy(JenkinsRule r) throws Exception {
ProxyTestHandler testHandler = new ProxyTestHandler();
prepare(testHandler);

Expand All @@ -124,12 +123,12 @@ public void simple_get_with_proxy() throws Exception {
null, buildApplicationProperties(), new NoOpThreadLocalContextManager(), new HttpClientOptions());

Response response = httpClient.newRequest("http://jenkins.io").get().get(30, TimeUnit.SECONDS);
Assert.assertEquals(200, response.getStatusCode());
Assert.assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
assertEquals(200, response.getStatusCode());
assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
}

@Test
public void simple_post_with_proxy() throws Exception {
void simple_post_with_proxy(JenkinsRule r) throws Exception {
ProxyTestHandler testHandler = new ProxyTestHandler();
prepare(testHandler);

Expand All @@ -145,9 +144,9 @@ public void simple_post_with_proxy() throws Exception {
.post()
.get(30, TimeUnit.SECONDS);
// we are sure to hit the proxy first :-)
Assert.assertEquals(200, response.getStatusCode());
Assert.assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
Assert.assertEquals("FOO", testHandler.postReceived);
assertEquals(200, response.getStatusCode());
assertEquals(CONTENT_RESPONSE, IOUtils.toString(response.getEntityStream()));
assertEquals("FOO", testHandler.postReceived);
}

public static class ProxyTestHandler extends Handler.Abstract {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.protocol.HttpContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.stubbing.Answer;

@RunWith(MockitoJUnitRunner.class)
public class CompletableFuturePromiseHttpPromiseAsyncClientTest {
@ExtendWith(MockitoExtension.class)
class CompletableFuturePromiseHttpPromiseAsyncClientTest {

@Mock
private CloseableHttpAsyncClient client;
Expand All @@ -47,7 +47,7 @@ public class CompletableFuturePromiseHttpPromiseAsyncClientTest {
private CompletableFuturePromiseHttpPromiseAsyncClient<Object> asyncClient;

@Test
public void ensureCloseHttpclientOnCompletion() throws IOException {
void ensureCloseHttpclientOnCompletion() throws IOException {
when(client.execute(eq(request), eq(context), any())).then((Answer<Future<HttpResponse>>) invocation -> {
invocation.getArgument(2, FutureCallback.class).completed(response);
return mock(Future.class);
Expand All @@ -59,7 +59,7 @@ public void ensureCloseHttpclientOnCompletion() throws IOException {
}

@Test
public void ensureCloseHttpclientOnFailure() throws IOException {
void ensureCloseHttpclientOnFailure() throws IOException {
when(client.execute(eq(request), eq(context), any())).then((Answer<Future<HttpResponse>>) invocation -> {
invocation.getArgument(2, FutureCallback.class).failed(null);
return mock(Future.class);
Expand All @@ -71,7 +71,7 @@ public void ensureCloseHttpclientOnFailure() throws IOException {
}

@Test
public void ensureCloseHttpclientOnCancellation() throws IOException {
void ensureCloseHttpclientOnCancellation() throws IOException {
when(client.execute(eq(request), eq(context), any())).then((Answer<Future<HttpResponse>>) invocation -> {
invocation.getArgument(2, FutureCallback.class).cancelled();
return mock(Future.class);
Expand Down
30 changes: 15 additions & 15 deletions src/test/java/hudson/plugins/jira/ChangingWorkflowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.concurrent.TimeoutException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

/**
* User: lanwen
* Date: 10.09.13
* Time: 0:57
*/
@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class ChangingWorkflowTest {

public static final String NON_EMPTY_COMMENT = "Non empty comment";
Expand All @@ -57,25 +57,25 @@ public class ChangingWorkflowTest {

private JiraSession spySession;

@Before
public void setupSpy() {
@BeforeEach
void setupSpy() {
spySession = spy(new JiraSession(site, restService));
}

@Test
public void onGetActionItInvokesServiceMethod() {
void onGetActionItInvokesServiceMethod() {
spySession.getActionIdForIssue(ISSUE_JQL, NON_EMPTY_WORKFLOW_LOWERCASE);
verify(restService, times(1)).getAvailableActions(eq(ISSUE_JQL));
}

@Test
public void getActionIdReturnsNullWhenServiceReturnsNull() {
void getActionIdReturnsNullWhenServiceReturnsNull() {
doReturn(null).when(restService).getAvailableActions(ISSUE_JQL);
assertThat(spySession.getActionIdForIssue(ISSUE_JQL, NON_EMPTY_WORKFLOW_LOWERCASE), nullValue());
}

@Test
public void getActionIdIteratesOverAllActionsEvenOneOfNamesIsNull() {
void getActionIdIteratesOverAllActionsEvenOneOfNamesIsNull() {
Transition action1 = mock(Transition.class);
Transition action2 = mock(Transition.class);

Expand All @@ -90,7 +90,7 @@ public void getActionIdIteratesOverAllActionsEvenOneOfNamesIsNull() {
}

@Test
public void getActionIdReturnsNullWhenNullWorkflowUsed() {
void getActionIdReturnsNullWhenNullWorkflowUsed() {
String workflowAction = null;
Transition action1 = mock(Transition.class);
when(action1.getName()).thenReturn("name");
Expand All @@ -100,7 +100,7 @@ public void getActionIdReturnsNullWhenNullWorkflowUsed() {
}

@Test
public void getActionIdReturnsIdWhenFoundIgnorecaseWorkflow() {
void getActionIdReturnsIdWhenFoundIgnorecaseWorkflow() {
String id = randomNumeric(5);
Transition action1 = mock(Transition.class);
when(action1.getName()).thenReturn(NON_EMPTY_WORKFLOW_LOWERCASE.toUpperCase());
Expand All @@ -112,7 +112,7 @@ public void getActionIdReturnsIdWhenFoundIgnorecaseWorkflow() {
}

@Test
public void addCommentsOnNonEmptyWorkflowAndNonEmptyComment() throws Exception {
void addCommentsOnNonEmptyWorkflowAndNonEmptyComment() throws Exception {
when(site.getSession(any(), anyBoolean())).thenCallRealMethod();
when(site.getSession(any())).thenCallRealMethod();
when(site.createSession(any(), anyBoolean())).thenReturn(mockSession);
Expand All @@ -130,7 +130,7 @@ public void addCommentsOnNonEmptyWorkflowAndNonEmptyComment() throws Exception {
}

@Test
public void addCommentsOnNullWorkflowAndNonEmptyComment() throws Exception {
void addCommentsOnNullWorkflowAndNonEmptyComment() throws Exception {
when(site.getSession(any())).thenCallRealMethod();
when(site.getSession(any(), anyBoolean())).thenCallRealMethod();
when(site.createSession(any(), anyBoolean())).thenReturn(mockSession);
Expand All @@ -146,7 +146,7 @@ public void addCommentsOnNullWorkflowAndNonEmptyComment() throws Exception {
}

@Test
public void dontAddCommentsOnNullWorkflowAndNullComment() throws TimeoutException {
void dontAddCommentsOnNullWorkflowAndNullComment() throws TimeoutException {
site.progressMatchingIssues(ISSUE_JQL, null, null, mock(PrintStream.class));
verify(mockSession, never()).addComment(anyString(), anyString(), isNull(), isNull());
}
Expand Down
21 changes: 11 additions & 10 deletions src/test/java/hudson/plugins/jira/CliParameterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@
import hudson.plugins.jira.listissuesparameter.JiraIssueParameterDefinition;
import hudson.plugins.jira.versionparameter.JiraVersionParameterDefinition;
import java.io.IOException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

public class CliParameterTest {
@WithJenkins
class CliParameterTest {

@Rule
public JenkinsRule jenkins = new JenkinsRule();
private JenkinsRule jenkins;

FreeStyleProject project;

@Before
public void setup() throws IOException {
@BeforeEach
void setup(JenkinsRule jenkins) throws IOException {
this.jenkins = jenkins;
project = jenkins.createFreeStyleProject();
}

@Test
public void jiraIssueParameterViaCli() throws Exception {
void jiraIssueParameterViaCli() throws Exception {
project.addProperty(new ParametersDefinitionProperty(
new JiraIssueParameterDefinition("jiraissue", "description", "filter")));

Expand All @@ -38,7 +39,7 @@ public void jiraIssueParameterViaCli() throws Exception {
}

@Test
public void jiraVersionParameterViaCli() throws Exception {
void jiraVersionParameterViaCli() throws Exception {
project.addProperty(new ParametersDefinitionProperty(
new JiraVersionParameterDefinition("jiraversion", "description", "PROJ", "RELEASE", "true", "false")));

Expand Down
23 changes: 10 additions & 13 deletions src/test/java/hudson/plugins/jira/ConfigAsCodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,40 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.Configurator;
import io.jenkins.plugins.casc.ConfiguratorRegistry;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.misc.junit.jupiter.WithJenkinsConfiguredWithCode;
import io.jenkins.plugins.casc.model.CNode;
import io.jenkins.plugins.casc.model.Mapping;
import java.util.List;
import java.util.Objects;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.junit.jupiter.api.Test;

public class ConfigAsCodeTest {

@Rule
public JenkinsRule r = new JenkinsConfiguredWithCodeRule();
@WithJenkinsConfiguredWithCode
class ConfigAsCodeTest {

@Test
@ConfiguredWithCode("multiple-sites.yml")
public void shouldSupportConfigurationAsCode() throws Exception {
void shouldSupportConfigurationAsCode(JenkinsConfiguredWithCodeRule r) throws Exception {
List<JiraSite> sites = JiraGlobalConfiguration.get().getSites();
assertThat(sites, hasSize(2));
Assert.assertEquals(
assertEquals(
"https://issues.jenkins-ci.org/",
Objects.requireNonNull(sites.get(0).getUrl()).toExternalForm());
Assert.assertEquals(
assertEquals(
"https://jira.com/",
Objects.requireNonNull(sites.get(1).getUrl()).toExternalForm());
}

@Test
@ConfiguredWithCode("single-site.yml")
public void shouldExportConfigurationAsCode() throws Exception {
void shouldExportConfigurationAsCode(JenkinsConfiguredWithCodeRule r) throws Exception {
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
ConfigurationContext context = new ConfigurationContext(registry);
final Configurator c = context.lookupOrFail(JiraGlobalConfiguration.class);
Expand Down
Loading
Loading