Panache repository unit test doesn't return 1 on count

121 Views Asked by At

I'm trying to test true return on count > 0, but it doesn't return 1 to pass the assertion.

Repository interface

public interface BookRepository extends PanacheRepository<Book> {

    boolean existsBookByIsbn(String isbn);

}

Repository implementation

@ApplicationScoped
public class BookRepositoryImpl implements BookRepository {

    private static final String ISBN = "isbn";

    public boolean existsBookByIsbn(String isbn) {
        return count(ISBN, isbn) > 0;
    }

}

Two different test implementations I've tried

@QuarkusTest
class BookRepositoryImplTest {

    @InjectMock
    private BookRepositoryImpl bookRepositoryImpl;

    private static final String DEFAULT_STRING = "TEST";
    private static final long LONG_1 = 1L;

    @Test
    void existsBookByIsbn_TRUE() {
        when(bookRepositoryImpl.count(anyString(), anyString())).thenReturn(LONG_1); // 0 on method call
        when(bookRepositoryImpl.existsBookByIsbn(anyString())).thenCallRealMethod();
        boolean result = bookRepositoryImpl.existsBookByIsbn(DEFAULT_STRING);
        assertTrue(result); // false
    }

    @Test
    void existsBookByIsbn_TRUE_withPanacheQuery() {
        PanacheQuery query = mock(PanacheQuery.class);
        when(query.count()).thenReturn(LONG_1); // 0 on method call
        when(bookRepositoryImpl.find(anyString(), anyString())).thenReturn(query);
        when(bookRepositoryImpl.existsBookByIsbn(anyString())).thenCallRealMethod();
        boolean result = bookRepositoryImpl.existsBookByIsbn(DEFAULT_STRING);
        assertTrue(result); // false
    }
0

There are 0 best solutions below