问题描述
我有一个 spring boot 应用程序,它有自定义的 Json Serializer/Deserializer,它被注册为这样的 bean:
public class JsonSerializationComponent extends SimpleModule {
public JsonSerializationComponent() {
super("MySerializationModule");
addSerializer(Instant.class,new InstantSerializer());
addDeserializer(Instant.class,new InstantDeserializer());
addSerializer(Hash.class,new HashSerializer());
addDeserializer(Hash.class,new HashDeserializer());
addDeserializer(DateTime.class,new DateTimeDeserializer());
addDeserializer(DMRestVolumeTime.class,new DMRestVolumeTimeDeserializer());
}
}
运行应用程序时,这按预期工作。
问题是当我运行集成测试时。我使用 Mock webAppContext 和以下测试设置:
@RunWith(SpringJUnit4ClassRunner.class)
@EnableWebMvc
@WebAppConfiguration
@ContextConfiguration(
classes = {
MyTestConfig.class
},initializers = {
ConfigDataApplicationContextinitializer.class
})
@ActiveProfiles({"test"})
public class MyIntegrationTest {
@Autowired
private WebApplicationContext webApplicationContext;
private String volumeId;
@Before
public void setup() throws Exception {
this.mockmvc = mockmvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}
@Test
public void mytest() throws Exception {
mockmvc.perform(get("/someEndpoint"))
.andDo(print())
.andExpect(status().isOk());
... more testing code here
}
...
}
尽管它已在 Spring 上下文中注册(已使用调试器进行了验证),但在将响应发送回测试客户端时,spring 控制器并未使用自定义 JSON Serializer/Deserializer。
我意识到以这种方式设置测试提供了一个简化的 Spring 环境(当使用这个 Mock 设置而不是使用 @SpringBoottest
时)。我这样做是为了让测试运行得更快一点,而且没有那么多内存使用。
如何让测试使用 JSON Serializer/Deserializer 与此设置?这可能吗?
注意:如果我将测试注释更改为:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBoottest(classes = MyApplication.class)
@WebAppConfiguration
@Import(MyTestConfiguration.class)
@Rollback
@Transactional
正如我所提到的,这会启动一个我试图避免的完整的 spring 应用程序。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)