How do I configure a Json provider for a JAX-RS test client using Spring?
I have set up a JAX-RS service using Apache CXF, which I would like to test using Apache CXF JAX-RS' Client API. The server has been configured to use Jackson as the json provider. Now I would like to do the same for the client: that is, letting Jacson handle the conversion to/from json.
Unfortunately I am unsure of how to "spring enable" the tests, having only superficial knowledge of Spring. I got away with programmatically setting up a provider in the test, but would like to know how to do it through Spring. So instead of
@BeforeClass
public static void setup() {
ProviderFactory.getSharedInstance().registerUserProvider(new JacksonJsonProvider());
}
I would want to just set it in springConfiguration.xml (or something like that :). When setting up the serverside, springConfiguration.xml looked like this.
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"/>
<jaxrs:server id="restContainer" address="/">
<jaxrs:serviceBeans>
<ref bean="echoServi开发者_如何学运维ce"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<ref bean="jsonProvider" />
</jaxrs:providers>
<jaxrs:extensionMappings>
<entry key="json" value="application/json"/>
</jaxrs:extensionMappings>
</jaxrs:server>
I tried to just add
<jaxrs:client id="restClient" >
<jaxrs:providers>
<ref bean="jsonProvider" />
</jaxrs:providers>
</jaxrs:client>
to springConfiguration.xml, but that did not do anything. No surprise, as I have not set up my JUnit tests to use Spring ... Could anyone tell me how to do this, or point me to any good resources with which I could assemble the necessary info?
The following annotations will load your application context prior to tests being run.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"applicationContext.xml"})
public class JaxRsTest {
@Autowired
MyBean myBean
public void testMyBean() {
//add some real tests here...
assertNotNull(myBean)
}
}
精彩评论