You are not adding query param in your test. Try following:
WebResource webResource = resource();
String responseMsg =
webResource.path("/test/foo").queryParam("bar",
"theparameter").post(String.class);
assertEquals("The bar parameter was: theparameter", responseMsg);
On 3/10/12 1:17 PM, Ludwig Magnusson wrote:
> Ok. I’ll put everything in the mail here. If you prefer attatched
> files, please tell me so and I will do that in the future.
> Below I display:
> 1 My dependencies declared in my pom.xml
> 2 The java-file with the method that receives the request
> 3 My test
> The result from my test is: metaTest(test.MetaTest): expected:<... bar
> parameter was: [theparameter]> but was:<... bar parameter was: [null]>
> These are my jersey-dependencies:
> Note. I have tried adding jersey-server as well but it makes no
> difference.
> <dependency>
> <groupId>com.sun.jersey</groupId>
> <artifactId>jersey-servlet</artifactId>
> <version>1.12</version>
> <scope>compile</scope>
> </dependency>
> <dependency>
> <groupId>com.sun.jersey.jersey-test-framework</groupId>
> <artifactId>jersey-test-framework-core</artifactId>
> <version>1.12</version>
> <type>jar</type>
> <scope>test</scope>
> </dependency>
> <dependency>
> <groupId>com.sun.jersey.jersey-test-framework</groupId>
> <artifactId>jersey-test-framework-grizzly</artifactId>
> <version>1.12</version>
> <type>jar</type>
> <scope>compile</scope>
> </dependency>
> This is my javafile:
> package test;
> import javax.ws.rs.POST;
> import javax.ws.rs.Path;
> import javax.ws.rs.QueryParam;
> @Path("/test")
> public class TestResource {
> @POST
> @Path("foo")
> public String foo(@QueryParam("bar") String bar) {
> return "The bar parameter was: " + bar;
> }
> }
> This is my test:
> package test;
> import static org.junit.Assert.assertEquals;
> import org.junit.Test;
> import com.sun.jersey.api.client.WebResource;
> import com.sun.jersey.api.representation.Form;
> import com.sun.jersey.test.framework.JerseyTest;
> public class MetaTest extends JerseyTest {
> public MetaTest() {
> super("test");
> }
> @Test
> public void metaTest() throws Exception {
> WebResource webResource = resource();
> Form form = new Form();
> form.add("bar", "theparameter");
> String responseMsg =
> webResource.path("/test/foo").post(String.class, form);
> assertEquals("The bar parameter was: theparameter", responseMsg);
> }
> }
> I hope this clears things up.
> /Ludwig
> *From:* Pavel Bucek <mailto:pavel.bucek_at_oracle.com>
> *Sent:* Saturday, March 10, 2012 12:01 PM
> *To:* users_at_jersey.java.net <mailto:users_at_jersey.java.net>
> *Subject:* [Jersey] Re: Problems with post parameters in tests
> 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
>>
>