quarkus: java.lang.IllegalStateException: This method is normally automatically..... in IT tests

I got this Exception running IT tests with ‘mvn test’:

java.lang.IllegalStateException: This method is normally automatically overridden in subclasses: did you forget to annotated your entity with @Entity?

this happen when I call this simple method of OrdiniService injected in the test:

@Path("/ordini")
@Slf4j
@Validating
@Authorizing
@Dependent
public class OrdiniService implements PanacheRepositoryBase<Ordine, String> {
    @GET
    @NoCache
    @Path("{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Ordine findOrdineById(@PathParam("id") @NotNull String id) {

        return findById(id);
    }

All works as expected when I run this method through the JAX-RS interface with the ‘quarkus:dev’ goal.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 12
  • Comments: 43 (34 by maintainers)

Commits related to this issue

Most upvoted comments

I found an interesting, somehow clean workaround:

@QuarkusTest
class UserRepositoryTest {
    @Inject
    UserRepositoryWrapper userRepository;

    @Test
    void persist() {
        final User user = User.builder()
                              .firstName("Naruto")
                              .lastName("Uzumaki")
                              .age(10)
                              .build();

        userRepository.persist(user);

        assertNotNull(user.getId());
    }

    @Test
    void listAll() {
        final List<User> users = userRepository.listAll();
        assertFalse(users.isEmpty());
    }

    @Test
    void name() {
        final User user = userRepository.find("firstName", "Naruto").firstResult();
        assertNotNull(user);
    }

    /**
     * Workaround to fix https://github.com/quarkusio/quarkus/issues/1724.
     */
    @ApplicationScoped
    @Transactional
    static class UserRepositoryWrapper {
        @Inject
        @Delegate
        UserRepository userRepository;
    }
}

This is using lombok @Delegate so you call the methods directly from the Wrapper. Once this is fixed, only the type needs to be changed. Alternative, a get method to the UserRepository on the inner class can be exposed and it also seems to work.

That won’t help when running from an IDE.

I will need to look into this. JUnit 5 is actually really restrictive around this, in a lot of ways it is less extensible than JUnit 4.

Yes!

Well sort of, they have not released it yet, but it will be integrated into the new ClassLoader work at https://github.com/stuartwdouglas/quarkus/tree/new-class-loading . Even if the timing does not work we can just document how to work around the double @BeforeAll.

I’ve just hit the same issue (Quarkus version 0.28.1). Basically I have a Book Panache entity:

@Entity
public class Book extends PanacheEntity {

  public String title;
  public String description;
  public Float price;
  ....
}

Manipulated by a transactional service :

@ApplicationScoped
@Transactional(SUPPORTS)
public class BookService {

  @Transactional(REQUIRED)
  public Book createBook(Book book) {
    Book.persist(book);
    return book;
  }

  public Book findBook(Long id) {
    return Book.findById(id);
  }
}

And the Quarkus test injects the service:

@QuarkusTest
public class BookServiceTest {

  @Inject
  private BookService bookService;

  @Test
  public void shouldCreateABook() throws Exception {

    // tag::shouldCreateABook[]
    Book book = new Book().title("Java EE 7").price(23.5F).isbn("1-84023-742-2").nbOfPages(354);

    book = bookService.createBook(book);
    assertNotNull(book.id, "Id should not be null");
  }
}

The test fails and Quarkus complains about the findById method (I do not have any @BeforeAll):

java.lang.IllegalStateException: This method is normally automatically overridden in subclasses: did you forget to annotate your entity with @Entity?