Migrate tests to @ParameterizedClass in spring-core#36181
Migrate tests to @ParameterizedClass in spring-core#36181CodingMasterLSW wants to merge 4 commits intospring-projects:mainfrom
@ParameterizedClass in spring-core#36181Conversation
@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.
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.
Can't this be declared directly on AbstractDataBufferAllocatingTests?
There was a problem hiding this comment.
I moved these annotations to the AbstractDataBufferAllocatingTests class.
| DataBufferTestUtilsTests(DataBufferFactory bufferFactory) { | ||
| DataBufferTestUtilsTests.this.bufferFactory = bufferFactory; | ||
| } |
There was a problem hiding this comment.
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.
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.
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.
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.
| 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".
| private final MultiValueMap<String, String> map; | ||
|
|
||
| public ParameterizedMultiValueMapTests(MultiValueMap<String, String> map) { | ||
| this.map = new LinkedMultiValueMap<>(map); |
There was a problem hiding this comment.
| this.map = new LinkedMultiValueMap<>(map); | |
| this.map = map; |
There was a problem hiding this comment.
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