application.properties:
server.port=8081
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/familybudgetapp
spring.datasource.username=root
spring.datasource.password=12345
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
``
test\ application.properties :
server.port=8081
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/familybudgetapp
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
``
./env :
MYSQLDB_USER=root
MYSQLDB_ROOT_PASSWORD=12345
MYSQLDB_DATABASE=familybudgetapp
MYSQLDB_LOCAL_PORT=3307
MYSQLDB_DOCKER_PORT=3306
SPRING_LOCAL_PORT=8081
SPRING_DOCKER_PORT=8081
``
dockerfile:
FROM openjdk:17-jdk-slim
EXPOSE 8081
ARG JAR_FILE=target/family-budget-app-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} family-budget-app.jar
ENTRYPOINT ["java","-jar","family-budget-app.jar"]
``
docker-compose.yml:
version: "3.8"
services:
mysqldb:
image: mysql
restart: unless-stopped
env_file: ./.env
environment:
- MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
- MYSQL_DATABASE=$MYSQLDB_DATABASE
ports:
- ${MYSQLDB_LOCAL_PORT}:${MYSQLDB_DOCKER_PORT}
volumes:
- db:/var/lib/mysql
web_app:
depends_on:
- mysqldb
build:
context: .
dockerfile: Dockerfile
restart: on-failure
env_file: ./.env
environment:
SPRING_APPLICATION_JSON: '{
"spring.datasource.url" : "jdbc:mysql://mysqldb:$MYSQLDB_DOCKER_PORT/$MYSQLDB_DATABASE?allowPublicKeyRetrieval=true&useSSL=false",
"spring.datasource.username" : "$MYSQLDB_USER",
"spring.datasource.password" : "$MYSQLDB_ROOT_PASSWORD",
"spring.jpa.properties.hibernate.dialect" : "org.hibernate.dialect.MySQLDialect",
"spring.jpa.hibernate.ddl-auto" : "update"
}'
volumes:
- .m2:/root/.m2
stdin_open: true
tty: true
volumes:
db:
``
repository test file: @DataJpaTest @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) class SpendingRepositoryTest {
@Autowired
SpendingRepository spendingRepository;
@Test
@Sql({"/data.sql"})
void findMostSpendingDetailsByDate_GivenDate_ReturnSpendings() throws ParseException {
Date startDate = new SimpleDateFormat("yyyy-MM-dd").parse("2001-06-01");
Date endDate = new SimpleDateFormat("yyyy-MM-dd").parse("2001-06-30");
List<Spending> spendings = spendingRepository.findMostSpendingDetailsByDate(startDate, endDate);
assertEquals(5, spendings.size());
assertEquals("tahafurkanunsal", spendings.get(0).getUser().getUsername());
}
}
I have a spring boot mysql project and I want it to work with docker, but since the repository test file in my project tests a real database and is intended to run without the need for mysql and maven when using docker, my test class throws a database connection error while running docker, so I want to set my test class to my in-memory database. I want to set. I want to make it work by connecting it with Docker, but I searched and couldn't get an idea.
I tried to configure h2 database but failed I guess I need some help