Migrate Service from Spring 2.x and JUnit 4 to Spring 3.3 and JUnit 5 #113
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is for migrating the service from Spring 2.x to Spring 3.3.x and the accompanying changes in JUnit 5 from JUnit 4. Several breaking changes were encountered due to updates in the Java, Spring, and JUnit systems, especially with the introduction of Jakarta EE, the deprecation of certain classes in Spring Security and Spring MVC, and the update to S3Mock for AWS S3 testing.
Changes
javax
tojakarta
packagesThe new Jakarta EE affected several commonly used packages:
javax.validation
→jakarta.validation
: All validation annotations were updated accordingly.javax.servlet.http → jakarta.servlet.http
: Servlet requests and responses were updated to use Jakarta.Updated the imports:
javax.validation.constraints.*
→jakarta.validation.constraints.*
javax.servlet.http.HttpServletResponse
→jakarta.servlet.http.HttpServletResponse
javax.servlet.http.HttpServletRequest
→jakarta.servlet.http.HttpServletRequest
WebMvcConfigurerAdapter
WebMvcConfigurerAdapter
class has been removed. Now we directly implement theWebMvcConfigurer
interface.WebMvcConfigurerAdapter
to implementWebMvcConfigurer
instead.WebSecurityConfigurerAdapter
is deprecated and removed in Spring Security 5.x and later versions. Security configuration has now moved to lambda-based functions usingSecurityFilterChain
andHttpSecurity
.WebSecurityConfigurerAdapter
and replaced it with aSecurityFilterChain
bean.application-test.yml
for Unit Testsapplication-test.yml
for unit tests. Missing configuration parameters in@TestPropertySource
, such ascontext-path
, break the tests.context-path
to the test property source to ensure controllers unit tests pass.Updated annotations and imports to align with
JUnit 5
:@RunWith(SpringJUnit4ClassRunner.class)
→@ExtendWith(SpringExtension.class)
org.junit.Assert
→org.junit.jupiter.api.Assertions
MockitoJUnitRunner
has been replaced by@ExtendWith(MockitoExtension.class)
in JUnit 5.MockitoAnnotations.initMocks()
is no longer required; Mockito now automatically initializes mocks.@Rule TemporaryFolder tempf
→@TempDir Path tempDir
For file creation, replaced
tempf.newFile()
withFiles.createFile(tempDir.resolve())
.For directory creation, replaced
tempf.newFolder()
withFiles.createDirectory(tempDir.resolve())
.JUnit 4 vs JUnit 5 Comparison Table
tempf.newFile("testdb.sqlite")
Files.createFile(tempDir.resolve("testdb.sqlite"))
tempf.newFolder("cache")
Files.createDirectory(tempDir.resolve("cache"))
new File(cdir, "cv0").mkdir()
Files.createDirectory(cacheDir.resolve("cv0"))
Difference between JUnit 5 an JUnit 4
Migrating from JUnit 4 to JUnit 5
Testing:
JUnit
4 toJUnit 5
, and test classes using Spring Boot’s LocalServerPort have been updated to use the correct package.@TempDir
.