view spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithInMemoryDatabase.java @ 11:f3f9102ef7c0

Noch ein Test mit der in-memory Datenbank: diesmal wird ein Testdatensatz vorher rein geladen, der dann im Test geholt wird.
author Dirk Olmes <dirk.olmes@codedo.de>
date Thu, 13 Aug 2020 17:33:20 +0200
parents 7ce4367b50bc
children
line wrap: on
line source

package de.comline.spring.service;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.jdbc.Sql;

import de.comline.spring.entity.Movie;

@SpringBootTest(classes = {
	DataSourceAutoConfiguration.class,
	HibernateJpaAutoConfiguration.class,
	MovieService.class,
})
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
@EnableJpaRepositories("de.comline.spring.repository")
@EntityScan("de.comline.spring.entity")
public class MovieServiceTestWithInMemoryDatabase {
	@Autowired
	private MovieService service;
	
	@Test
	@DisplayName("Test with a real movie repository")
	void withRealRepository() {
		String title = "the-movie-title";
		
		Movie movie = service.createMovie(title);
		assertThat(movie.getTitle(), is(title));
		assertTrue(movie.getId() > 0);
	}

	@Test
	@DisplayName("Fetching from a pre-populated database")
	@Sql("/sql/movie.sql")
	public void fetchFromDatabase() {
		assertThat(service.listAllMovies(), is("Shrek,"));
	}
}