Implement Your First API Endpoint
- Add the Mockito dependency for Quarkus to the Maven
pom.xml
xml<dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-junit5-mockito</artifactId> <scope>test</scope> </dependency>
- Start with writing a test under the
com.redhat.runtimes.api
packagejavapackage com.redhat.runtimes.api; import com.redhat.runtimes.data.TodosRepository; import com.redhat.runtimes.models.Todo; import io.quarkus.test.junit.QuarkusMock; import io.quarkus.test.junit.QuarkusTest; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.Mockito; import javax.inject.Inject; import java.util.ArrayList; import java.util.Arrays; import java.util.UUID; import static org.junit.jupiter.api.Assertions.*; @DisplayName("Todos API Tests") @QuarkusTest public class TodosApiTest { @Inject TodosApi underTest; static TodosRepository repo; @BeforeAll public static void setup() { repo = Mockito.mock(TodosRepository.class); QuarkusMock.installMockForType(repo, TodosRepository.class); } @Test public void testGetTodosNoResults() { // Given Mockito.when(repo.listAll()).thenReturn(new ArrayList<>()); // When var result = underTest.gettodos(); // Then assertEquals(0, result.size(), "Without prepared data, we expect 0 results"); } @Test public void testGetTodosOneResult() { // Given Todo todo = new Todo().author("dphillips").complete(false).title("My test todo") .description("A much longer description of my test Todo item").id(UUID.randomUUID()); Mockito.when(repo.listAll()).thenReturn(Arrays.asList(todo)); // When var result = underTest.gettodos(); // Then assertEquals(1, result.size(), "With prepared data, we expect 1 results"); assertEquals("dphillips", result.get(0).getAuthor(), "The author name should be 'dphillips'"); assertFalse(result.get(0).getComplete(), "The complete flag should be false"); assertEquals("My test todo", result.get(0).getTitle(), "The title should be 'My test todo'"); assertEquals("A much longer description of my test Todo item", result.get(0).getDescription(), "The description should be 'A much longer description of my test Todo item'"); } }
- Implement the
TodosApi
interface which was generated by OpenAPI Generatorjavapackage com.redhat.runtimes.api; import com.redhat.runtimes.data.TodosRepository; import com.redhat.runtimes.models.Todo; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import java.util.List; import java.util.UUID; @RequestScoped public class TodosApiImpl implements TodosApi { @Inject TodosRepository repository; @Override public Todo createTodo(Todo todo) { return null; } @Override public void deleteTodo(UUID todoId) { } @Override public Todo getTodo(UUID todoId) { return null; } @Override public List<Todo> gettodos() { return repository.listAll(); } @Override public Todo updateTodo(UUID todoId, Todo todo) { return null; } }
- Run our tests to ensure that our implementation is correctbash
./mvnw test
- Repeat this process for each API Interface and method