Database Test with Spring Boot

Asked

Viewed 693 times

0

I’m having an error, when running my application with profile test, Spring Boot does not create the same database in the local profile.
Any suggestions?

Follow my application-local file.properties:

# Hibernate
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

spring.datasource.username=rw_itau
spring.datasource.password=rw_itau
spring.datasource.url=jdbc\:postgresql\://localhost/rw_itau
spring.datasource.driver-class-name=org.postgresql.Driver

spring.jpa.hibernate.ddl-auto=create-drop

And the application-test.properties file:

# Hibernate
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

spring.datasource.username=rw_itau
spring.datasource.password=rw_itau
spring.datasource.url=jdbc\:postgresql\://localhost/rw_itau_teste
spring.datasource.driver-class-name=org.postgresql.Driver

spring.jpa.hibernate.ddl-auto=create-drop

Plus I have several classes as the starting class of Springboot:

package br.com.zup.rw.itau;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

@SpringBootApplication
@Controller
public class Boot
{

   public static void main(String[] args)
   {
      SpringApplication.run(Boot.class, args);
   }

}

I am using Maven and Docker with Banco Postgres the error generated is this:

org.postgresql.util.Psqlexception: FATAL: database "rw_itau_teste" does not exist

I run the project with this argument:

-Dspring.profiles.active=test

But when running with the local argument, the bank is normally created.

-Dspring.profiles.active=local

When I run the second error profile when connecting to the database.

  • You could update your question with an example test (with an setup annotations and whatever else is needed), its folder structure and the relevant parts of the Maven / Gradle / build system?

  • See if it’s better? I’m using Maven, but I don’t believe there’s anything very important to post besides springboot

  • 1

    Renato, I would post the test settings with @TestPropertySource, @ActiveProfiles, etc and a test. It may be missing some annotation, some file may be in the wrong folder, etc.

  • Opa my properties files are all in the Resources src/main/Resources folder. I am using the Annotation @Activeprofiles("test") in my test class, only for the project to understand which profile to use. In addition to this I have these settings in my test: Runwith(Springrunner.class) Springboottest Autoconfiguremockmvc Autoconfigurerestdocs(outputDir = "target/snippets")

  • Try to move the file application-test.properties for src/test/resources. If it doesn’t work please update the question with a MVCE

  • @Anthonyaccioly by what I understood so far the system is opening the application-test.properties, correctly it just fails to create the bank, the strange thing is that in application-local.properties the bank was created normally.

  • I thought the problem was really stupid in vdd, I’m creating a single database on Docker, I just need to learn how to create Multiplus.

Show 2 more comments

1 answer

1


Good Afternoon

  spring.jpa.hibernate.ddl-auto=create-drop

This property does not create the database(database), only the database(database) tables. Create manually in Postgres, and you will see what will run and create the tables.

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html

Workaround: You can create in the main method a JDBC connection by running Preparedstatemente.executeQuery("create database rw_itau_teste").

Use flywayDb as in the link above, using Migration;

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.