0

I`ve been coding some REST API tests, and i dont know either i wrote my test wrong or my POST method is invalid. Im getting this error when i perform the test:

ERROR [2015-12-14 21:37:00,309] org.glassfish.grizzly.servlet.ServletHandler: service exception: ! java.lang.NullPointerException: null ! at resources.ActorResource.postActor(ActorResource.java:31) ...etc

and i have no idea where it came from. Actor object in the tests is properly initialized, it all should have worked fine.

Here is the post method :

    @POST
    @UnitOfWork
    public Saying postActor(@HeaderParam("actorName") String name, @HeaderParam("actorBirthDate") String birthDate) {
        Actor actor = dao.create(new Actor(name,birthDate));
        return new Saying("Added : " + actor.toString());
    }

And here is the test i wrote

    @Test
    public void postActor() {
        when(movieDAO.create(actor)).thenReturn(actor);
        final Response response = RULE.getJerseyTest().target("/actors").request().post(Entity.text(""));

        assertThat(response.getStatusInfo().getStatusCode()).isEqualTo(Response.Status.OK.getStatusCode());
        verify(movieDAO).create(actor);
    }

EDIT Here`s Actor initialization method

    @Before
    public void setup() {
        actor = new Actor();
        actor.setId(1L);
        actor.setBirthDate("12-03-2014");
        actor.setName("Karol Marks");
    }

And Actor class

@Entity
@Table(name="Actors")
public class Actor {
    private long id;
    private String name;
    private String birthDate;
    private Movie movie;

    public Actor() {
    }

    public Actor(String name, String birthDate) {
        this.name = name;
        this.birthDate = birthDate;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(name = "name", nullable = false)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "birthDate", nullable = false)
    public String getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(String birthDate) {
        this.birthDate = birthDate;
    }

    @ManyToOne(cascade = CascadeType.ALL)
    public Movie getMovie() {
        return movie;
    }

    public void setMovie(Movie movie) {
        this.movie = movie;
    }

    @Override
    public String toString() {
        return "Actor{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", birthDate='" + birthDate + '\'' +
                '}';
    }
VanDavv
  • 836
  • 2
  • 13
  • 38

2 Answers2

2

exception: ! java.lang.NullPointerException: null ! at resources.ActorResource.postActor(ActorResource.java:31) ...etc means that a NullPointerException was thrown by code in ActorResource, line 31 of the Java source file.

You are getting a NullPointerException because your code at run-time is trying to invoke a method on an object but the object is actually null.

As I can't see the line numbers for ActorResource then

  • if Actor actor = dao.create(new Actor(name,birthDate)); is line 31 of ActorResource then dao must be null and invoking dao.create(…) causes the NullPointerException (which means dao is not yet initialized);
  • otherwise if return new Saying("Added : " + actor.toString()); is line 31 of ActorResource then actor must be null and invoking actor.toString() causes the NullPointerException (which means dao.create(…) returned null).
mfulton26
  • 29,956
  • 6
  • 64
  • 88
  • 31 line is `return new Saying("Added : " + actor.toString());` line. So is it problem with the test method at `when(...)` ? – VanDavv Dec 14 '15 at 21:12
  • Then `actor` is `null` which means `dao.create(…)` is returning `null`. I suggest adding some logging and/or debugging your application to figure out why. – mfulton26 Dec 14 '15 at 21:13
1

You need an ArgumentMatcher like argThat, see ArgumentMatcher:

when(mock.addAll(argThat(new IsListOfTwoElements()))).thenReturn(true);

or you have to overwrite equals, see Argument matchers:

Be reasonable with using complicated argument matching. The natural matching style using equals() with occasional anyX() matchers tend to give clean & simple tests. Sometimes it's just better to refactor the code to allow equals() matching or even implement equals() method to help out with testing.

dur
  • 15,689
  • 25
  • 79
  • 125