Discussion:
Exceptions not being thrown when using JerseyTest
Erik Holstad
2014-03-24 21:26:52 UTC
Permalink
I have a servlet that looks something like:

@POST
@Path("")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response createUser(@FormParam(Params.NAME)
RequiredString name) {

name.toString();

return Response.ok().build();
}


Where RequiredString validates the input and throws a
WebApplicationException if it fails.

This works fine when I deploy the code to a server or even when I use the
maven-jetty-plugin to run integration tests.

The problem occurs when I run this using the JerseyTest class. Everything
is fine when I pass in valid data into name, but as soon as I pass in empty
string or null I get a NPE from the toString() method on the name variable
instead of the WebApplicationException I would expect.


When stepping through the code I see it going in to the code where the
exception is being thrown, but it looks like it just fails silently and
just continues the execution.

Thoughts?
--
Regards Erik
Vetle Leinonen-Roeim
2014-03-25 12:26:53 UTC
Permalink
Hi,

What test container factory are you using? Is there a difference if you
another test container factory?

Regards,
Vetle
Post by Erik Holstad
@POST
@Path("")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
RequiredString name) {
name.toString();
return Response.ok().build();
}
Where RequiredString validates the input and throws a
WebApplicationException if it fails.
This works fine when I deploy the code to a server or even when I use the
maven-jetty-plugin to run integration tests.
The problem occurs when I run this using the JerseyTest class. Everything
is fine when I pass in valid data into name, but as soon as I pass in empty
string or null I get a NPE from the toString() method on the name variable
instead of the WebApplicationException I would expect.
When stepping through the code I see it going in to the code where the
exception is being thrown, but it looks like it just fails silently and
just continues the execution.
Thoughts?
Erik Holstad
2014-03-26 00:29:19 UTC
Permalink
I tested the Grizzly, Jdk and InMemory factories and non of them are
throwing the NPE, just seems like the fail silently, cause don't see any
errors in the logs.
Post by Vetle Leinonen-Roeim
Hi,
What test container factory are you using? Is there a difference if you
another test container factory?
Regards,
Vetle
Post by Erik Holstad
@POST
@Path("")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
RequiredString name) {
name.toString();
return Response.ok().build();
}
Where RequiredString validates the input and throws a
WebApplicationException if it fails.
This works fine when I deploy the code to a server or even when I use the
maven-jetty-plugin to run integration tests.
The problem occurs when I run this using the JerseyTest class. Everything
is fine when I pass in valid data into name, but as soon as I pass in
empty
Post by Erik Holstad
string or null I get a NPE from the toString() method on the name
variable
Post by Erik Holstad
instead of the WebApplicationException I would expect.
When stepping through the code I see it going in to the code where the
exception is being thrown, but it looks like it just fails silently and
just continues the execution.
Thoughts?
--
Regards Erik
Vetle Leinonen-Roeim
2014-03-26 07:57:38 UTC
Permalink
Perhaps you could try to create a complete test case and share it with
us on Github?
Post by Erik Holstad
I tested the Grizzly, Jdk and InMemory factories and non of them are
throwing the NPE, just seems like the fail silently, cause don't see any
errors in the logs.
Post by Vetle Leinonen-Roeim
Hi,
What test container factory are you using? Is there a difference if you
another test container factory?
Regards,
Vetle
Post by Erik Holstad
@POST
@Path("")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
RequiredString name) {
name.toString();
return Response.ok().build();
}
Where RequiredString validates the input and throws a
WebApplicationException if it fails.
This works fine when I deploy the code to a server or even when I use the
maven-jetty-plugin to run integration tests.
The problem occurs when I run this using the JerseyTest class. Everything
is fine when I pass in valid data into name, but as soon as I pass in
empty
Post by Erik Holstad
string or null I get a NPE from the toString() method on the name
variable
Post by Erik Holstad
instead of the WebApplicationException I would expect.
When stepping through the code I see it going in to the code where the
exception is being thrown, but it looks like it just fails silently and
just continues the execution.
Thoughts?
Erik Holstad
2014-03-26 21:26:55 UTC
Permalink
I have been able to isolate the issue, but I don't think it has anything to
do with test framework.

