Skip to content

Commit

Permalink
test: Fix MovieServiceTest + MovieControllerTest
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilKes committed Feb 13, 2024
1 parent adf10c8 commit 63b7885
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 65 deletions.
5 changes: 5 additions & 0 deletions movieland-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@
<artifactId>janino</artifactId>
<version>3.1.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>


</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.phil.movieland.rest;

import com.phil.movieland.MovielandSpringApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;

@SpringBootApplication(exclude = {ThymeleafAutoConfiguration.class})
public class MovielandTestApplication {

public static void main(String[] args) {
SpringApplication.run(MovielandSpringApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -1,66 +1,113 @@
package com.phil.movieland.rest.controller;

import com.phil.movieland.MovielandSpringApplication;
import static org.hamcrest.Matchers.hasSize;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

import com.phil.movieland.data.entity.Movie;
import com.phil.movieland.rest.MovielandTestApplication;
import com.phil.movieland.rest.service.MovieService;
import org.junit.Before;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageImpl;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

@RunWith(SpringRunner.class)
@SpringBootTest( classes = MovielandSpringApplication.class)
@AutoConfigureMockMvc
@ActiveProfiles("test")
@WebMvcTest(controllers = {MovieController.class}, excludeAutoConfiguration = {
SecurityAutoConfiguration.class})
@ContextConfiguration(classes = {MovielandTestApplication.class})
public class MovieControllerTest {

@Autowired
private MockMvc mvc;

@MockBean
private MovieService movieService;
private MovieController movieController;

private List<Movie> testMovies= Arrays.asList(createMovie("Movie 1"),
createMovie("Movie 2"),
createMovie("Movie 3"),
createMovie("Movie 4"));
private List<Movie> testMovies = Arrays.asList(createMovie("Movie 1"),
createMovie("Movie 2"),
createMovie("Movie 3"),
createMovie("Movie 4"));

private static Movie createMovie(String name){
Movie movie= new Movie();
private static long movieIdCounter = 1;

private static Movie createMovie(String name) {
Movie movie = new Movie();
movie.setDate(new Date());
movie.setMovId(movieIdCounter++);
movie.setName(name);
movie.setLength(100L);
movie.setDescription("A Movie");
return movie;
}


@Before
public void setUp() throws Exception {
movieService= mock(MovieService.class);
movieController= new MovieController(movieService);
}

@Test
public void testGetMovies200Ok(){
public void testGetAllMovies200Ok() throws Exception {
when(movieService.getAllMovies()).thenReturn(testMovies);
Collection<Movie> returnedMovies= movieController.getMovies(null);
mvc.perform(MockMvcRequestBuilders.get("/api/movies"))
.andDo(print())
.andExpect(jsonPath("$", hasSize(4)))
.andExpect(jsonPath("$[0].name").value("Movie 1"));
verify(movieService, times(1)).getAllMovies();
verifyNoMoreInteractions(movieService);
assertEquals(testMovies,returnedMovies);
}

@Test
public void testGetSearchMovies200Ok() throws Exception {
when(movieService.queryAllMovies("Movie 1"))
.thenReturn(testMovies.subList(0, 1));
mvc.perform(MockMvcRequestBuilders.get("/api/movies")
.param("name", "Movie 1"))
.andDo(print())
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].name").value("Movie 1"));
verify(movieService, times(1)).queryAllMovies("Movie 1");
verifyNoMoreInteractions(movieService);
}

@Test
public void testGetMoviesPage200Ok() throws Exception {
when(movieService.getAllMoviesPaged(1, 10)).thenReturn(new PageImpl<>(testMovies));
mvc.perform(MockMvcRequestBuilders.get("/api/movies/page/1"))
.andDo(print())
.andExpect(jsonPath("$", hasSize(4)))
.andExpect(header().string("hasMore", "false"))
.andExpect(jsonPath("$[0].name").value("Movie 1"));
verify(movieService, times(1)).getAllMoviesPaged(1, 10);
verifyNoMoreInteractions(movieService);
}

@Test
public void testGetMoviesByIds200Ok() throws Exception {
List<Long> ids = List.of(1L, 2L);
when(movieService.queryMoviesByIds(ids))
.thenReturn(testMovies.subList(0, 2));
mvc.perform(MockMvcRequestBuilders.get("/api/movies/ids")
.param("ids", "1,2"))
.andDo(print())
.andExpect(jsonPath("$[\"1\"].name").value("Movie 1"))
.andExpect(jsonPath("$[\"2\"].name").value("Movie 2"));
verify(movieService, times(1)).queryMoviesByIds(ids);
verifyNoMoreInteractions(movieService);
}


//...

}
Original file line number Diff line number Diff line change
@@ -1,42 +1,35 @@
package com.phil.movieland.rest.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import com.phil.movieland.MovielandSpringApplication;
import com.phil.movieland.data.entity.Movie;
import com.phil.movieland.data.repository.MovieRepository;
import com.phil.movieland.rest.controller.MovieController;
import com.phil.movieland.rest.service.MovieService;
import com.phil.movieland.utils.DateUtils;
import com.phil.movieland.utils.TmdbApiService;
import info.movito.themoviedbapi.model.MovieDb;
import org.junit.Before;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import java.util.*;
import java.util.stream.Collectors;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MovielandSpringApplication.class)
@AutoConfigureMockMvc
@ActiveProfiles("test")
@RunWith(MockitoJUnitRunner.class)
public class MovieServiceTest {

@Autowired
private MockMvc mvc;

@InjectMocks
private MovieService movieService;
@Mock
private MovieRepository movieRepository;
@Mock
private TmdbApiService tmdbApiService;
private MovieService movieService;

private List<Movie> testMovies = Arrays.asList(createMovie("Movie 1"),
createMovie("Movie 2"),
Expand All @@ -49,26 +42,30 @@ private static Movie createMovie(String name) {
movie.setName(name);
movie.setLength(100L);
movie.setDescription("A Movie");
movie.setTmdbId(1L);
return movie;
}

@Before
public void setUp() throws Exception {
movieRepository = mock(MovieRepository.class);
tmdbApiService = mock(TmdbApiService.class);
movieService = new MovieService(movieRepository, tmdbApiService);
private static MovieDb createMovieDb(String name) {
MovieDb movieDb = new MovieDb();
movieDb.setTitle(name);
movieDb.setOverview("Description for " + name);
return movieDb;

}

@Test
public void testGetAllMovies() {
when(movieRepository.findAllByOrderByName()).thenReturn(testMovies);
when(movieRepository.save(any())).thenReturn(any());

testMovies.forEach(testMovie -> when(tmdbApiService.getMovieFromTmdb(testMovie)).thenReturn(new MovieDb()));
testMovies.forEach(testMovie -> when(tmdbApiService.getMovieFromTmdb(testMovie)).thenReturn(
createMovieDb(testMovie.getName())));
Collection<Movie> returnedMovies = movieService.getAllMovies();
verify(movieRepository, times(1)).findAllByOrderByName();
verify(movieRepository, times(4)).save(any());
verifyNoMoreInteractions(movieRepository);
assertEquals(testMovies, returnedMovies);
}

//...
}

0 comments on commit 63b7885

Please sign in to comment.