Hello SF Developers
Just being curious - is there an official guide or best practices about how to migrate all the Test classes from Spring Framework to Spring Boot? The latter has some specific set of annotations and settings (@SpringBootTest
). Therefore - consider a SF project with more 500 of Test classes and later it has green light to be migrated to SB. How migrate the complete set of test classes with the minor impact or refactoring?
- For example If my memory does not fail me, I remember that from the Spring Integration project, the module
spring-messaging
was moved to Spring Framework. - Or perhaps - Would be created a new special annotation to be shared between these 2 projects? (SF=SB) - Really the reason to create this post - therefore the testing classes are totally agnostic in what framework would be running
Thanks for your understanding
Comment From: sbrannen
Although this type of question is probably better suited for Stack Overflow (and I'm labeling it as such), I talked it over with @wilkinsona to gain a Spring Boot perspective, and we came up with the following.
Spring does not provide any kind of tooling or guidelines for migrating a test suite from Spring Framework (SF) to Spring Boot (SB). Spring Boot does provide guidance for how to Convert an Existing Application to Spring Boot. So it's conceivable that some of the following tips could be added to that guide.
Spring Boot's testing support builds on the support in spring-test
in SF. Thus, many of the integration tests in a SF test suite would continue to work unmodified (or only slightly modified) after a migration.
- The majority of the tests in a test suite are likely unit tests anyway, and those would not need to be migrated at all.
- Spring-related integration tests using the Spring TestContext Framework could be migrated to make use of
@SpringBootTest
as a first step. That would ensure that your integration tests make use of SB features that your application will likely begin to rely on. - At that point you could consider gradually migrating specific categories of integration tests to SB's "test slice" support (e.g.,
@WebMvcTest
,@DataJpaTest
, etc.) or making use of@MockBean
and@SpyBean
where applicable.
Comment From: manueljordan
Even when it would be a valid question or SO - I did not post it there because it would a opinion question/best practices. I mean, is not a technical question
I thought to create this post on SB - but because spring-test module is located here, I posted here this situation
My point was (in case was not clear) a Test class created on SF should be re-used in peace in SB without any impact - the ideal scenario. Of course, it is not possible yet. And of course respecting all your work - spring team. But I hope you see point.
Comment From: sbrannen
My point was (in case was not clear) a Test class created on SF should be re-used in peace in SB without any impact - the ideal scenario. Of course, it is not possible yet.
For some cases it would work without modification without any issues. It really depends on how many of Boot's features your application depends on and how your tests configure resources for the ApplicationContext
.
In some scenarios it should be sufficient to replace @SpringJUnitConfig(AppConfig.class)
with @SpringBootTest
(assuming AppConfig.class
was the central @Configuration
class for your application before the migration).
If you had previously modularized your configuration classes and included different sets of configuration classes in @SpringJUnitConfig
or @ContextConfiguration
across various test classes in your test suite, you may have to manually migrate those to any new structure you have come up with. Though, it still might be as simple as switching from @SpringJUnitConfig
or @ContextConfiguration
to @SpringBootTest
.
In summary, the tips we have provided above are a guideline for one way to approach such a migration, but in the end it may be a bit of trial and error depending on the nuances of your application and how you have chosen to migrate.
Comment From: manueljordan
I see.
The point was have a common api between them, for example
@SpringBootTest
(and with WebEnvironment)@MockBean
TestRestTemplate
Are specific to Spring Boot.
As ussual, thanks for the support