Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running DataJpaTest with PostgreSQL

I wrote a jpa test with Spring Boot as explained here: https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4

@RunWith(SpringRunner.class)
@DataJpaTest
public class UserRepositoryTests {

@Autowired
private TestEntityManager entityManager;

...
}

This configures a in-memory db by default. How can I configure this test, that it uses my local PostgreSQL db?

like image 847
René Winkler Avatar asked Apr 25 '16 19:04

René Winkler


People also ask

What is the use of @DataJpaTest?

@DataJpaTest is used to test JPA repositories. It is used in combination with @RunWith(SpringRunner. class) . The annotation disables full auto-configuration and applies only configuration relevant to JPA tests.

How do you write junit test cases for JPA repository?

You can create a @DataJpaTest and @Autowire your repository into it. For example: @RunWith(SpringRunner. class) @DataJpaTest public class MyJpaTest { @Autowired private ChartRepository chartRepository; @Test public void myTest() { ... } }


2 Answers

If you want to use the same datasource as your regular application you can use:

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace=Replace.NONE)
like image 179
Phil Webb Avatar answered Oct 16 '22 20:10

Phil Webb


@DataJpaTest contains @RunWith(SpringRunner.class)

@DataJpaTest
@AutoConfigureTestDatabase(replace=Replace.NONE)

It's enough. Good luck!

like image 11
Dmitry Petelko Avatar answered Oct 16 '22 20:10

Dmitry Petelko