The issue occurs when you are sending in an empty String "" instead of a
value or null.

For the null case I had to add @NotNull to make it work, but when passing
in "" I think it thows a NPE inside
"throw new WebApplicationException(response);" which for some reason just
makes the code flow continue getting to an actual NPE in my code.
Post by Vetle Leinonen-Roeim
Perhaps you could try to create a complete test case and share it with
us on Github?
Post by Erik Holstad
I tested the Grizzly, Jdk and InMemory factories and non of them are
throwing the NPE, just seems like the fail silently, cause don't see any
errors in the logs.
Post by Vetle Leinonen-Roeim
Hi,
What test container factory are you using? Is there a difference if you
another test container factory?
Regards,
Vetle
Post by Erik Holstad
@POST
@Path("")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
RequiredString name) {
name.toString();
return Response.ok().build();
}
Where RequiredString validates the input and throws a
WebApplicationException if it fails.
This works fine when I deploy the code to a server or even when I use
the
Post by Erik Holstad
Post by Vetle Leinonen-Roeim
Post by Erik Holstad
maven-jetty-plugin to run integration tests.
The problem occurs when I run this using the JerseyTest class.
Everything
Post by Erik Holstad
Post by Vetle Leinonen-Roeim
Post by Erik Holstad
is fine when I pass in valid data into name, but as soon as I pass in
empty
Post by Erik Holstad
string or null I get a NPE from the toString() method on the name
variable
Post by Erik Holstad
instead of the WebApplicationException I would expect.
When stepping through the code I see it going in to the code where the
exception is being thrown, but it looks like it just fails silently and
just continues the execution.
Thoughts?
--
Regards Erik
Erik Holstad
2014-03-26 21:49:55 UTC
Permalink
Adding @NotNull actually seems to have resolved the issue even when passing
in "" and not just null values
Post by Erik Holstad
I have been able to isolate the issue, but I don't think it has anything
to do with test framework.
The issue occurs when you are sending in an empty String "" instead of a
value or null.
in "" I think it thows a NPE inside
"throw new WebApplicationException(response);" which for some reason just
makes the code flow continue getting to an actual NPE in my code.
Post by Vetle Leinonen-Roeim
Perhaps you could try to create a complete test case and share it with
us on Github?
Post by Erik Holstad
I tested the Grizzly, Jdk and InMemory factories and non of them are
throwing the NPE, just seems like the fail silently, cause don't see any
errors in the logs.
Post by Vetle Leinonen-Roeim
Hi,
What test container factory are you using? Is there a difference if you
another test container factory?
Regards,
Vetle
Post by Erik Holstad
@POST
@Path("")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
RequiredString name) {
name.toString();
return Response.ok().build();
}
Where RequiredString validates the input and throws a
WebApplicationException if it fails.
This works fine when I deploy the code to a server or even when I use
the
Post by Erik Holstad
Post by Vetle Leinonen-Roeim
Post by Erik Holstad
maven-jetty-plugin to run integration tests.
The problem occurs when I run this using the JerseyTest class.
Everything
Post by Erik Holstad
Post by Vetle Leinonen-Roeim
Post by Erik Holstad
is fine when I pass in valid data into name, but as soon as I pass in
empty
Post by Erik Holstad
string or null I get a NPE from the toString() method on the name
variable
Post by Erik Holstad
instead of the WebApplicationException I would expect.
When stepping through the code I see it going in to the code where the
exception is being thrown, but it looks like it just fails silently
and
Post by Erik Holstad
Post by Vetle Leinonen-Roeim
Post by Erik Holstad
just continues the execution.
Thoughts?
--
Regards Erik
--
Regards Erik
Loading...