diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..ec7cba6 --- /dev/null +++ b/mise.toml @@ -0,0 +1,2 @@ +[tools] +java = "25" diff --git a/pom.xml b/pom.xml index 79f3ae9..b5ec512 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,8 @@ 0 + + false diff --git a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubeConfigExpanderTest.java b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubeConfigExpanderTest.java index 2d5f3e7..287a6ca 100644 --- a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubeConfigExpanderTest.java +++ b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubeConfigExpanderTest.java @@ -1,8 +1,8 @@ package org.jenkinsci.plugins.kubernetes.cli; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; -import org.junit.Test; +import org.junit.jupiter.api.Test; import hudson.EnvVars; diff --git a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlBuildStepTest.java b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlBuildStepTest.java index 15e0b76..6dc73a1 100644 --- a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlBuildStepTest.java +++ b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlBuildStepTest.java @@ -1,8 +1,8 @@ package org.jenkinsci.plugins.kubernetes.cli; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.regex.Pattern; @@ -16,8 +16,9 @@ import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; -import org.junit.Rule; -import org.junit.Test; +import org.jenkinsci.plugins.kubernetes.cli.helpers.JenkinsRuleExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.jvnet.hudson.test.JenkinsRule; import hudson.model.Result; @@ -26,9 +27,9 @@ /** * @author Max Laverse */ +@ExtendWith(JenkinsRuleExtension.class) public class KubectlBuildStepTest { - @Rule - public JenkinsRule r = new JenkinsRule(); + public final JenkinsRule r = new JenkinsRule(); @Test public void testListedCredentials() throws Exception { @@ -55,7 +56,8 @@ public void testScopedCredentials() throws Exception { WorkflowJob p = folder.createProject(WorkflowJob.class, "testScopedCredentials"); p.setDefinition( - new CpsFlowDefinition(TestResourceLoader.loadAsString("withKubeConfigPipelineEchoPath.groovy"), true)); + new CpsFlowDefinition(TestResourceLoader + .loadAsString("withKubeConfigPipelineEchoPath.groovy"), true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); assertNotNull(b); @@ -70,7 +72,8 @@ public void testMissingScopedCredentials() throws Exception { WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testMissingScopedCredentials"); p.setDefinition( - new CpsFlowDefinition(TestResourceLoader.loadAsString("withKubeConfigPipelineEchoPath.groovy"), true)); + new CpsFlowDefinition(TestResourceLoader + .loadAsString("withKubeConfigPipelineEchoPath.groovy"), true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); assertNotNull(b); @@ -85,7 +88,9 @@ public void testKubeConfigDisposedAlsoOnFailure() throws Exception { WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testCleanupOnFailure"); p.setDefinition( - new CpsFlowDefinition(TestResourceLoader.loadAsString("withKubeConfigPipelineFailing.groovy"), true)); + new CpsFlowDefinition( + TestResourceLoader.loadAsString("withKubeConfigPipelineFailing.groovy"), + true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); assertNotNull(b); @@ -97,7 +102,8 @@ public void testKubeConfigDisposedAlsoOnFailure() throws Exception { public void testCredentialNotProvided() throws Exception { WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testWithEmptyCredentials"); p.setDefinition(new CpsFlowDefinition( - TestResourceLoader.loadAsString("withKubeConfigPipelineMissingCredentials.groovy"), true)); + TestResourceLoader.loadAsString("withKubeConfigPipelineMissingCredentials.groovy"), + true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); assertNotNull(b); @@ -111,7 +117,8 @@ public void testUnsupportedCredential() throws Exception { WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "testWithUnsupportedCredentials"); p.setDefinition( - new CpsFlowDefinition(TestResourceLoader.loadAsString("withKubeConfigPipelineEchoPath.groovy"), true)); + new CpsFlowDefinition(TestResourceLoader + .loadAsString("withKubeConfigPipelineEchoPath.groovy"), true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); assertNotNull(b); @@ -129,15 +136,17 @@ public void testEnvVariableFormat() throws Exception { WorkflowJob p = folder.createProject(WorkflowJob.class, "testScopedCredentials"); p.setDefinition( - new CpsFlowDefinition(TestResourceLoader.loadAsString("withKubeConfigPipelineEchoPath.groovy"), true)); + new CpsFlowDefinition(TestResourceLoader + .loadAsString("withKubeConfigPipelineEchoPath.groovy"), true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); assertNotNull(b); assertBuildStatus(b, Result.SUCCESS); String regExp = "Using temporary file '(.+).kube(.+)config'"; Pattern kubeConfigPathRegexp = Pattern.compile(regExp); - assertTrue("No line in the logs matched the regular expression '" + regExp + "': " + JenkinsRule.getLog(b), - kubeConfigPathRegexp.matcher(JenkinsRule.getLog(b)).find()); + assertTrue(kubeConfigPathRegexp.matcher(JenkinsRule.getLog(b)).find(), + "No line in the logs matched the regular expression '" + regExp + "': " + + JenkinsRule.getLog(b)); } private void assertBuildStatus(WorkflowRun b, Result result) throws Exception { diff --git a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlBuildWrapperTest.java b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlBuildWrapperTest.java index e2799df..81f30e4 100644 --- a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlBuildWrapperTest.java +++ b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlBuildWrapperTest.java @@ -1,7 +1,7 @@ package org.jenkinsci.plugins.kubernetes.cli; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import com.cloudbees.plugins.credentials.CredentialsProvider; import com.cloudbees.plugins.credentials.CredentialsStore; @@ -12,8 +12,9 @@ import org.jenkinsci.plugins.kubernetes.cli.helpers.DummyCredentials; import org.jenkinsci.plugins.matrixauth.AuthorizationType; import org.jenkinsci.plugins.matrixauth.PermissionEntry; -import org.junit.Rule; -import org.junit.Test; +import org.jenkinsci.plugins.kubernetes.cli.helpers.JenkinsRuleExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.jvnet.hudson.test.JenkinsRule; import hudson.model.FreeStyleBuild; @@ -31,9 +32,9 @@ /** * @author Max Laverse */ +@ExtendWith(JenkinsRuleExtension.class) public class KubectlBuildWrapperTest { - @Rule - public JenkinsRule r = new JenkinsRule(); + public final JenkinsRule r = new JenkinsRule(); @Test public void testEnvVariablePresent() throws Exception { diff --git a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlCredentialTest.java b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlCredentialTest.java index 7297a48..293b96e 100644 --- a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlCredentialTest.java +++ b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlCredentialTest.java @@ -1,15 +1,16 @@ package org.jenkinsci.plugins.kubernetes.cli; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import com.cloudbees.plugins.credentials.CredentialsProvider; import com.cloudbees.plugins.credentials.CredentialsStore; import com.cloudbees.plugins.credentials.domains.Domain; import org.jenkinsci.plugins.kubernetes.cli.helpers.DummyCredentials; +import org.jenkinsci.plugins.kubernetes.cli.helpers.JenkinsRuleExtension; import org.jenkinsci.plugins.workflow.job.WorkflowJob; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.jvnet.hudson.test.JenkinsRule; import hudson.util.ListBoxModel; @@ -17,9 +18,9 @@ /** * @author Max Laverse */ +@ExtendWith(JenkinsRuleExtension.class) public class KubectlCredentialTest { - @Rule - public JenkinsRule r = new JenkinsRule(); + public final JenkinsRule r = new JenkinsRule(); @Test public void testListedCredentials() throws Exception { diff --git a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlIntegrationTest.java b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlIntegrationTest.java index 218b944..122fb38 100644 --- a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlIntegrationTest.java +++ b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/KubectlIntegrationTest.java @@ -2,10 +2,10 @@ import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.File; import java.nio.file.Files; import java.nio.file.Path; @@ -25,11 +25,12 @@ import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; -import org.junit.Assume; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.experimental.categories.Category; +import org.jenkinsci.plugins.kubernetes.cli.helpers.JenkinsRuleExtension; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.jvnet.hudson.test.JenkinsRule; import hudson.model.Fingerprint; @@ -39,22 +40,22 @@ /** * @author Max Laverse */ -@Category(KubectlIntegrationTest.class) +@Tag("org.jenkinsci.plugins.kubernetes.cli.KubectlIntegrationTest") +@ExtendWith(JenkinsRuleExtension.class) public class KubectlIntegrationTest { - @Rule - public JenkinsRule r = new JenkinsRule(); + public final JenkinsRule r = new JenkinsRule(); protected static final String CREDENTIAL_ID = "test-credentials"; protected static final String SECONDARY_CREDENTIAL_ID = "cred9999"; - @Before + @BeforeEach public void checkKubectlPresence() { assertThat("The '" + kubectlBinaryName() + "' binary could not be found in the PATH", kubectlPresent()); } @Test public void testKubeConfigPermissionsRestrictedRead() throws Exception { - Assume.assumeFalse(System.getProperty("os.name").contains("Windows")); + Assumptions.assumeFalse(System.getProperty("os.name").contains("Windows")); CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), DummyCredentials.usernamePasswordCredential(CREDENTIAL_ID)); @@ -75,7 +76,7 @@ public void testKubeConfigPermissionsRestrictedRead() throws Exception { @Test public void testKubeConfigPermissionsDefault() throws Exception { - Assume.assumeFalse(System.getProperty("os.name").contains("Windows")); + Assumptions.assumeFalse(System.getProperty("os.name").contains("Windows")); CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), DummyCredentials.usernamePasswordCredential(CREDENTIAL_ID)); diff --git a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/MultiKubectlBuildWrapperTest.java b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/MultiKubectlBuildWrapperTest.java index 321c429..e2bb842 100644 --- a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/MultiKubectlBuildWrapperTest.java +++ b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/MultiKubectlBuildWrapperTest.java @@ -1,6 +1,6 @@ package org.jenkinsci.plugins.kubernetes.cli; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.ArrayList; import java.util.List; @@ -9,8 +9,9 @@ import com.cloudbees.plugins.credentials.domains.Domain; import org.jenkinsci.plugins.kubernetes.cli.helpers.DummyCredentials; -import org.junit.Rule; -import org.junit.Test; +import org.jenkinsci.plugins.kubernetes.cli.helpers.JenkinsRuleExtension; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.jvnet.hudson.test.JenkinsRule; import hudson.model.FreeStyleProject; @@ -18,9 +19,9 @@ /** * @author Max Laverse */ +@ExtendWith(JenkinsRuleExtension.class) public class MultiKubectlBuildWrapperTest { - @Rule - public JenkinsRule r = new JenkinsRule(); + public final JenkinsRule r = new JenkinsRule(); @Test public void testConfigurationPersistedOnSave() throws Exception { @@ -56,7 +57,7 @@ public void testConfigurationPersistedOnSave() throws Exception { " test-credentials\n" + " \n" + " \n" + - " false\n" + + " false\n" + " \n" + " \n" + "", p.getConfigFile().asString()); diff --git a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/helpers/JenkinsRuleExtension.java b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/helpers/JenkinsRuleExtension.java new file mode 100644 index 0000000..22515a3 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/helpers/JenkinsRuleExtension.java @@ -0,0 +1,144 @@ +package org.jenkinsci.plugins.kubernetes.cli.helpers; + +import org.junit.jupiter.api.extension.BeforeEachCallback; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.jvnet.hudson.test.JenkinsRule; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +/** + * JUnit 5 extension that bridges JenkinsRule (designed for JUnit 4) with JUnit + * 5. + * + *

+ * This extension automatically finds JenkinsRule fields in test classes and + * manages + * their lifecycle (before/after each test). It sets up the test description + * that + * JenkinsRule expects from JUnit 4's test runner. + * + *

+ * Usage: + * + *

+ * {@code @ExtendWith(JenkinsRuleExtension.class)}
+ * public class MyTest {
+ *     public final JenkinsRule r = new JenkinsRule();
+ *     // ... tests ...
+ * }
+ * 
+ */ +public class JenkinsRuleExtension implements BeforeEachCallback, AfterEachCallback { + + private static final Field TEST_DESCRIPTION_FIELD; + + static { + Field field = null; + try { + field = JenkinsRule.class.getDeclaredField("testDescription"); + field.setAccessible(true); + } catch (NoSuchFieldException | SecurityException e) { + // Field doesn't exist or can't be accessed - will handle gracefully + } + TEST_DESCRIPTION_FIELD = field; + } + + /** + * Finds all JenkinsRule fields in the test instance. + */ + private List findJenkinsRuleFields(Object testInstance) { + List jenkinsRuleFields = new ArrayList<>(); + Class clazz = testInstance.getClass(); + + // Check all fields in the class hierarchy + while (clazz != null) { + for (Field field : clazz.getDeclaredFields()) { + if (field.getType() == JenkinsRule.class) { + field.setAccessible(true); + jenkinsRuleFields.add(field); + } + } + clazz = clazz.getSuperclass(); + } + + return jenkinsRuleFields; + } + + /** + * Sets up the test description for JenkinsRule to work with JUnit 5. + */ + private void setupTestDescription(JenkinsRule rule, Object testInstance, ExtensionContext context) { + if (TEST_DESCRIPTION_FIELD == null) { + return; + } + + try { + String testMethodName = context.getTestMethod() + .map(m -> m.getName()) + .orElse("test"); + + org.junit.runner.Description description = org.junit.runner.Description.createTestDescription( + testInstance.getClass(), + testMethodName); + + TEST_DESCRIPTION_FIELD.set(rule, description); + } catch (IllegalAccessException e) { + // If we can't set the description, continue without it + // Some versions of JenkinsRule may not require it + } + } + + @Override + public void beforeEach(ExtensionContext context) throws Exception { + Object testInstance = context.getRequiredTestInstance(); + List fields = findJenkinsRuleFields(testInstance); + + for (Field field : fields) { + JenkinsRule rule = (JenkinsRule) field.get(testInstance); + if (rule != null) { + setupTestDescription(rule, testInstance, context); + try { + rule.before(); + } catch (Error e) { + // Re-throw Errors as-is (they should not be caught) + throw e; + } catch (Exception e) { + // Re-throw Exceptions as-is + throw e; + } catch (Throwable t) { + // Wrap other Throwables (though unlikely) + throw new Exception("JenkinsRule.before() failed", t); + } + } + } + } + + @Override + public void afterEach(ExtensionContext context) throws Exception { + Object testInstance = context.getRequiredTestInstance(); + List fields = findJenkinsRuleFields(testInstance); + + // Process in reverse order for cleanup + for (int i = fields.size() - 1; i >= 0; i--) { + Field field = fields.get(i); + JenkinsRule rule = (JenkinsRule) field.get(testInstance); + if (rule != null) { + try { + rule.after(); + } catch (Error e) { + // Re-throw Errors as-is (they should not be caught) + throw e; + } catch (Exception e) { + // Re-throw Exceptions as-is + throw e; + } catch (Throwable t) { + // Wrap other Throwables (though unlikely) + throw new Exception("JenkinsRule.after() failed", t); + } + } + } + } +} diff --git a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/helpers/TestResourceLoader.java b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/helpers/TestResourceLoader.java index 74c7dd1..4177b10 100644 --- a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/helpers/TestResourceLoader.java +++ b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/helpers/TestResourceLoader.java @@ -1,6 +1,7 @@ package org.jenkinsci.plugins.kubernetes.cli.helpers; -import org.apache.commons.compress.utils.IOUtils; +import java.io.IOException; +import java.io.InputStream; public class TestResourceLoader { public static String loadAsString(String name) { @@ -8,10 +9,13 @@ public static String loadAsString(String name) { } public static byte[] loadAsByteArray(String name) { - try { - return IOUtils.toByteArray(TestResourceLoader.class.getResourceAsStream("../" + name)); - } catch (Throwable t) { - throw new RuntimeException("Could not read resource:[" + name + "]."); + try (InputStream inputStream = TestResourceLoader.class.getResourceAsStream("../" + name)) { + if (inputStream == null) { + throw new RuntimeException("Could not find resource:[" + name + "]."); + } + return inputStream.readAllBytes(); + } catch (IOException e) { + throw new RuntimeException("Could not read resource:[" + name + "].", e); } } } diff --git a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/kubeconfig/CertificateHelperTest.java b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/kubeconfig/CertificateHelperTest.java index 35b7236..c7938df 100644 --- a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/kubeconfig/CertificateHelperTest.java +++ b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/kubeconfig/CertificateHelperTest.java @@ -1,8 +1,8 @@ package org.jenkinsci.plugins.kubernetes.cli.kubeconfig; -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 CertificateHelperTest { @Test diff --git a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/kubeconfig/KubeConfigWriterAuthTest.java b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/kubeconfig/KubeConfigWriterAuthTest.java index 72dd0bd..8138941 100644 --- a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/kubeconfig/KubeConfigWriterAuthTest.java +++ b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/kubeconfig/KubeConfigWriterAuthTest.java @@ -12,27 +12,28 @@ import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuth; import org.jenkinsci.plugins.kubernetes.auth.impl.*; import org.jenkinsci.plugins.kubernetes.cli.helpers.DummyTokenCredentialImpl; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.mockito.Mockito; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; public class KubeConfigWriterAuthTest { - @Rule - public TemporaryFolder tempFolder = new TemporaryFolder(); + @TempDir + Path tempFolder; FilePath workspace; Launcher mockLauncher; @@ -84,9 +85,11 @@ public void KubernetesAuthUsernamePassword() throws Exception { " username: \"test-user\"\n", configDumpContent); } - @Before + @BeforeEach public void init() throws IOException, InterruptedException { - workspace = new FilePath(tempFolder.newFolder("workspace")); + Path workspacePath = tempFolder.resolve("workspace"); + Files.createDirectories(workspacePath); + workspace = new FilePath(workspacePath.toFile()); mockLauncher = Mockito.mock(Launcher.class); VirtualChannel mockChannel = Mockito.mock(VirtualChannel.class); diff --git a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/kubeconfig/KubeConfigWriterBuilderTest.java b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/kubeconfig/KubeConfigWriterBuilderTest.java index 1b1f745..fb65c82 100644 --- a/src/test/java/org/jenkinsci/plugins/kubernetes/cli/kubeconfig/KubeConfigWriterBuilderTest.java +++ b/src/test/java/org/jenkinsci/plugins/kubernetes/cli/kubeconfig/KubeConfigWriterBuilderTest.java @@ -1,6 +1,6 @@ package org.jenkinsci.plugins.kubernetes.cli.kubeconfig; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @@ -8,16 +8,16 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.PrintStream; +import java.nio.file.Files; import com.fasterxml.jackson.core.JsonProcessingException; import org.jenkinsci.plugins.kubernetes.auth.KubernetesAuth; import org.jenkinsci.plugins.kubernetes.auth.impl.KubernetesAuthKubeconfig; import org.jenkinsci.plugins.kubernetes.auth.impl.KubernetesAuthUsernamePassword; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.mockito.Mockito; import hudson.EnvVars; @@ -27,11 +27,12 @@ import hudson.model.TaskListener; import hudson.remoting.VirtualChannel; import io.fabric8.kubernetes.api.model.ConfigBuilder; +import java.nio.file.Path; public class KubeConfigWriterBuilderTest { final ByteArrayOutputStream output = new ByteArrayOutputStream(); - @Rule - public TemporaryFolder tempFolder = new TemporaryFolder(); + @TempDir + public Path tempFolder; FilePath workspace; Launcher mockLauncher; AbstractBuild build; @@ -64,9 +65,11 @@ private static KubernetesAuthKubeconfig dummyKubeConfigAuth() { " username: \"existing-user\"\n"); } - @Before + @BeforeEach public void init() throws IOException, InterruptedException { - workspace = new FilePath(tempFolder.newFolder("workspace")); + Path workspacePath = tempFolder.resolve("workspace"); + Files.createDirectories(workspacePath); + workspace = new FilePath(workspacePath.toFile()); mockLauncher = Mockito.mock(Launcher.class); VirtualChannel mockChannel = Mockito.mock(VirtualChannel.class);