0
votes

I have this simple OrderResource class that I would like to create unit tests for. I'm qusing Quarkus with Hibernate and Panache. What I don't understand is how do i get the function getOrders in my test? How can I mock the response without running this through a live system? I've currently added the quarkus-panache-mock dependency but i can't figure out how to mock the resource

Should the return values come from some service class? and then be injected into the test?


@Entity
public class Order extends PanacheEntity {
    public Long id;
    public String summary;
    public String description;
}
@Path("orders")
public class OrderResource {

    @GET
    @Path("orders")
    public String getAllOrders() {
        return Order.listAll();
    }

    @POST
    @Transactional
    public Response create(Order order) {
        order.persist();
        return Response.created(URI.create("/orders/" + order.id)).build();
    }


}

I tried creating a test like this:


    @Test
    public void testCreateOrder() {
        OrderResource orderResource = new OrderResource();
        System.out.println(orderResource.getAllOrders().get(0).name); //uses the live system
    }

But this is not a unit test but rather an integration test since it takes data from the real database.

I do not understand how the panache repository plays into this. Right now, the resource does not call any dependent service. What are you trying to test? What is the given? What is the when? What is the then?Turing85
@Turing85 well this is a simple case, so in this case I would like to test that the methods return the correct values. But without using the live system. So I wanna create unit tests for each method. when getOrders is called with orderId 1 then we should get "orderId: 1"Kinder9211
final String actual = new OrderResource().getOrders("foo"); assertThat(actual, is("orderId: foo")); (or whatever framework you are using to write assertions)Turing85
@Turing85 but wont that activate the whole system and make an actual get? I just want to unit test the logic.Kinder9211
No. It just creates an instance of the class and calls a method.Turing85