Maiko Kingma Maiko Kingma - 6 months ago 26
reST (reStructuredText) Question

Jersey test gives NullPointerException when using UriInfo

I am creating a application that should be able to create an account using a REST api. When I wrote the
class everything worked fine. I used postman to verify that I got the right response.

So my
works correctly but the unit test I wrote with it don't work. When the unit test is executed it returns a
which results in a InternalServerError within the REST API. While debugging i found out that if I remove the use of UriInfo in
and return Response.ok() instead of created the test is successfull.

So my question is. How do i properly test/mock a method that returns and URI?

Bellow are all relevant classes and dependencies. The
class is in another package. But both the main application as the project that contains
are part of the same parent pom. I am not sure if this is relevant to my question.

public class AccountController {

private AccountService accountService;

private UriInfo uriInfo;

public Response create(Account account) {
final Account newAccount = accountService.create(account);
System.out.println("Pre uriinfo message");
//Removing this line and returning Response.ok() removes the exception
final URI uri = uriInfo.getAbsolutePathBuilder().path(newAccount.getId() + "").build();
System.out.println("Post uriinfo message");
return Response.created(uri).entity(newAccount).build();

public class AccountControllerTest extends JerseyTest {

private AccountService service;

protected Application configure() {
final ResourceConfig config = new ResourceConfig(AccountController.class);
config.register(new AbstractBinder() {
protected void configure() {
return config;

public void createUserTest() throws Exception {
final Account testAccount = new Account("testName", "");

System.out.println("Create account test");
//Create a new Account
final Response correctResult = target("/accounts")
Assert.assertEquals(HTTP_CREATED, correctResult.getStatus());
Assert.assertEquals(testAccount, correctResult.readEntity(Account.class));

public class Account {

@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(nullable = false)
private String fullName;
@Column(unique = true, nullable = false)
private String mail;
private boolean active;

public Account() { }

public Account(String fullName, String mail) {
this.fullName = fullName;
this.mail = mail;

//Contains getters&setters

Test dependencies

<!-- jersey version for "Jersey Media Json Jackson" & "Jersey Test Framework Provider Jetty" -->

<!-- -->

<!-- -->

<!-- -->

<!-- -->

<!-- -->

Answer Source

So I finally figured it out. Somewhere on this line in a NullPointerException was thrown:

final URI uri = uriInfo.getAbsolutePathBuilder().path(newAccount.getId() + "").build();

This only happened when using my JerseyUnitTest so i guess Jersey must change some behavior of UriInfo.

Eventually I solved the problem by overriding equals in

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download