Giter Site home page Giter Site logo

micronaut-testing-best-practices's Introduction

Micronaut Testing best practices

This is a Micronaut application with examples of everything I explain in my Micronaut Testing best practices webinar.

The commits are self-contained and introduce a new feature at a time. It is suggested to review them independently instead of just checking the master branch directly.

Configuration

You need to install JDK 11 and Docker.

The application uses the OMDB API to show how to Mock 3rd party APIs. To use the real API you need to create an apikey at http://www.omdbapi.com and set it in application.yml.

Run the tests and application

Run the test

To run all the test:

$ ./gradlew test

Run the application

It is necessary to have a Postgres database for running the application. You can run it using Docker:

docker run -it --rm -p 5432:5432 -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=micronaut postgres:11.5-alpine

Start the application:

$ ./gradlew run

Endpoints

To create a new author:

curl -X POST -H 'Content-Type:application/json' -d '{"name":"Stephen King"}' localhost:8080/authors

To find and author by name:

curl -v "localhost:8080/authors/by-name?author=Stephen%20King&username=admin"

NOTE: The application will create automatically an author with two books if the database is empty. Take a look at BootstrapService.

OpenAPI and Swagger-UI

The application automatically generates an OpenAPI yml file that is exposed at http://localhost:8080/swagger/demo-0.1.yml.

It also includes the Swagger-UI so you can take a look at the API documentation and execute the different methods directly from the browser: http://localhost:8080/swagger-ui/index.html.

swaggerui

GraalVM

It is possible to convert the application to a GraalVM native-image.

If you don't have GraalVM installed, run:

$ sdk install java 20.1.0.r11-grl
$ gu install native-image

Then, to convert the application to a native-image:

$ sdk use java 20.1.0.r11-grl
$ ./gradlew assemble
$ native-image --no-server --class-path build/libs/micronaut-test-best-practices-0.1-all.jar

And run it:

$ ./demo
22:47:26.461 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
22:47:26.467 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
22:47:26.485 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 33ms. Server Running: http://localhost:8080

micronaut-testing-best-practices's People

Contributors

ilopmar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.