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

Migrate Service from Spring 2.x and JUnit 4 to Spring 3.3 and JUnit 5 #113

Open
wants to merge 14 commits into
base: integration
Choose a base branch
from

Conversation

elmiomar
Copy link
Contributor

@elmiomar elmiomar commented Oct 9, 2024

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

  1. Migration from javax to jakarta packages
  • The new Jakarta EE affected several commonly used packages:

    • javax.validationjakarta.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.HttpServletResponsejakarta.servlet.http.HttpServletResponse
    • javax.servlet.http.HttpServletRequestjakarta.servlet.http.HttpServletRequest
  1. Removal of WebMvcConfigurerAdapter
  • The WebMvcConfigurerAdapter class has been removed. Now we directly implement the WebMvcConfigurer interface.
  • Updated classes extending WebMvcConfigurerAdapter to implement WebMvcConfigurer instead.
  1. Spring Security Updates
  • WebSecurityConfigurerAdapter is deprecated and removed in Spring Security 5.x and later versions. Security configuration has now moved to lambda-based functions using SecurityFilterChain and HttpSecurity.
  • Exception handling was simplified: Removed: Custom exceptionHandling() due to deprecation.
  • Removed WebSecurityConfigurerAdapter and replaced it with a SecurityFilterChain bean.
  1. Spring 3.3 Requires application-test.yml for Unit Tests
  • In Spring 3.3, apparently it is required to have an application-test.yml for unit tests. Missing configuration parameters in @TestPropertySource, such as context-path, break the tests.
  • Fix: Added missing config parameters like context-path to the test property source to ensure controllers unit tests pass.
  1. JUnit 4 to JUnit 5 Migration
  • Updated annotations and imports to align with JUnit 5:

    • @RunWith(SpringJUnit4ClassRunner.class)@ExtendWith(SpringExtension.class)
    • org.junit.Assertorg.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() with Files.createFile(tempDir.resolve()).

  • For directory creation, replaced tempf.newFolder() with Files.createDirectory(tempDir.resolve()).

  • JUnit 4 vs JUnit 5 Comparison Table

Feature JUnit 4 JUnit 5
Temporary Directory @rule TemporaryFolder tempf @tempdir Path tempDir
Create Temporary File tempf.newFile("testdb.sqlite") Files.createFile(tempDir.resolve("testdb.sqlite"))
Create Temporary Directory tempf.newFolder("cache") Files.createDirectory(tempDir.resolve("cache"))
Create Subdirectory new File(cdir, "cv0").mkdir() Files.createDirectory(cacheDir.resolve("cv0"))
Setup Method Annotation @before @beforeeach
Exception Handling try-catch with fail() assertThrows()
Assertions assertEquals, assertTrue, assertFalse, assertNotNull Same in JUnit 5

Difference between JUnit 5 an JUnit 4
Migrating from JUnit 4 to JUnit 5

  1. Dependencies and Configuration Changes
  • Updated pom.xml to ensure compatibility with Java 21 and Spring Boot 3.x
  • Replaced deprecated dependencies with their Jakarta alternatives.
  • Ensured compatibility with Java 21 with all third-party libraries.

Testing:

  • All unit tests have been updated from JUnit 4 to JUnit 5, and test classes using Spring Boot’s LocalServerPort have been updated to use the correct package.
  • Verified that temporary file and directory creation in tests work as expected using JUnit 5's @TempDir.

@elmiomar elmiomar requested review from RayPlante and deoyani October 9, 2024 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants