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 + '\'' +
'}';
}