Skip to content

Migrate tests to JUnit5#1734

Merged
jglick merged 15 commits into
jenkinsci:masterfrom
strangelookingnerd:migrate_to_junit5
Jul 9, 2026
Merged

Migrate tests to JUnit5#1734
jglick merged 15 commits into
jenkinsci:masterfrom
strangelookingnerd:migrate_to_junit5

Conversation

@strangelookingnerd

Copy link
Copy Markdown
Contributor

This PR aims to migrate all tests to JUnit5. Changes include:

  • Migrate annotations and imports
  • Migrate assertions
  • Remove public visibility for test classes and methods
  • Minor clean up

I am well aware that this is a quite large changeset however I hope that there is still interest in this PR and it will be reviewed.
If there are any questions, please do not hesitate to ping me.

Submitter checklist

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests that demonstrate the feature works or the issue is fixed

* Migrate annotations and imports
* Migrate assertions
* Remove public visibility for test classes and methods
* Minor code cleanup
@strangelookingnerd

This comment was marked as resolved.

@strangelookingnerd strangelookingnerd marked this pull request as ready for review August 21, 2025 08:35
@strangelookingnerd strangelookingnerd requested a review from a team as a code owner August 21, 2025 08:35
* ReaperListenerWatchEvent converted to record
* CapturingReaperListener#CAPTURED_EVENTS not static anymore. Removed Junit extension as it was not actually needed.
* Use await() instead of relying on java monitor and synchronized.
@Vlatombe

This comment was marked as resolved.

@Vlatombe Vlatombe added the test Tests label Aug 25, 2025
# Conflicts:
#	src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java
@Vlatombe Vlatombe enabled auto-merge (squash) September 30, 2025 07:41
ConfigMapVolume configMapVolume = new ConfigMapVolume("oneMountPath", "Myvolume", false);
configMapVolume.setSubPath("miSubpath");
assertEquals(configMapVolume.getSubPath(), "miSubpath");
assertEquals("miSubpath", configMapVolume.getSubPath());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, was backwards in 4, being corrected while we are here.

auto-merge was automatically disabled December 4, 2025 13:12

Head branch was pushed to by a user without write access

