quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.56k stars 2.62k forks source link

Panache Mocking for Kotlin based projects #12267

Open HeavyTobi opened 3 years ago

HeavyTobi commented 3 years ago

Description I recently found the quarkus-panache-mocking extension to be able to mock panache based entities in tests. I currently have an active project using Panache and Kotlin but it seems not to be possible to mock panache-kotlin based entities. I've tested it in Java, there everything is working fine. It would be great to also have this feature for Kotlin programmers :)

Example test I have used (test written in Java and Code in Kotlin):

package mycompany.project.service.impl;

import mycompany.project.entity.Category;
import mycompany.project.model.ListModel;
import io.quarkus.panache.mock.PanacheMock;
import org.junit.jupiter.api.Test;

import javax.inject.Inject;

import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

public class CategoryServiceImplTest
{
    @Inject
    CategoryServiceImpl categoryService;

    @Test
    void should_get_page_of_3_categories()
    {
        // given
        PanacheMock.mock(Category.class);
        when(Category.Companion.count()).thenReturn(30L); // fails on this line

        // when
        ListModel<Category> result = categoryService.getList(3, 0);

        // then
        assertEquals(result.getListInfo().getLengthTotal(), Category.Companion.count());
    }
}

expected result: should work ;)

actual result:

> Task :test FAILED

This method is normally automatically overridden in subclasses: did you forget to annotate your entity with @Entity?
java.lang.IllegalStateException: This method is normally automatically overridden in subclasses: did you forget to annotate your entity with @Entity?
    at io.quarkus.hibernate.orm.panache.common.runtime.AbstractJpaOperations.implementationInjectionMissing(AbstractJpaOperations.java:455)
    at io.quarkus.hibernate.orm.panache.kotlin.runtime.JpaOperations.implementationInjectionMissing(JpaOperations.java:193)
    at io.quarkus.hibernate.orm.panache.kotlin.PanacheKt.injectionMissing(Panache.kt:5)
    at io.quarkus.hibernate.orm.panache.kotlin.PanacheCompanion$DefaultImpls.count(PanacheCompanion.kt:396)

Implementation ideas forking the panache-mock extension and release a panache-mock-kotlin extension as done with panache-kotlin

quarkusbot commented 3 years ago

/cc @evanchooly /cc @FroMage, @loicmathieu

effusion commented 2 years ago

Hello Guys,

3 days ago I stumbled over the same issue. Is there any progress on this side?

FroMage commented 2 years ago

@evanchooly ?