users@jersey.java.net

[Jersey] Re: Unit Testing with Spring

From: Jonathan Cook - FM&T <Jonathan.Cook2_at_bbc.co.uk>
Date: Wed, 12 Jan 2011 13:22:11 +0000

That was it :)

Now I would like to be able to mock out that autowired bean. I came across
this but it looks like a bit of work, is there anything neater?
http://jersey.576304.n2.nabble.com/unit-test-a-jersey-resource-with-mocked-s
pring-beans-td581233.html

Thanks


On 12/01/2011 12:33, "Jonathan Cook - FM&T" <Jonathan.Cook2_at_bbc.co.uk>
wrote:

> Just read this:
> http://jersey.576304.n2.nabble.com/Jersey-test-and-Spring-td5607651.html
>
> Perhaps that is the reason?
>
> Thanks
>
>
> On 12/01/2011 12:28, "Jonathan Cook - FM&T" <Jonathan.Cook2_at_bbc.co.uk> wrote:
>
>> Hi,
>>
>> I have the following simple example unit test:
>>
>> public class TableResourceTest extends JerseyTest {
>>
>> public TableResourceTest() throws Exception {
>> super(new WebAppDescriptor.Builder("bbc.forge.statsapi")
>> .contextPath("football")
>> .contextParam("contextConfigLocation",
>> "classpath:applicationContext.xml")
>> .contextParam("log4jConfigLocation",
>> "classpath:log4j.properties")
>> .contextParam("propertiesConfigLocation",
>> "classpath:propertiesConfigContext.xml")
>> .contextListenerClass(ContextLoaderListener.class)
>> .requestListenerClass(RequestContextListener.class)
>> .servletClass(SpringServlet.class).build());
>> }
>>
>> @Test
>> public void testTable() throws Exception {
>>
>> WebResource webResource = resource();
>> String responseMsg =
>> webResource.path("/football/table/competition/100").get(String.class);
>> Assert.assertEquals("Some Text", responseMsg);
>> }
>> }
>>
>> However on running the test I get a NullPointerException. The resource I am
>> invoking has an AutoWired property which is in the applicationContext.xml.
>> But it appears the http container isnıt initialising spring at all. Iım not
>> really sure why. I have looked through some of the samples such as
>> spring-annotations and it doesnıt appear to be any different.
>>
>> The container is initialising the resources ok so I wasnıt sure why it wasnıt
>> picking up the applicationContext.xml. Or maybe it is and this doesnıt work
>> with autowiring?
>>
>> INFO: Scanning for root resource and provider classes in the packages:
>> bbc.forge.statsapi
>> Jan 12, 2011 12:19:55 PM com.sun.jersey.api.core.ScanningResourceConfig
>> logClasses
>> INFO: Root resource classes found:
>>
>> Thanks
>> Jon