users@jersey.java.net

[Jersey] Re: _at_Context HttpServletRequest not injected in Grizzly2 JerseyTest

From: Stevo Slavić <sslavic_at_gmail.com>
Date: Fri, 31 Oct 2014 17:14:33 +0100

Thanks Paul, for pointing me in right direction.

Maybe I'm wrong, but from what I figured out, I had to override two methods
in my JerseyTest,

    @Override
    protected TestContainerFactory getTestContainerFactory()
    {
        return new GrizzlyWebTestContainerFactory();
    }

    @Override
    protected DeploymentContext configureDeployment()
    {
        return ServletDeploymentContext.builder(configure()).build();
    }



Since I'm also using Spring, I added to the
ServletDeploymentContext.Builder instance

                .addListener(ContextLoaderListener.class)
                .contextParam("contextClass",
"org.springframework.web.context.support.AnnotationConfigWebApplicationContext")
                .contextParam("contextConfigLocation",
"some.package.MockSpringJavaConfig")


That last one, configuring contextConfigLocation, works the same if
configured in JerseyTest configure() with
                application.property("contextConfigLocation",
"some.package.MockSpringJavaConfig");

Not sure if there is equivalent Application/ResourceConfig property for
configuring contextClass. Is it documented somewhere?

Anyway, problem I'm facing now, is how to get a bean defined in Spring
context injected in a field annotated with @Inject or @Autowired in my
JerseyTest. Is that possible?
It would help if I could at least get access to Spring context instance. Is
there a method to do that within JerseyTest?
Bean I'm trying to gain access to is a mock object that I'd like to
configure and verify differently in test methods.

Wish there were more docs on Spring integration. Just
https://jersey.java.net/documentation/2.13/spring.html with example which
has absolutely no tests, does not help.

Kind regards,
Stevo Slavic.


On Thu, Oct 30, 2014 at 2:17 AM, Paul O'Fallon <paul_at_ofallonfamily.com>
wrote:

> From the Jersey docs (
> https://jersey.java.net/documentation/latest/test-framework.html#d0e15530)
> it looks like Grizzly supports two Test Container Factories. You'll want
> to be sure you're using the GrizzlyWebTestContainerFactory in order to get
> Servlet support.
>
> Hope this helps!
>
> - Paul
>
> On Wed, Oct 29, 2014 at 6:52 PM, Stevo Slavić <sslavic_at_gmail.com> wrote:
>
>> Hello Jersey community,
>>
>> In ExceptionMapper implementation I have HttpServletRequest field,
>> annotated with @Context. When running application in servlet container,
>> field gets injected appropriately.
>>
>> In a resource test extending JerseyTest, with
>> jersey-test-framework-provider-grizzly2 provider, exception gets thrown and
>> mentioned exception mapper handles the exception. Problem is that the
>> request does not get injected - it is null.
>> Same exception mapper has UriInfo field, also annotated with @Context,
>> and in that test it gets injected well.
>>
>> Am I doing something wrong? Are my expectations wrong that this should
>> work? Should I enable something? Is this a bug?
>>
>> I'm using jersey 2.13
>>
>> Kind regards,
>> Stevo Slavic.
>>
>
>