view spring-boot-playground/src/test/java/de/comline/spring/service/MovieServiceTestWithMockRepo.java @ 20:452cfefb0e1e default tip

Einfache Spring Application, die ihre config aus dem Spring Cloud Config Server liest
author Dirk Olmes <dirk.olmes@codedo.de>
date Thu, 17 Sep 2020 17:25:15 +0200
parents 4594ff529ab1
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.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.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;

import de.comline.spring.entity.Movie;
import de.comline.spring.repository.MovieRepository;
import de.comline.spring.service.MovieServiceTestWithMockRepo.TestConfig;

@SpringBootTest(classes = {
	MovieService.class,
	TestConfig.class
})
@DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
public class MovieServiceTestWithMockRepo {
	@Autowired
	private MovieService service;
	
	@Test
	@DisplayName("Test with a mock movie repository")
	void withMockRepository() {
		String title = "the-movie-title";
		
		Movie movie = service.createMovie(title);
		assertThat(movie.getTitle(), is(title));
	}
	
	@Configuration
	static class TestConfig {
		@Bean
		MovieRepository createMockRepository() {
			return RepositoryMocker.createMockRepository();
		}
	}
}