开发者社区> 问答> 正文

我的存储库中带有@Query的MockMVC还是重构了控制器以不使用它?

我的存储库中有一个@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

展开
收起
montos 2020-03-23 14:33:54 475 0
1 条回答
写回答
取消 提交回答
  • 在您的代码中,您要创建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

    2020-03-23 14:34:27
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
ui-model,跨框架复用 立即下载
开源广进-用Service Catalog构造K8S服务能力 立即下载
低代码开发师(初级)实战教程 立即下载