Skip to content

Commit 3b5d855

Browse files
committed
Esempio05testJunit5Mokito eseguito con successo
1 parent fab7960 commit 3b5d855

File tree

6 files changed

+32
-8
lines changed

6 files changed

+32
-8
lines changed

Esempio05testJunit5Mokito/Dockerfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ COPY pom.xml .
55
COPY src ./src
66
RUN mvn clean package -DskipTests
77

8-
# Usa JDK come immagine base #ex #FROM eclipse-temurin:17-jre-alpine
9-
FROM openjdk:17-jdk-slim
8+
# Usa JDK come immagine base #ex #
9+
#FROM eclipse-temurin:17-jre-alpine
10+
#FROM eclipse-temurin:17-jdk-jammy
11+
FROM openjdk:21-jdk-slim
1012

1113
# Crea directory per l'app
1214
WORKDIR /app
1315

14-
# Installa il client mysql e curl #ex RUN apk add --no-cache url
16+
# Installa curl e il client MySQL in un'unica riga per efficienza e consistenza
17+
#RUN apk update && apk add curl mysql-client && rm -rf /var/cache/apk/*
1518
RUN apt-get update && \
1619
apt-get install -y default-mysql-client curl iputils-ping && \
1720
rm -rf /var/lib/apt/lists/*

Esempio05testJunit5Mokito/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ Presente anche un `docker-compose` dedicato per eseguire **SonarQube** per la ve
1818
docker compose up --build
1919
```
2020
Nota: il microservizio ci mette un po' a partire perchè deve aspettare che il server MySql sia partito. Per evitare il problema che il microservizio si rompa in avvio è stato fatto uno script `start_microservice.sh` che aspetta che il MySql sia attivo prima di partire.
21+
- Comandi per il monitoraggio del database MySql
22+
```
23+
docker logs es05-mysql-db
24+
docker exec -it es05-mysql-db mysql -u root -pstupendo
25+
> SHOW DATABASES;
26+
> USE Applicazione;
27+
> SHOW TABLES;
28+
```
29+
- Comandi curl per provare il funzionamento
30+
```
31+
curl -X POST "http://localhost:8045/api/login?nome=alnao&password=bellissimo"
32+
curl -X POST "http://localhost:8045/api/users" -H "Content-Type: application/json" -d '{"nome":"NuovoUtente","password":"password123" }'
33+
curl -X GET "http://localhost:8045/api/users"
34+
curl -X GET "http://localhost:8045/api/users/1"
35+
```
2136
- Comando per la distruzione di tutta l'architettura
2237
```
2338
docker-compose down --volumes --rmi all

Esempio05testJunit5Mokito/docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ services:
1515
- "3307:3306"
1616
volumes:
1717
- es05-mysql-data:/var/lib/mysql
18-
- ./init:/docker-entrypoint-initdb.d # esegue script SQL all'avvio
18+
# - ./init/:/docker-entrypoint-initdb.d # esegue script SQL all'avvio
19+
- ./init/users.sql:/docker-entrypoint-initdb.d/users.sql
1920
networks:
2021
- es05-app-network
2122

Esempio05testJunit5Mokito/init/start_microservice.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ echo "> Avvio del microservizio Spring Boot con MySQL... ${MYSQL_DATASOURCE_HOST
66
# Attesa che MySQL sia pronto
77
echo ">Attesa che MySQL sia raggiungibile..."
88
counter=0
9-
until mysql -h"${MYSQL_DATASOURCE_HOST}" -P"${MYSQL_DATASOURCE_PORT}" -u"${MYSQL_DATASOURCE_USERNAME}" -p"${MYSQL_DATASOURCE_PASSWORD}" -e 'SELECT 1'; do
9+
until mysql -h"${MYSQL_DATASOURCE_HOST}" -p"${MYSQL_DATASOURCE_PORT}" -u"${MYSQL_DATASOURCE_USERNAME}" -p"${MYSQL_DATASOURCE_PASSWORD}" --ssl-verify-server-cert=0 -e 'SELECT 1'; do
1010
counter=$((counter + 1))
1111
if [ $counter -ge 10 ]; then
1212
echo "> MySQL non raggiungibile dopo 10 tentativi. Proseguo comunque..."

Esempio05testJunit5Mokito/src/main/resources/application-test.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ spring.datasource.username=sa
44
spring.datasource.password=
55
spring.jpa.hibernate.ddl-auto=create-drop
66
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
7-
spring.jpa.show-sql=true
7+
spring.jpa.show-sql=true
8+
management.metrics.system.processor.enabled=false
9+
spring.autoconfigure.exclude=org.springframework.boot.actuate.autoconfigure.metrics.SystemMetricsAutoConfiguration
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
server.port=8045
22

3-
spring.datasource.url=jdbc:mysql://${MYSQL_DATASOURCE_HOST:localhost}:${MYSQL_DATASOURCE_PORT:3306}/${MYSQL_DATASOURCE_DATABASE:xxx}?useSSL=false&serverTimezone=UTC
3+
spring.datasource.url=jdbc:mysql://${MYSQL_DATASOURCE_HOST:localhost}:${MYSQL_DATASOURCE_PORT:3306}/${MYSQL_DATASOURCE_DATABASE:xxx}?serverTimezone=UTC&useSSL=true&trustServerCertificate=true
44
spring.datasource.username=${MYSQL_DATASOURCE_USERNAME:appuser}
55
spring.datasource.password=${MYSQL_DATASOURCE_PASSWORD:apppass}
66
spring.jpa.hibernate.ddl-auto=${SPRING_JPA_HIBERNATE_DDL_AUTO:update}
77
spring.jpa.show-sql=true
88
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
99

10-
springdoc.api-docs.path=/api-docs
10+
springdoc.api-docs.path=/api-docs
11+
12+
management.metrics.system.processor.enabled=false
13+
spring.autoconfigure.exclude=org.springframework.boot.actuate.autoconfigure.metrics.SystemMetricsAutoConfiguration

0 commit comments

Comments
 (0)