0

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.

7
  • 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? Commented Jan 4, 2022 at 22:56
  • @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" Commented Jan 4, 2022 at 23:02
  • final String actual = new OrderResource().getOrders("foo"); assertThat(actual, is("orderId: foo")); (or whatever framework you are using to write assertions) Commented Jan 4, 2022 at 23:04
  • @Turing85 but wont that activate the whole system and make an actual get? I just want to unit test the logic. Commented Jan 5, 2022 at 9:49
  • No. It just creates an instance of the class and calls a method. Commented Jan 5, 2022 at 9:52

1 Answer 1

1

Following the official documentation, i would recommend PanacheMock to achieve the appropriate behavior in unit tests:

@QuarkusTest
public class OrderResourceTest {

    @Test
    public void myTest() {
        PanacheMock.mock(Order.class);
        OrderResource orderResource = new OrderResource();

        // force return value of count()
        Mockito.when(Order.count()).thenReturn(23L);    
        System.out.println(Order.count());
        
        // force return value of listAll()
        Mockito.when(Order.listAll()).thenReturn(Collections.emptyList());    
        System.out.println(orderResource.getAllOrders());
    }
}

you also need to add the dependency to your maven pom:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-panache-mock</artifactId>
    <scope>test</scope>
</dependency>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.