[BAEL-4471] Quarkus Testing (#9894)
* [BAEL-4471] initial commit [BAEL-4471] commit [BAEL-4471] commit [BAEL-4471] commit [BAEL-4471] commit * [BAEL-4471] cosmetic fixes * [BAEL-4471] remove testcontainers * [BAEL-4471] PR comments fixes * [BAEL-4471] PR comments fixes * [BAEL-4471] assign different port per profile while testing * [BAEL-4471] rename unit to integration test
This commit is contained in:
parent
44d66d9a0f
commit
556d5d8eaa
|
@ -30,9 +30,48 @@
|
|||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-resteasy</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-resteasy-jackson</artifactId>
|
||||
<version>${quarkus.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-hibernate-orm-panache</artifactId>
|
||||
<version>${quarkus.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-jdbc-h2</artifactId>
|
||||
<version>${quarkus.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.6</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-junit5</artifactId>
|
||||
<version>${quarkus.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-junit5-mockito</artifactId>
|
||||
<version>${quarkus.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-test-h2</artifactId>
|
||||
<version>${quarkus.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -117,7 +156,8 @@
|
|||
|
||||
<properties>
|
||||
<surefire-plugin.version>2.22.0</surefire-plugin.version>
|
||||
<quarkus.version>0.15.0</quarkus.version>
|
||||
<quarkus.version>1.7.0.Final</quarkus.version>
|
||||
<junit-jupiter.version>5.6.0</junit-jupiter.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.quarkus;
|
||||
|
||||
import com.baeldung.quarkus.model.Book;
|
||||
import com.baeldung.quarkus.service.LibraryService;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import java.util.Set;
|
||||
|
||||
@Path("/library")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public class LibraryResource {
|
||||
|
||||
@Inject
|
||||
LibraryService libraryService;
|
||||
|
||||
@GET
|
||||
@Path("/book")
|
||||
public Set<Book> findBooks(@QueryParam("query") String query) {
|
||||
return libraryService.find(query);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.quarkus.model;
|
||||
|
||||
import io.quarkus.hibernate.orm.panache.PanacheEntity;
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@NoArgsConstructor(access = AccessLevel.PROTECTED)
|
||||
public class Book extends PanacheEntity {
|
||||
private String title;
|
||||
private String author;
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.quarkus.repository;
|
||||
|
||||
import com.baeldung.quarkus.model.Book;
|
||||
import io.quarkus.hibernate.orm.panache.PanacheRepository;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.quarkus.panache.common.Parameters.with;
|
||||
|
||||
@ApplicationScoped
|
||||
public class BookRepository implements PanacheRepository<Book> {
|
||||
|
||||
public Stream<Book> findBy(String query) {
|
||||
return find("author like :query or title like :query", with("query", "%"+query+"%")).stream();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.quarkus.service;
|
||||
|
||||
import com.baeldung.quarkus.model.Book;
|
||||
import com.baeldung.quarkus.repository.BookRepository;
|
||||
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.inject.Inject;
|
||||
import javax.transaction.Transactional;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.util.stream.Collectors.toSet;
|
||||
|
||||
@Transactional
|
||||
@ApplicationScoped
|
||||
public class LibraryService {
|
||||
|
||||
@Inject
|
||||
BookRepository bookRepository;
|
||||
|
||||
public Set<Book> find(String query) {
|
||||
if (query == null) {
|
||||
return bookRepository.findAll().stream().collect(toSet());
|
||||
}
|
||||
|
||||
return bookRepository.findBy(query).collect(toSet());
|
||||
}
|
||||
|
||||
}
|
|
@ -1,3 +1,12 @@
|
|||
# Configuration file
|
||||
# key = value
|
||||
greeting=Good morning
|
||||
greeting=Good morning
|
||||
|
||||
quarkus.datasource.db-kind = h2
|
||||
quarkus.datasource.jdbc.url = jdbc:h2:tcp://localhost/mem:test
|
||||
|
||||
quarkus.hibernate-orm.database.generation = drop-and-create
|
||||
|
||||
%custom-profile.quarkus.datasource.jdbc.url = jdbc:h2:file:./testdb
|
||||
|
||||
quarkus.http.test-port=0
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.quarkus;
|
||||
|
||||
import com.baeldung.quarkus.utils.CustomTestProfile;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.hasItems;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(CustomTestProfile.class)
|
||||
class CustomLibraryResourceManualTest {
|
||||
|
||||
public static final String BOOKSTORE_ENDPOINT = "/custom/library/book";
|
||||
|
||||
@Test
|
||||
void whenGetBooksGivenNoQuery_thenAllBooksShouldBeReturned() {
|
||||
given().contentType(ContentType.JSON)
|
||||
.when().get(BOOKSTORE_ENDPOINT)
|
||||
.then().statusCode(200)
|
||||
.body("size()", is(2))
|
||||
.body("title", hasItems("Foundation", "Dune"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.quarkus;
|
||||
|
||||
import io.quarkus.test.common.http.TestHTTPEndpoint;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
|
||||
@QuarkusTest
|
||||
@TestHTTPEndpoint(LibraryResource.class)
|
||||
class LibraryHttpEndpointIntegrationTest {
|
||||
|
||||
@Test
|
||||
void whenGetBooks_thenShouldReturnSuccessfully() {
|
||||
given().contentType(ContentType.JSON)
|
||||
.when().get("book")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.quarkus;
|
||||
|
||||
import io.quarkus.test.common.http.TestHTTPEndpoint;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static java.nio.charset.Charset.defaultCharset;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTest
|
||||
class LibraryResourceHttpResourceIntegrationTest {
|
||||
|
||||
@TestHTTPEndpoint(LibraryResource.class)
|
||||
@TestHTTPResource("book")
|
||||
URL libraryEndpoint;
|
||||
|
||||
@Test
|
||||
void whenGetBooksByTitle_thenBookShouldBeFound() {
|
||||
given().contentType(ContentType.JSON).param("query", "Dune")
|
||||
.when().get(libraryEndpoint)
|
||||
.then().statusCode(200)
|
||||
.body("size()", is(1))
|
||||
.body("title", hasItem("Dune"))
|
||||
.body("author", hasItem("Frank Herbert"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGetBooks_thenBooksShouldBeFound() throws IOException {
|
||||
assertTrue(IOUtils.toString(libraryEndpoint.openStream(), defaultCharset()).contains("Asimov"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.quarkus;
|
||||
|
||||
import com.baeldung.quarkus.service.LibraryService;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.mockito.InjectSpy;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@QuarkusTest
|
||||
class LibraryResourceInjectSpyIntegrationTest {
|
||||
|
||||
@InjectSpy
|
||||
LibraryService libraryService;
|
||||
|
||||
@Test
|
||||
void whenGetBooksByAuthor_thenBookShouldBeFound() {
|
||||
given().contentType(ContentType.JSON).param("query", "Asimov")
|
||||
.when().get("/library/book")
|
||||
.then().statusCode(200);
|
||||
|
||||
verify(libraryService).find("Asimov");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.quarkus;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
|
||||
@QuarkusTest
|
||||
class LibraryResourceIntegrationTest {
|
||||
|
||||
@Test
|
||||
void whenGetBooksByTitle_thenBookShouldBeFound() {
|
||||
given().contentType(ContentType.JSON).param("query", "Dune")
|
||||
.when().get("/library/book")
|
||||
.then().statusCode(200)
|
||||
.body("size()", is(1))
|
||||
.body("title", hasItem("Dune"))
|
||||
.body("author", hasItem("Frank Herbert"));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,8 +1,11 @@
|
|||
package com.baeldung.quarkus;
|
||||
|
||||
import io.quarkus.test.junit.SubstrateTest;
|
||||
import io.quarkus.test.common.QuarkusTestResource;
|
||||
import io.quarkus.test.h2.H2DatabaseTestResource;
|
||||
import io.quarkus.test.junit.NativeImageTest;
|
||||
|
||||
@SubstrateTest
|
||||
@NativeImageTest
|
||||
@QuarkusTestResource(H2DatabaseTestResource.class)
|
||||
public class NativeHelloResourceIT extends HelloResourceUnitTest {
|
||||
|
||||
// Execute the same tests but in native mode.
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.quarkus;
|
||||
|
||||
import io.quarkus.test.common.QuarkusTestResource;
|
||||
import io.quarkus.test.h2.H2DatabaseTestResource;
|
||||
import io.quarkus.test.junit.NativeImageTest;
|
||||
|
||||
@NativeImageTest
|
||||
@QuarkusTestResource(H2DatabaseTestResource.class)
|
||||
class NativeLibraryResourceIT extends LibraryHttpEndpointIntegrationTest {
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.quarkus.repository;
|
||||
|
||||
import com.baeldung.quarkus.utils.QuarkusTransactionalTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@QuarkusTransactionalTest
|
||||
class BookRepositoryIntegrationTest {
|
||||
|
||||
@Inject
|
||||
BookRepository bookRepository;
|
||||
|
||||
@Test
|
||||
void givenBookInRepository_whenFindByAuthor_thenShouldReturnBookFromRepository() {
|
||||
assertTrue(bookRepository.findBy("Herbert").findAny().isPresent());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.quarkus.service;
|
||||
|
||||
import com.baeldung.quarkus.model.Book;
|
||||
import com.baeldung.quarkus.repository.BookRepository;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.mockito.InjectMock;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@QuarkusTest
|
||||
class LibraryServiceInjectMockUnitTest {
|
||||
|
||||
@Inject
|
||||
LibraryService libraryService;
|
||||
|
||||
@InjectMock
|
||||
BookRepository bookRepository;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
when(bookRepository.findBy("Frank Herbert"))
|
||||
.thenReturn(Arrays.stream(new Book[] {
|
||||
new Book("Dune", "Frank Herbert"),
|
||||
new Book("Children of Dune", "Frank Herbert")}));
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenFindByAuthor_thenBooksShouldBeFound() {
|
||||
assertEquals(2, libraryService.find("Frank Herbert").size());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.quarkus.service;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
@QuarkusTest
|
||||
class LibraryServiceIntegrationTest {
|
||||
|
||||
@Inject
|
||||
LibraryService libraryService;
|
||||
|
||||
@Test
|
||||
void whenFindByAuthor_thenBookShouldBeFound() {
|
||||
assertFalse(libraryService.find("Frank Herbert").isEmpty());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.quarkus.service;
|
||||
|
||||
import com.baeldung.quarkus.model.Book;
|
||||
import com.baeldung.quarkus.repository.BookRepository;
|
||||
import com.baeldung.quarkus.utils.TestBookRepository;
|
||||
import io.quarkus.test.junit.QuarkusMock;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@QuarkusTest
|
||||
class LibraryServiceQuarkusMockUnitTest {
|
||||
|
||||
@Inject
|
||||
LibraryService libraryService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
BookRepository mock = Mockito.mock(TestBookRepository.class);
|
||||
Mockito.when(mock.findBy("Asimov"))
|
||||
.thenReturn(Arrays.stream(new Book[] {
|
||||
new Book("Foundation", "Isaac Asimov"),
|
||||
new Book("I Robot", "Isaac Asimov")}));
|
||||
QuarkusMock.installMockForType(mock, BookRepository.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenFindByAuthor_thenBooksShouldBeFound() {
|
||||
assertEquals(2, libraryService.find("Asimov").size());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.quarkus.utils;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class CustomTestProfile implements QuarkusTestProfile {
|
||||
|
||||
@Override
|
||||
public Map<String, String> getConfigOverrides() {
|
||||
return Collections.singletonMap("quarkus.resteasy.path", "/custom");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Class<?>> getEnabledAlternatives() {
|
||||
return Collections.singleton(TestBookRepository.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getConfigProfile() {
|
||||
return "custom-profile";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.quarkus.utils;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
|
||||
import javax.enterprise.inject.Stereotype;
|
||||
import javax.transaction.Transactional;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@QuarkusTest
|
||||
@Stereotype
|
||||
@Transactional
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface QuarkusTransactionalTest {
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.quarkus.utils;
|
||||
|
||||
import com.baeldung.quarkus.model.Book;
|
||||
import com.baeldung.quarkus.repository.BookRepository;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Priority;
|
||||
import javax.enterprise.context.ApplicationScoped;
|
||||
import javax.enterprise.inject.Alternative;
|
||||
|
||||
@Priority(1)
|
||||
@Alternative
|
||||
@ApplicationScoped
|
||||
public class TestBookRepository extends BookRepository {
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
persist(new Book("Dune", "Frank Herbert"),
|
||||
new Book("Foundation", "Isaac Asimov"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue