-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Migrate tests to @ParameterizedClass in spring-core
#36181
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
base: main
Are you sure you want to change the base?
Migrate tests to @ParameterizedClass in spring-core
#36181
Conversation
@ParameterizedClass in spring-core
|
Please provide the DCO sign-off in all commits. |
Signed-off-by: CodingMasterLSW <spqjekdl1004@gmail.com>
Signed-off-by: CodingMasterLSW <spqjekdl1004@gmail.com>
f8245c7 to
151df60
Compare
|
@sbrannen |
sbrannen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the first PR in this "series"! 👍
I think we'll "learn as we go" regarding how best to achieve this migration.
So, I've requested a few changes as a first step.
| @ParameterizedClass | ||
| @MethodSource("org.springframework.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests#dataBufferFactories()") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't this be declared directly on AbstractDataBufferAllocatingTests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved these annotations to the AbstractDataBufferAllocatingTests class.
| DataBufferTestUtilsTests(DataBufferFactory bufferFactory) { | ||
| DataBufferTestUtilsTests.this.bufferFactory = bufferFactory; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similarly, instead of declaring such constructors in each subclass, I think it would be much better to annotate the bufferFactory field in AbstractDataBufferAllocatingTests with @Parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, That's great point!
I'm new to @parameter, so I appreciate you pointing that out!
| @Nested | ||
| @ParameterizedClass | ||
| @MethodSource("org.springframework.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests#dataBufferFactories()") | ||
| class ParameterizedDataBufferUtilsTests { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the @Nested test classes like this one should extend AbstractDataBufferAllocatingTests, and the enclosing classes should no longer extend that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense to me.
Reflected your feedback in 672157b.
| assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() -> relative4.getContentAsString(UTF_8)); | ||
| } | ||
|
|
||
| private static Stream<Arguments> resource() throws URISyntaxException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| private static Stream<Arguments> resource() throws URISyntaxException { | |
| private static Stream<Arguments> resources() throws URISyntaxException { |
While we're at it, we might as well rename that method to "resources".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reflected your feedback in 84d677b
| private final MultiValueMap<String, String> map; | ||
|
|
||
| public ParameterizedMultiValueMapTests(MultiValueMap<String, String> map) { | ||
| this.map = new LinkedMultiValueMap<>(map); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| this.map = new LinkedMultiValueMap<>(map); | |
| this.map = map; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found a potential issue with using @ParameterizedClass in this case.
Since the parameter object from @MethodSource is created only once and shared across all test methods,
modifying it (e.g., this.map = map;) causes state to leak between tests.
To ensure test isolation, I've considered the following options:
-
Create a defensive copy in the constructor (current approach)
Since the constructor is called for each test method, instantiating a new object ensures each test gets its own isolated instance. -
Use @ParameterizedTest instead
Each test method receives a fresh parameter instance, but this requires repeating the parameter declaration for every test method.
Please let me know if there's a better way to handle this!
…ferAllocatingTests Signed-off-by: CodingMasterLSW <spqjekdl1004@gmail.com>
Signed-off-by: CodingMasterLSW <spqjekdl1004@gmail.com>
|
Hi @sbrannen, I've applied your feedback. Additionally, I’ve updated Also, I’ve left a comment regarding a potential issue with one of the suggested changes. I’d appreciate it if you could share your thoughts on that. Thanks! |
This PR migrates parameterized tests to
@ParameterizedClassin thespring-coremodule.To prioritize code cohesion, I actively use
@Nestedtest classes.@ParameterizedClasswhere feasible #35833