Junit 5扩展,带有Spring @Import和@importautoconfiguration
我观看了有关通过扩展代替继承的演讲 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
对字段objectmapper
和mockmvc
进行改进。 我尝试以下扩展名:
@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 @ImportAutoConfiguration
working 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论