comparison spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithInMemoryDatabase.java @ 8:7ce4367b50bc

Ein echter Unit Test, der eine in-memory H2 als Teil vom Test startet, das Schema mit liquibase erzeugt und nach dem Test wieder stoppt. Das alles ohne die Web Controller zu starten.
author Dirk Olmes <dirk.olmes@codedo.de>
date Thu, 13 Aug 2020 16:51:05 +0200
parents
children f3f9102ef7c0
comparison
equal deleted inserted replaced
7:0c3494137a82 8:7ce4367b50bc
1 package de.comline.spring.service;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.is;
5 import static org.junit.jupiter.api.Assertions.assertTrue;
6
7 import org.junit.jupiter.api.DisplayName;
8 import org.junit.jupiter.api.Test;
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.boot.autoconfigure.domain.EntityScan;
11 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
12 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
13 import org.springframework.boot.test.context.SpringBootTest;
14 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
15
16 import de.comline.spring.entity.Movie;
17
18 @SpringBootTest(classes = {
19 DataSourceAutoConfiguration.class,
20 HibernateJpaAutoConfiguration.class,
21 MovieService.class,
22 })
23 @EnableJpaRepositories("de.comline.spring.repository")
24 @EntityScan("de.comline.spring.entity")
25 public class MovieServiceTestWithInMemoryDatabase {
26 @Autowired
27 private MovieService service;
28
29 @Test
30 @DisplayName("Test with a real movie repository")
31 void withRealRepository() {
32 String title = "the-movie-title";
33
34 Movie movie = service.createMovie(title);
35 assertThat(movie.getTitle(), is(title));
36 assertTrue(movie.getId() > 0);
37 }
38 }