如何测试jackson-jaxrs-json-provider(Arquillian + Wildfly)

时间:2016-02-09 09:28:45

标签: json jackson jax-rs wildfly-8 jboss-arquillian

我正在使用Jackson JSON提供程序来序列化/反序列化JAXRS请求。

为了设置它我在jboss-deployment-structure.xml文件夹下有一个WEB-INF文件:

<jboss-deployment-structure>
    <deployment>
        <exclusions>
            <module name="org.jboss.resteasy.resteasy-jackson-provider"/>
            <module name="org.jboss.resteasy.resteasy-jettison-provider"/>
        </exclusions>
        <dependencies>
            <module name="org.jboss.resteasy.resteasy-jackson2-provider" services="import"/>
        </dependencies>
    </deployment>
</jboss-deployment-structure>

所以,我已经建立了一个测试以获得它:

@RunWith(Arquillian.class)
public class FollowUpActivityDTOSerializationTest
{

    @Inject private ObjectMapper mapper;

    @Deployment
    public static WebArchive createDeployment()
    {   
        System.getProperties().remove("javax.xml.parsers.SAXParserFactory");
        EmbeddedGradleImporter importer = ShrinkWrap.create(EmbeddedGradleImporter.class);
        WebArchive war = importer.forThisProjectDirectory().importBuildOutput().as(WebArchive.class);

        war.addClass(ArquillianAlternative.class);
        war.addClass(MockFactory.class);
        war.addAsWebInfResource(
            new StringAsset("<alternatives><stereotype>com.living.mock.ArquillianAlternative</stereotype></alternatives>"),
            "beans.xml"
        );

        JavaArchive[] libs = Maven.resolver().resolve("org.mockito:mockito-core:2.0.31-beta").withTransitivity().as(JavaArchive.class);
        war.addAsLibraries(libs);

        return war;
    }

    @Test
    public void emptyJSON()
    {   
        String emptyJSON = "{\"id\": \"id\"}";

        try {
            FollowUpActivityDTO dto = this.mapper.readValue(emptyJSON, FollowUpActivityDTO.class);
            assertNotNull(dto);
            assertEquals(dto.getId(), "id");
        } catch (IOException e) {
            fail(e.getMessage());
        }
    }

}

问题是韦尔德告诉我:

  

带有限定符@Default

的ObjectMapper类型的不满意依赖项

问题是,如何获得杰克逊提供者?

2 个答案:

答案 0 :(得分:0)

这里最重要的是获得测试时间,与JAX-RS实现相同的ObjectMapper

这很重要,因为我在提供程序中配置了与此对象相关的一些设置:

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonConfig implements ContextResolver<ObjectMapper> {

    @Override
    public ObjectMapper getContext(final Class<?> type) {

        final ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        mapper.setSerializationInclusion(Include.NON_EMPTY);

        return mapper;
    }

}

因此,使用此provider非常重要。

所以,为了注入这个对象,我写了这个注入代码:

@Inject private ContextResolver<ObjectMapper> mapperResolver;

答案 1 :(得分:-1)

默认情况下,ObjectMapper没有生产者,除非您明确提供了一个。{1}}。如果使用ObjectMapper的实例化替换注入点,例如private ObjectMapper objectMapper = new ObjectMapper()您将避免注射问题。