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

Add test for SorterStrategyDescriptor class #429

Merged

Conversation

yashpal2104
Copy link
Contributor

Testing done

Add test for SorterStrategyDescriptor class

Aims to Fix JENKINS-69757

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

@github-actions github-actions bot added the tests Automated test addition or improvement label Jan 1, 2025
@yashpal2104
Copy link
Contributor Author

yashpal2104 commented Jan 1, 2025

@MarkEWaite
I have implemented the TestSorterStrategy and TestSorterStrategyDescriptor classes to provide specific implementations for the abstract methods. Could you please review my approach to creating a concrete implementation of the abstract SorterStrategy class for testing purposes is it valid or not?
I want to ensure that this approach is valid and follows best practices for testing SorterStrategyDescriptor.

@yashpal2104 yashpal2104 marked this pull request as ready for review January 4, 2025 12:48
@yashpal2104 yashpal2104 requested a review from a team as a code owner January 4, 2025 12:48
The spotbugs annotation is used in the declaration of the method so
should be used in this implementation.
Individual tests do not need the cost or time delay of starting a new
Jenkins controller.
Allocate a strategy and test its descriptor
Copy link
Contributor

@MarkEWaite MarkEWaite left a comment

Choose a reason for hiding this comment

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

The technique looks good to me. I've submitted a few changes and corrections.

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Queue;
import org.jetbrains.annotations.NotNull;
Copy link
Contributor

Choose a reason for hiding this comment

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

We use spotbugs annotations for nullability rather than JetBrains annotations

Suggested change
import org.jetbrains.annotations.NotNull;
import edu.umd.cs.findbugs.annotations.NonNull;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I think this was because I was using Intellij Jetbrains IDE, I mistakenly imported the incorrect one


private static class TestSorterStrategy extends SorterStrategy {
@Override
public SorterStrategyCallback onNewItem(@NotNull Queue.Item item, SorterStrategyCallback weightCallback) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Spotbugs annotation rather than JetBrains annotation

Suggested change
public SorterStrategyCallback onNewItem(@NotNull Queue.Item item, SorterStrategyCallback weightCallback) {
public SorterStrategyCallback onNewItem(@NonNull Queue.Item item, SorterStrategyCallback weightCallback) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for correcting this

Other tests will fail if descriptor is null
@MarkEWaite MarkEWaite enabled auto-merge (squash) January 4, 2025 16:17
@MarkEWaite MarkEWaite merged commit f1aa5dd into jenkinsci:master Jan 4, 2025
18 checks passed
@yashpal2104
Copy link
Contributor Author

Thanks for reviewing the PR @MarkEWaite .
It is getting more and more difficult to write Tests for the classes without modifying the original src/main class because some methods and functions are missing for eg. The get function to retrieve or set functions, recently for MultiBucketStrategy there was a jenkins wrapper class(dont seem to remeber the name) which I needed to test so I was not able to add a dependency in the pom.xml (I think there was some sort of XStream Core dependency error I think the latest version was not able to be added). What are your thoughts on this? Do you think there needs modification in the original Source code and anything you would like to add?

@yashpal2104 yashpal2104 deleted the sorterStrategyDescriptorTest branch January 4, 2025 16:36
@MarkEWaite
Copy link
Contributor

MarkEWaite commented Jan 4, 2025

It is getting more and more difficult to write Tests for the classes without modifying the original src/main class because some methods and functions are missing

Changes to the main classes are acceptable if they are:

  • compatible, not breaking upgrades or other use cases
  • minimally invasive, changing as little as they can while still improving testability

In the case of MultiBucketStrategy, why not focus on testing AbsoluteStrategy, since it extends MultiBucketStrategy?

I was not able to add a dependency in the pom.xml

I suspect you may have been making a mistake in that case. Generally you should not need to add to the plugin pom file unless the addition is test scoped and specific to that test.

@yashpal2104
Copy link
Contributor Author

In the case of MultiBucketStrategy, why not focus on testing AbsoluteStrategy, since it extends MultiBucketStrategy?

Alright, I will try this instead

I suspect you may have been making a mistake in that case. Generally you should not need to add to the plugin pom file unless the addition is test scoped and specific to that test.

Ok I was only thinking of using that wrapper class for only specific test class only but a error of not having XStream-Core and a fix was provided by the IDE was to add this dependency link

@MarkEWaite
Copy link
Contributor

Ok I was only thinking of using that wrapper class for only specific test class only but a error of not having XStream-Core and a fix was provided by the IDE was to add this dependency link

XStream is already provided by Jenkins core. There should never be a need to add it to a plugin pom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests Automated test addition or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants