我有一个配置类如下:
@Configuration()
public class JsonConfiguration {
/** Default constructor. */
protected JsonConfiguration() {
}
/**
* Overrides the default jackson mapper builder to remove empty/null values from returned json.
* @return the customised jackson mapper.
*/
@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
Jackson2ObjectMapperBuilder b = new Jackson2ObjectMapperBuilder()
.serializationInclusion(Include.NON_EMPTY);
return b;
}
}
我需要对我的方法“jacksonBuilder”进行单元测试。
有人可以帮助我了解如何继续。
答案 0 :(得分:0)
好吧,我做了一些研究并找到了同样的答案。
以下是我为测试编写的代码,以防万一有人陷入同一点,请参阅以下内容:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class JsonConfigurationTest {
@Configuration
static class ContextConfiguration {
@Bean
public JsonConfiguration orderService() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Constructor<JsonConfiguration> constructor = JsonConfiguration.class
.getDeclaredConstructor(new Class[0]);
constructor.setAccessible(true);
JsonConfiguration conf= constructor.newInstance(new Object[0]);
return conf;
}
}
@Autowired
private JsonConfiguration configuration;
@Test
public void jacksonBuilderTest() {
Jackson2ObjectMapperBuilder builder = configuration.jacksonBuilder();
assertNotNull(builder);
}
}