users@jersey.java.net

[Jersey] Re: Problems with post parameters in tests

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Sat, 10 Mar 2012 12:01:31 +0100

You'll need to share your test environment description with us then.

Are you using Jersey Test Framework? Or.. and that would be most likely
the best approach - can you share runnable testcase?

Pavel

On 3/10/12 11:28 AM, Ludwig Magnusson wrote:
> My code looks exactly like that, i.e:
> @POST
> public String post(@QueryParam("foo") String foo) {
> //code
> // value of foo is null here
> }
> The thing is, if I access the method outside tests and make a HTTP
> POST request to the address with the post paramteters I am able to
> retreive them properly. It is only in the tests that no parameters are
> received.
> Apart from doing as in the code snippet above i have tried to extract
> parameters from the parameterMap in the HttpServlerRequest object and
> it is always completely empty. This is not the case outside the tests.
> The parameters are there if I make a reqular post to the addres via a
> browser.
> /Ludwig
> *From:* Pavel Bucek <mailto:pavel.bucek_at_oracle.com>
> *Sent:* Saturday, March 10, 2012 10:34 AM
> *To:* users_at_jersey.java.net <mailto:users_at_jersey.java.net>
> *Subject:* [Jersey] Re: Problems with post parameters in tests
> On 3/9/12 8:55 PM, Ludwig Magnusson wrote:
>> Hi!
>> I am having some trouble with setting up my first test case for
>> jersey. The problem is that post/get paramaters I want to use in my
>> tests. None of the parameters are included in the HttpServlerRequest
>> on the receiving side. Everything else works such as post/get,
>> content types and the different paths.
>> I use the grizzly server for my tests.
>> My test class extends JerseyTest and this is the code for my test
>> that sends the post:
>> WebResource webResource = resource().path("/mypath");
>> webResource.queryParam(“foo”, "bar");
>> String responseMsg = webResource.post(String.class);
>> I have also tried:
>> WebResource webResource = resource().path("/mypath");
>> Form form = new Form();
>> form.add(“foo”, “bar”);
>> String responseMsg = webResource.post(String.class, form)
>> Is there something worng with my tests?
>
> most likely yes.
>
> Can you please post the code where you're trying to access query params?
>
> hint: it should look similarly to this:
>
> @Path("mypath")
> public class MyResource {
>
> ...
>
> @POST
> public String post(@QueryParam("foo") String foo) {
> return "received query param foo = \"" + foo + "\"";
> }
>
> }
>
> See "Extracting Request Parameters" chapter in our user guide:
> http://jersey.java.net/nonav/documentation/latest/user-guide.html#d4e247
>
> Pavel
>
>
>> /Ludwig
>