title: Spring Boot Testing with embedded Postgressql database
body: <p>I want to test my controller in Spring-Boot 2.1.3.
  I want to simulate a POST request. 
  This works quite well but I can't save the data to my Postgres database because H2 can't do JSONB. 
  Therefore I use <a href="https://github.com/opentable/otj-pg-embedded" rel="nofollow noreferrer">this</a> library to embed Postgres.</p>
  
  <p>That works quiet well, the database starts but I can't/know how I can bring my mock to save the data in the embedded database. </p>
  
  <p>Do you have any ideas ? </p>
  
  <p>My Test:</p>
  
  <pre><code>@RunWith( SpringRunner.class )
  @AutoConfigureMockMvc
  @SpringBootTest
  public class ControllerTest
  {
  
  
  @InjectMocks
  private Student student;
  
  @InjectMocks
  private StudentInfo studentinfo;
  
  @Autowired
  private MockMvc mockMvc;
  
  
  @ClassRule
  public static PreparedDbRule preparedDbRule = EmbeddedPostgresRules.preparedDatabase( FlywayPreparer.forClasspathLocation( "db/migration" ) );
  
  @Test
  public void test() throws Exception
  {
  
      try(EmbeddedPostgres postgres = EmbeddedPostgres.builder.setPort(62239).start();
      Connection connection = postgres.getPostgresDatabase().getConnection()){
  
      String jsonString = "{"Student" : "name", "course" : "course1"}";
  
          this.mockMvc.perform( post( "/students" )
                  .contentType( MediaType.APPLICATION_JSON )
                  .content( jsonString) )
                  .andDo( print() )
                  .andExpect( status().is( 201 )
                  );
      }catch( Exception  e){
          e.printStackTrace();
      }
    }
  }
  </code></pre>
  
  <p>My application.properties:</p>
  
  <pre><code>spring.datasource.driver-class-name=org.postgresql.Driver
  spring.datasource.url=jdbc:postgresql://localhost:62239/postgres
  spring.datasource.username=postgres
  spring.datasource.password=postgres
  flyway.enabled=false
  </code></pre>
  
  <p>This is not a duplicate question from <a href="https://stackoverflow.com/questions/48956743/embedded-postgres-for-spring-boot-tests">this</a>, because in this question they used a <a href="https://github.com/zonkyio/embedded-database-spring-test" rel="nofollow noreferrer">libary</a> to solve the problem which is only available/compatible in Spring-Boot 1.4.0  </p>
  
  <blockquote>
    <p>upports both Spring and Spring Boot frameworks
    Supported versions are Spring 4.3.0+ and Spring Boot 1.4.0+</p>
  </blockquote>
  
  <p><strong>Edit</strong>
  After some testing i found out that i should use an "application.properties" file for my Test. I also set the Port for the Database. 
  But now my I get an random port although i set it with setPort(). I get an random port every start. </p>