I have a Book model class in my application that looks like this:
@Entity
public class Book extends PanacheEntity{
public String title;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
In my test class, I create an instance of this class and call persistAndFlush(). Next, I make an HTTP call with rest assured to the controller. I pass the id of the newly created and persisted entity as path param to the controller.
@QuarkusTest
public class GreetingResourceTest {
@Test
@Transactional
public void testBookEndpoint() {
Book book= new Book();
book.title="Quarkus is awesome";
book.persistAndFlush();
assertTrue(book.isPersistent());
given()
.when().get("/hello/"+book.id)
.then()
.statusCode(200)
.body(is("Quarkus is awesome"));
}
}
The controller gets the request successfully. It receives the book and the id. BUT: when the controller query the database, the entity doesn't exist.
The full code below:
@Path("/hello")
public class GreetingResource {
@GET
@Path("/{id}")
@Produces(MediaType.TEXT_PLAIN)
public String getBook(@PathParam("id") Long id) {
Book book=Book.findById(id);
return book.title; //NullPointerException. Book cannot be found
}
}
Book.findById(id)
returns null. Why?