-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Fix MovieServiceTest + MovieControllerTest
- Loading branch information
Showing
4 changed files
with
129 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
movieland-backend/src/test/java/com/phil/movieland/rest/MovielandTestApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
113 changes: 80 additions & 33 deletions
113
movieland-backend/src/test/java/com/phil/movieland/rest/controller/MovieControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
//... | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters