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

Fixes @LocalData when being used on a test method in superclass #907

Merged
merged 1 commit into from
Jan 23, 2025

Conversation

Vlatombe
Copy link
Member

@Vlatombe Vlatombe commented Jan 23, 2025

By switching to getDeclaredMethod in #900, this broke this use case that is currently exercised in email-ext plugin
(ScriptContentSecureTest>ScriptContentTest#testTemplateShouldBeLoadedFromTheClosestExistingFolderConfigInTheHierarchyUpToGlobalConfig)

The provided test breaks without this patch.

Testing done

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 demonstrates feature works or fixes the issue

By switching to `getDeclaredMethod` in jenkinsci#900, this broke this use case
that is currently exercised in email-ext plugin
(`ScriptContentSecureTest>ScriptContentTest#testTemplateShouldBeLoadedFromTheClosestExistingFolderConfigInTheHierarchyUpToGlobalConfig`)
/**
* Illustrates a case where a test class extends another test class that has test methods using the LocalData recipe.
*/
public class LocalDataExtendedTest extends LocalDataZipTest {
Copy link
Member

Choose a reason for hiding this comment

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

just to confirm this is intended not to have any test inside it and it fails without the change in src/main?

Copy link
Member Author

@Vlatombe Vlatombe Jan 23, 2025

Choose a reason for hiding this comment

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

Yes, the purpose is for it to pick up the test method defined in LocalDataZipTest to demonstrate the problem.

@Vlatombe Vlatombe merged commit 8235962 into jenkinsci:master Jan 23, 2025
15 checks passed
@Vlatombe Vlatombe deleted the localdata-superclass branch January 23, 2025 16:41
Copy link
Contributor

@strangelookingnerd strangelookingnerd left a comment

Choose a reason for hiding this comment

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

@Vlatombe Sorry for the inconvenience, I was unaware of that use case being used. Please see my comment below.

@@ -102,7 +102,7 @@ public void setup(JenkinsRule jenkinsRule, LocalData recipe) throws Exception {
Method testMethod;

try {
testMethod = desc.getTestClass().getDeclaredMethod(desc.getMethodName());
Copy link
Contributor

Choose a reason for hiding this comment

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

This will not work for JUnit5 tests that do not have a JenkinsRule as parameter (which is a valid option) and are not public (which is also a valid option).

One remedy would be to use both getMethod and getDeclaredMethod.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, ok. Got misleaded by the existing tests that were still passing and didn't go further. I guess we'll need both indeed.

Copy link
Member Author

Choose a reason for hiding this comment

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

that do not have a JenkinsRule as parameter (which is a valid option)

How is that valid? @LocalData is expected to work within a JenkinsRule instance, right?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes it is, however you can do something like this:

@WithJenkins
class TestClass {

  private JenkinsRule rule;

  @BeforeEach
  void before(JenkinsRule rule) {
    this.rule = rule;
  }
  
  @LocalData
  @Test
  void testMethod() {
   // do stuff with rule
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants