Skip to content

Commit

Permalink
Re-introduce support for Junit5 tests not having JenkinsRule as param…
Browse files Browse the repository at this point in the history
…eter (#909)
  • Loading branch information
Vlatombe authored Jan 24, 2025
1 parent f1281ba commit ab3a21a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/main/java/org/jvnet/hudson/test/recipes/LocalData.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.logging.Logger;
import org.junit.runner.Description;
import org.jvnet.hudson.test.HudsonHomeLoader.Local;
import org.jvnet.hudson.test.HudsonTestCase;
Expand Down Expand Up @@ -86,6 +87,7 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface LocalData {
String value() default "";
Logger LOGGER = Logger.getLogger(LocalData.class.getName());

class RunnerImpl extends Recipe.Runner<LocalData> {
@Override
Expand All @@ -99,14 +101,21 @@ class RuleRunnerImpl extends JenkinsRecipe.Runner<LocalData> {
public void setup(JenkinsRule jenkinsRule, LocalData recipe) throws Exception {
Description desc = jenkinsRule.getTestDescription();

Method testMethod;

try {
testMethod = desc.getTestClass().getMethod(desc.getMethodName());
} catch (NoSuchMethodException ex) {
testMethod = desc.getTestClass().getDeclaredMethod(desc.getMethodName(), JenkinsRule.class);
Method testMethod = null;
for (var cls = desc.getTestClass(); testMethod == null && cls != null; cls = cls.getSuperclass()) {
try {
testMethod = cls.getDeclaredMethod(desc.getMethodName());
} catch (NoSuchMethodException ex) {
try {
testMethod = cls.getDeclaredMethod(desc.getMethodName(), JenkinsRule.class);
} catch (NoSuchMethodException ex2) {
LOGGER.fine("No method " + desc.getMethodName() + " in " + cls);
}
}
}
if (testMethod == null) {
throw new NoSuchMethodException("Could not look up any test method for " + desc);
}

jenkinsRule.with(new Local(testMethod, recipe.value()));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.jvnet.hudson.test.junit.jupiter;

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

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.recipes.LocalData;

@WithJenkins
class JenkinsRuleResolverBeforeTest {
private JenkinsRule rule;

@BeforeEach
void before(JenkinsRule rule) {
this.rule = rule;
}

@LocalData
@Test
void localData() {
assertNotNull(rule.jenkins.getItem("testJob"));
}

@LocalData
@Test
void localDataZip() {
assertNotNull(rule.jenkins.getItem("somejob"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version='1.0' encoding='UTF-8'?>
<project>
<actions/>
<description></description>
<properties>
<hudson.security.AuthorizationMatrixProperty>
<useProjectSecurity>true</useProjectSecurity>
<permission>hudson.model.Item.ExtendedRead:alice</permission>
<permission>hudson.model.Item.Configure:alice</permission>
<permission>hudson.model.Item.Configure:bob</permission>
<permission>hudson.model.Item.ExtendedRead:charlie</permission>
</hudson.security.AuthorizationMatrixProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<canRoam>true</canRoam>
<disabled>false</disabled>
<jdk>(Default)</jdk>
<triggers class="vector"/>
<builders/>
<publishers/>
<buildWrappers/>
</project>
Binary file not shown.

0 comments on commit ab3a21a

Please sign in to comment.