diff --git a/movieland-backend/pom.xml b/movieland-backend/pom.xml
index 3b1ad25..566caaf 100644
--- a/movieland-backend/pom.xml
+++ b/movieland-backend/pom.xml
@@ -175,6 +175,11 @@
janino
3.1.9
+
+ junit
+ junit
+ test
+
diff --git a/movieland-backend/src/test/java/com/phil/movieland/rest/MovielandTestApplication.java b/movieland-backend/src/test/java/com/phil/movieland/rest/MovielandTestApplication.java
new file mode 100644
index 0000000..4fc18f4
--- /dev/null
+++ b/movieland-backend/src/test/java/com/phil/movieland/rest/MovielandTestApplication.java
@@ -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);
+ }
+
+}
diff --git a/movieland-backend/src/test/java/com/phil/movieland/rest/controller/MovieControllerTest.java b/movieland-backend/src/test/java/com/phil/movieland/rest/controller/MovieControllerTest.java
index 4ff6501..16999e5 100644
--- a/movieland-backend/src/test/java/com/phil/movieland/rest/controller/MovieControllerTest.java
+++ b/movieland-backend/src/test/java/com/phil/movieland/rest/controller/MovieControllerTest.java
@@ -1,47 +1,55 @@
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 testMovies= Arrays.asList(createMovie("Movie 1"),
- createMovie("Movie 2"),
- createMovie("Movie 3"),
- createMovie("Movie 4"));
+ private List 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");
@@ -49,18 +57,57 @@ private static Movie createMovie(String name){
}
- @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 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 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);
+ }
+
+
+ //...
+
}
\ No newline at end of file
diff --git a/movieland-backend/src/test/java/com/phil/movieland/rest/service/MovieServiceTest.java b/movieland-backend/src/test/java/com/phil/movieland/rest/service/MovieServiceTest.java
index 8c3e030..cdd2321 100644
--- a/movieland-backend/src/test/java/com/phil/movieland/rest/service/MovieServiceTest.java
+++ b/movieland-backend/src/test/java/com/phil/movieland/rest/service/MovieServiceTest.java
@@ -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 testMovies = Arrays.asList(createMovie("Movie 1"),
createMovie("Movie 2"),
@@ -49,15 +42,15 @@ 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
@@ -65,10 +58,14 @@ 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 returnedMovies = movieService.getAllMovies();
verify(movieRepository, times(1)).findAllByOrderByName();
+ verify(movieRepository, times(4)).save(any());
verifyNoMoreInteractions(movieRepository);
assertEquals(testMovies, returnedMovies);
}
+
+ //...
}
\ No newline at end of file