我的存储库中有一个@Query,可根据SQL查询将数据返回到控制器,该如何模拟呢?
这是我的资料库
package movieweb.movies.repository;
import movieweb.movies.models.UserMovies;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMoviesRepository extends CrudRepository<UserMovies, Integer> {
@Query(value = "select * from movies, user_movies where movies.movie_id = user_movies.movie_id and uname = ?1", nativeQuery = true)
List<UserMovies> findByUname(String uname);
}
这是我的控制器
package movieweb.movies.controllers;
import movieweb.movies.models.Movies;
import movieweb.movies.models.UserMovies;
import movieweb.movies.repository.UserMoviesRepository;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Query;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class UserMoviesController {
@Autowired
private UserMoviesRepository umRepository;
@CrossOrigin
@PostMapping(path = "/newUserMovie")
public ResponseEntity<UserMovies> addNewUserMovie(@RequestBody UserMovies data){
umRepository.save(data);
return new ResponseEntity<UserMovies>(data, HttpStatus.CREATED);
}
@CrossOrigin
@GetMapping(path="/getUserMovies")
public List<UserMovies> getUsersMovies(){
return (List<UserMovies>) umRepository.findAll();
}
@CrossOrigin
@GetMapping(path = "/getUserMovies/{uname}")
public List<UserMovies> getUserMovies(@PathVariable String uname){
return umRepository.findByUname(uname);
}
@CrossOrigin
@DeleteMapping(path ="/deleteUserMovies/{id}")
ResponseEntity deleteUserMovie(@PathVariable Integer id) {
umRepository.deleteById(id);
return new ResponseEntity(HttpStatus.ACCEPTED);
}
@CrossOrigin
@PutMapping("/usermovie/update/{id}")
public ResponseEntity<UserMovies> updateMovie(@RequestBody UserMovies updateMovie, @PathVariable Integer id) {
return umRepository.findById(id)
.map(userMovies -> {
userMovies.setMovieId(updateMovie.getMovieId());
userMovies.setUname(updateMovie.getUname());
umRepository.save(userMovies);
return new ResponseEntity<UserMovies>(userMovies, HttpStatus.OK);
})
.orElseGet(() -> {
updateMovie.setMovieId(id);
umRepository.save(updateMovie);
return new ResponseEntity<UserMovies>(updateMovie, HttpStatus.OK);
});
}
}
这是我在测试中想要做的
@Test
void getUserMoviesPerUser() throws Exception{
ArrayList<UserMovies> userMovies = new ArrayList<>();
userMovies.add(new UserMovies(1, "jamie", 1));
userMovies.add(new UserMovies(2, "joe", 1));
userMovies.add(new UserMovies(3, "jamie", 2));
userMovies.add(new UserMovies(4, "joe", 2));
when(userMoviesRepository.findByUname("jamie")).thenReturn(userMovies);
mockMvc.perform(get("/getUserMovies/{uname}", "jamie"))
.andDo(print())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].userMovieID", is(1)))
.andExpect(jsonPath("$[0].uname", is("jamie")))
.andExpect(jsonPath("$[0].movieId", is(1)))
.andExpect(jsonPath("$[1].userMovieID", is(3)))
.andExpect(jsonPath("$[1].uname", is("jamie")))
.andExpect(jsonPath("$[1].movieId", is(1)));
Mockito.verify(userMoviesRepository, times(1)).findAll();
}
我面临的问题是,当我在上面运行测试时,我没有像预期的那样只得到2条记录,而是得到4条。这在我不能依靠SQL来运行测试的事实中是有道理的。有没有更好的方法来编写我的控制器以基于movie_id获取数据?
问题来源:Stack Overflow
在您的代码中,您要创建4 records用于模拟的内容,然后只需删除即可2 of them,2 records以得到响应。
@Test
void getUserMoviesPerUser() throws Exception{
ArrayList<UserMovies> userMovies = new ArrayList<>();
userMovies.add(new UserMovies(1, "jamie", 1));
userMovies.add(new UserMovies(2, "joe", 1));
when(userMoviesRepository.findByUname("jamie")).thenReturn(userMovies);
mockMvc.perform(get("/getUserMovies/{uname}", "jamie"))
.andDo(print())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].userMovieID", is(1)))
.andExpect(jsonPath("$[0].uname", is("jamie")))
.andExpect(jsonPath("$[0].movieId", is(1)))
.andExpect(jsonPath("$[1].userMovieID", is(3)))
.andExpect(jsonPath("$[1].uname", is("jamie")))
.andExpect(jsonPath("$[1].movieId", is(1)));
Mockito.verify(userMoviesRepository, times(1)).findAll();
}
回答来源:Stack Overflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。