cxf-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sergey Beryozkin <sberyoz...@gmail.com>
Subject Re: Unable to inject @context from junit test case
Date Fri, 19 Nov 2010 16:37:42 GMT
Hi

sorry it's taken me long to reply.
I've tried your test case today and I was able to confirm a certain
limitation in the way contexts injection is handled.
Note that in your server setup code you use an IService interface to set the
resource class and provide a concrete implementation ServiceImpl as well.

It is the fact you start from the interface that makes it impossible for CXF
to figure out if some fields of the concrete class have Context annotations
or not. But the concrete class is also available so this issue has been just
fixed on the mainline.

Usually it is the job of CXF JAX-RS to find out if a concrete resource class
happens to be a JAX-RS root resource class by checking the subclasses and
interfaces. DOSGI RI actually follows the approach you use by starting from
the interface and then turning concrete implementations into JAX-RS
resources dynamically - so if you do need to have something similar then use
the interface directly, otherwise just register a concrete class :

sf.setResourceClasses(ServiceImpl.class);

thanks, Sergey

On Wed, Nov 17, 2010 at 3:23 PM, DonDiego761
<romain.pruvost@capgemini.com>wrote:

>
> I have an interface :
> @Produces(MediaType.APPLICATION_XML)
> public interface IService {
>
>        @GET
>        @Path("{id}")
>        Response getObjects(@PathParam(value = "id") final String id);
>
> }
>
> Its implementation:
> public class ServiceImpl {
>
>        @Context
>        private UriInfo uriInfo = null;
>
>        @Override
>        public final Response getObjects(@PathParam(value = "id") final
> String id)
> {
>             .....
>        }
> }
>
> And in my Junit test case :
> public class Test {
>
>        @Test
>        public void getElementsNominal() {
>               startRestServer(...);
>               .....
>       }
>
>       private Server startRestServer(final String urlServeur, final String
> restServicePath) {
>                JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
>                sf.setResourceClasses(IService.class);
>                sf.getInInterceptors().add(new LoggingInInterceptor());
>                sf.getOutInterceptors().add(new LoggingOutInterceptor());
>                ServiceImpl service = new ServiceImpl ();
>                sf.setResourceProvider(IService.class, new
> SingletonResourceProvider(service));
>                sf.setAddress(urlServeur + restServicePath);
>                Server server = sf.create();
>                LOGGER.info("Rest Server started @ " + sf.getAddress());
>                return server;
>        }
> }
>
> I used JUnit 4 and cxf v2.2.6.
>
> The problem is the variable uriInfo (in the ServiceImpl class) is null when
> I call a service via my JUnit test.
>
> I hope I was clear.
>
> --
> View this message in context:
> http://cxf.547215.n5.nabble.com/Unable-to-inject-context-from-junit-test-case-tp3269105p3269186.html
> Sent from the cxf-user mailing list archive at Nabble.com.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message