Junit 5扩展,带有Spring @Import和@importautoconfiguration

发布于 2025-02-03 10:22:36 字数 1315 浏览 2 评论 0原文

我观看了有关通过扩展代替继承的演讲 https://wwwww.youtube.com/watch?v=v=v=v=v6_ria30yze

测试我的测试我Spring MVC控制器我们正在使用父级摘要类中进行初始化:

@Import({ControllerMappingConfiguration.class, Translator.class})
@ImportAutoConfiguration(MessageSourceAutoConfiguration.class)
public abstract class AbstractControllerTest {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    MockMvc mockMvc;

它注册了ModelMapper转换器并配置本地化。

我们正在将其用于继承:

@WebMvcTest(controllers = {BankExposureController.class})
class BankExposureControllerTest extends AbstractControllerTest {

它可以正常运行。

我尝试用扩展名替换此方法,但找不到一种方法来使@import@importautoconfiguration与Extension一起工作。

你知道何要这样做吗?

在此示例中,如果它有效,我将通过使用@importautoconfiguration对字段objectmappermockmvc进行改进。 我尝试以下扩展名:

@Import({ControllerMappingConfiguration.class, Translator.class})
@ImportAutoConfiguration(MessageSourceAutoConfiguration.class)
class AbstractControllerExtension implements Extension {

I watch this presentation about replacing inheritance by Extension
https://www.youtube.com/watch?v=V6_rIa30YzE

To test my Spring Mvc Controller we are doing initialisations in an parent abstract class:

@Import({ControllerMappingConfiguration.class, Translator.class})
@ImportAutoConfiguration(MessageSourceAutoConfiguration.class)
public abstract class AbstractControllerTest {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    MockMvc mockMvc;

It registers our ModelMapper converters and configures the localisation.

We are using it with inheritance:

@WebMvcTest(controllers = {BankExposureController.class})
class BankExposureControllerTest extends AbstractControllerTest {

And it works fine.

I try to replace this approach with an extension and I can't find a way to have the @Import and @ImportAutoConfigurationworking with Extension.

Do you know ho to do it ?

In this example, if it works, I will improve it by using @ImportAutoConfiguration for the fields objectMapper and mockMvc.
I try an extension like:

@Import({ControllerMappingConfiguration.class, Translator.class})
@ImportAutoConfiguration(MessageSourceAutoConfiguration.class)
class AbstractControllerExtension implements Extension {

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文