# Conflicts:
#	src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesClientProviderTest.java
#	src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecoratorTest.java
# Conflicts:
#	src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesClientProviderTest.java
#	src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecoratorTest.java
@jglick jglick mentioned this pull request Jul 8, 2026
8 tasks
@Before
public void setUpCloud() {
@BeforeEach
void beforeEach() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original method name was more descriptive, so there was no reason to rename it, but 🤷

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR migrates the plugin’s test suite from JUnit 4 to JUnit 5 (JUnit Jupiter), updating annotations/assertions and adapting Jenkins test harness usage to JUnit 5 extensions (e.g., @WithJenkins, RealJenkinsExtension, JenkinsSessionExtension).

Changes:

  • Updated test annotations/imports/assertions to JUnit Jupiter and removed public visibility where appropriate.
  • Replaced JUnit4 rule/runner patterns with JUnit5/Jenkins-test-harness JUnit5 extensions (JenkinsRule injection, @ExtendWith(MockitoExtension.class), timeouts, etc.).
  • Removed JUnitParams and adjusted tests previously depending on JUnit4 theories/parameterization patterns.

Reviewed changes

Copilot reviewed 71 out of 71 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/test/java/org/csanchez/jenkins/plugins/kubernetes/volumes/workspace/GenericEphemeralWorkspaceVolumeTest.java Migrates basic unit test to JUnit Jupiter (imports/visibility).
src/test/java/org/csanchez/jenkins/plugins/kubernetes/volumes/GenericEphemeralVolumeTest.java Migrates basic unit test to JUnit Jupiter (imports/visibility).
src/test/java/org/csanchez/jenkins/plugins/kubernetes/TaskListenerEventWatcherTest.java Migrates to JUnit Jupiter + Mockito Jupiter extension; uses StandardCharsets.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/RestrictedPssSecurityInjectorTest.java Switches to JUnit Jupiter lifecycle methods and updated exception handling.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodUtilsTest.java Migrates assertions to JUnit Jupiter and removes public.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateUtilsTest.java Reworks JUnit4 Theories to JUnit5 parameterized tests and modernizes collections usage.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateTest.java Migrates to JUnit Jupiter and simplifies exception signatures.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateMapTest.java Migrates JenkinsRule usage to JUnit5 @WithJenkins injection style.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateJenkinsTest.java Migrates JenkinsRule to JUnit5 @WithJenkins + injected rule.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodTemplateFilterTest.java Migrates JenkinsRule handling and lifecycle to JUnit5.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodEnvVarTest.java Migrates to JUnit Jupiter assertions and visibility updates.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/PodContainerSourceTest.java Migrates JenkinsRule + assertions to JUnit5/Jupiter; updates Mockito wiring.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pod/retention/ReaperTest.java Migrates to JUnit5, updates extension handling, and switches timeouts to @Timeout.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pod/retention/PodRetentionTest.java Migrates lifecycle + assertions to JUnit Jupiter.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pod/decorator/PodDecoratorTest.java Migrates to JUnit5 + Mockito Jupiter and JenkinsRule injection.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/WebSocketTest.java Migrates pipeline test lifecycle to JUnit Jupiter.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/steps/SetupCloud.java Migrates RealJenkinsRule step to RealJenkinsExtension.Step.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/steps/CreateWorkflowJobThenScheduleTask.java Migrates step interface to RealJenkinsExtension.Step2.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/steps/CreateWorkflowJobThenScheduleRun.java Migrates step interface to RealJenkinsExtension.Step2.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/steps/AssertBuildStatusSuccess.java Migrates step interface to RealJenkinsExtension.Step.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/steps/AssertBuildLogMessage.java Migrates step interface to RealJenkinsExtension.Step.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/RestartPipelineTest.java Migrates JUnit4 rules (TemporaryFolder, TestName, LoggerRule) to JUnit5 equivalents (@TempDir, TestInfo, LogRecorder, extensions).
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ResourcesTest.java Migrates basic unit test methods to JUnit Jupiter.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/PodTemplateStepTest.java Migrates JenkinsRule to injected JUnit5 style.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/PodTemplateStepExecutionTest.java Migrates lifecycle and JenkinsRule to JUnit5 injection.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/PodProvisioningStatusLogsTest.java Migrates pipeline test methods and assertions to JUnit Jupiter.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/PodNameTest.java Migrates lifecycle hooks to JUnit Jupiter.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/NoDelayProvisionerStrategyTest.java Migrates Mockito runner to Mockito Jupiter; updates setup/lifecycle.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesSamplesTest.java Migrates to JUnit5; replaces ErrorCollector usage with direct assertions and adds class timeout.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineWebsocketRJRTest.java Migrates to JUnit Jupiter and updated constructor exception typing.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineTest.java Major JUnit5 migration: assumptions, lifecycle hooks, rule replacements, and assertion API updates.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineRJRTest.java Migrates RealJenkinsRule-based tests to JUnit5 style and TestInfo-based naming.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesPipelineOverriddenNamespaceTest.java Renames class to match file intent and migrates to JUnit Jupiter.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesNodeContextTest.java Migrates Mockito runner to Mockito Jupiter and uses assertThrows.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesDeclarativeAgentUnitTest.java Migrates JenkinsRule usage and updates directive-generation assertions to text blocks.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesDeclarativeAgentTest.java Migrates to JUnit5, adopts JUnit5 git sample repo extension integration.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesDeclarativeAgentRJRTest.java Migrates to JUnit Jupiter + updated constructor exception typing.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/KubernetesAgentErrorConditionTest.java Migrates to JUnit Jupiter and replaces BuildWatcher/LoggerRule with JUnit5 extensions/LogRecorder.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/DirectConnectionTest.java Updates JUnit lifecycle override to JUnit5 @BeforeEach pattern with injected JenkinsRule/TestInfo.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerLogStepTest.java Migrates assertions and visibility to JUnit Jupiter.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecoratorWindowsTest.java Migrates to JUnit5; replaces JUnit4 rules with injected JenkinsRule/TestInfo and LogRecorder.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecoratorTest.java Migrates to JUnit5, updates exception assertions and logging; adds class-level timeout.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/ContainerExecDecoratorPipelineTest.java Migrates logging rule to LogRecorder and assertions to Jupiter style.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/AbstractKubernetesPipelineTest.java Refactors common pipeline test base to JUnit5 (@WithJenkins, injected JenkinsRule, TestInfo).
src/test/java/org/csanchez/jenkins/plugins/kubernetes/pipeline/AbstractKubernetesPipelineRJRTest.java Migrates RealJenkinsRule usage to RealJenkinsExtension and JUnit5 lifecycle.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/NonConfigurableKubernetesCloudTest.java Migrates JenkinsRule handling + LoggerRule to JUnit5 injection + LogRecorder.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/MetricNamesTest.java Migrates assertions to Jupiter and removes JUnit4 Assert.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesTestUtil.java Updates helpers for JUnit5 assumptions/assertions and switches TestName usage to string-based naming.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesTest.java Migrates LocalData tests and logger usage to JUnit5/JenkinsRule injection patterns.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesSlaveTest.java Migrates to JUnit5; replaces try/catch fail with assertDoesNotThrow and modern collections.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesRestartTest.java Migrates JenkinsSessionRule to JUnit5 JenkinsSessionExtension.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesQueueTaskDispatcherTest.java Migrates JenkinsRule and Mockito rule usage to JUnit5 + Mockito Jupiter.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesProvisioningLimitsTest.java Migrates JenkinsRule/logging rule to JUnit5 injection + LogRecorder.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesLauncherTest.java Migrates to JUnit Jupiter and updates assertion style.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesFolderPropertyTest.java Migrates JenkinsRule to JUnit5 injection style.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesFactoryAdapterTest.java Migrates lifecycle hooks and JenkinsRule usage to JUnit5 injection.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTraitTest.java Migrates JenkinsRule to injected JUnit5 style and updates assertions.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudTest.java Migrates to JUnit5; updates exception assertions and lifecycle hooks.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesCloudFIPSTest.java Migrates to JUnit5; replaces FlagRule with manual system-property lifecycle management.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubernetesClientProviderTest.java Migrates assertions to Jupiter and simplifies assertion plumbing.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/KubectlBuildWrapperTest.java Migrates setup/test methods to JUnit Jupiter.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/ContainerTemplateTest.java Migrates assertion APIs and exception assertions to Jupiter.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/ConfigMapVolumeTest.java Migrates to JUnit Jupiter and corrects assert argument order.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/ClientAuthenticationTest.java Migrates to JUnit Jupiter and removes unused logger.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/casc/WorkspaceVolumeCasCTest.java Migrates CASC round-trip test base to JUnit5 version; introduces (unsupported) class parameterization annotations.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/casc/VolumeCasCTest.java Migrates CASC round-trip test base to JUnit5 version; introduces (unsupported) class parameterization annotations.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/casc/EnvVarCasCTest.java Migrates CASC round-trip test base to JUnit5 version; introduces (unsupported) class parameterization annotations.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/casc/CasCTest.java Migrates CASC round-trip test base to JUnit5 version.
src/test/java/org/csanchez/jenkins/plugins/kubernetes/AbstractGoldenFileTest.java Migrates shared golden-file test base to JUnit Jupiter lifecycle + assertion API.
pom.xml Adds a property related to banning JUnit4 imports and switches Mockito dependency to mockito-junit-jupiter.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


@BeforeAll
static void beforeAll() {
fipsFlag = System.setProperty(FIPS140.class.getName() + ".COMPLIANCE", "true");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could now use FlagExtension IIUC (also IIRC there is a new Jupiter built-in feature to manage system properties specifically)

Comment on lines +28 to +31
@BeforeEach
void beforeEach(JenkinsRule rule) {
j = rule;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some reason for this idiom? Seems more straightforward to pass JenkinsRule j as an arg to test methods.

public void testGetPodRetention() {
try {
void testGetPodRetention() {
assertDoesNotThrow(() -> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No apparent reason for this. Original test code should probably just have declared throws Exception.

client.pods().list();
} catch (Exception e) {
assumeNoException(e);
assumeTrue(false, e.toString());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deletePods(client, getLabels(this, name), true);
}

// in case we need to pull windows docker image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment belonged on the @WithTimeout annotation. Confusing where it is now.

KubernetesClient client = cloud.connect();
Map<String, String> labels = getLabels(this, name);
assertTrue(client.pods().withLabels(labels).delete().size() > 0);
assertFalse(client.pods().withLabels(labels).delete().isEmpty());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or Hamcrest, for the better error messages

System.err.println("==== " + gs.title() + " ====");
p = r.createProject(WorkflowJob.class, gs.name());
p.setDefinition(new CpsFlowDefinition(gs.script(), true));
errors.checkSucceeds(() -> r.buildAndAssertSuccess(p));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the purpose of ErrorCollector here was to get one failure message per failing sample.

@jglick jglick left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only minor comments.

@jglick jglick merged commit 7338d28 into jenkinsci:master Jul 9, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants