Mockito无法在RESTcontroller中进行更新/创建



>我正在尝试使用 Mockito 编写一些测试,我设法为 findAll 编写了它们并删除操作,但对于创建和更新,它们不起作用。

@RunWith(MockitoJUnitRunner.class)
public class RentedControllerTest {
    @Mock
    private RentedService rentalService;
    @Mock
    private MovieService movieService;
    @Mock
    private ClientService clientService;
    @InjectMocks
    private RentedController rentalController;
    @InjectMocks
    private MovieController movieController;
    @InjectMocks
    private ClientController clientController;

    @Before
    public void setUp() throws Exception {
        initMocks(this);
    }
    @Test
    public void getMovies() throws Exception {
        List<Movie> movies = new ArrayList<>();
        movies.add(mock(Movie.class));
//        System.out.println(movies.toString());
        when(movieService.findAll()).thenReturn(movies);
////        System.out.println(movieService.findAll().toString());
        MoviesDto response = movieController.getMovies();
        assertEquals("should be 1 movie", 1, response.getMovies().size());
    }
    @Test
    public void updateMovie() throws Exception {
        Movie movie = new Movie(2,"Lotr",0, "dir2", 2003);
        MovieDto movieDto = mock(MovieDto.class);
        System.out.println(movie.toString());
        when(movieService.updateMovie(anyInt(), anyString(), anyInt(), anyString(), anyInt())).thenReturn(movie);
//        Movie m = new Movie();
//        m = movieService.updateMovie(2,"Lotrrrrr",0, "dir2", 2003);
//        System.out.println(m.toString());
        Map<String, MovieDto> map = movieController.updateMovie(2, movieDto);
        System.out.println(map.toString());
        assertEquals("Title should be Lots", "Lotr", map.get("movie").getName());
    }
    @Test
    public void createMovie() throws Exception {
        Movie movie = new Movie(2,"Lotr",0, "dir2", 2003);
        MovieDto movieDto = mock(MovieDto.class);
        when(movieService.createMovie(anyInt(), anyString(), anyInt(), anyString(), anyInt())).thenReturn(movie);
        Map<String, MovieDto> map = movieController.createMovie(movieDto);
        assertEquals("Title should be Lotr", "Lotr", map.get("movie").getName());
    }
    @Test
    public void deleteMovie() throws Exception {
        ResponseEntity response = movieController.deleteMovie(1);
        assertEquals("Http status should be OK", HttpStatus.OK, response.getStatusCode());
    }
}

所以"当(电影服务.更新....)工作就好了。正如您在代码中看到的那样,我用写行对其进行了测试,它可以工作。问题就在这里

Map<String, MovieDto> map = movieController.updateMovie(2, movieDto);

我在该方法中给了我NullPointerException。该方法如下所示:

@RequestMapping(value = "/movies/{movieId}", method = RequestMethod.PUT, consumes = CatalogMediaType.API_JSON)
    public Map<String, MovieDto> updateMovie(@PathVariable final Integer movieId,
                                             @RequestBody final MovieDto movieDto) {
        log.trace("updateMovie: movieId={} movieDto={}", movieId, movieDto);
        Movie movie = movieService.updateMovie(movieId, movieDto.getMovie().getName(), movieDto.getMovie().getNumberofrentals(), movieDto.getMovie().getDirector(), movieDto.getMovie().getYear());
        Map<String, MovieDto> movieDtoMap = new HashMap<>();
        movieDtoMap.put("movie", new MovieDto(movie));
        log.trace("updateMovie: movieDtoMap={}", movieDtoMap);
        return movieDtoMap;
    }

应用程序本身运行良好,仅在运行模拟测试时出现问题。

java.lang.NullPointerException
    at ro.ubb.stcatalog.web.controller.MovieController.updateMovie(MovieController.java:41)
    at ro.ubb.stcatalog.web.controller.RentedControllerTest.updateMovie(RentedControllerTest.java:82)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
2016-05-26T23:51:52,845 TRACE [main]: MovieController                      - createMovie: moviesDto=Mock for MovieDto, hashCode: 825249556 
java.lang.NullPointerException
    at ro.ubb.stcatalog.web.controller.MovieController.createMovie(MovieController.java:54)
    at ro.ubb.stcatalog.web.controller.RentedControllerTest.createMovie(RentedControllerTest.java:92)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

我真的不明白问题是什么,如果查找所有和删除工作正常......有什么想法吗?

我相信

MovieDto类的嘲弄导致了NPE,因为您的控制器期望DTO中存在一些参数,而这些参数不是由mock()设置的,例如movieDto.getMovie().getName()

使用所有必需的参数从头开始创建一个MovieDto对象,或者在模拟后设置参数,您应该没问题。

以下应该有效。

@Test
    public void updateMovie() throws Exception {
        Movie movie = new Movie(2,"Lotr",0, "dir2", 2003);
        MovieDto movieDto = mock(MovieDto.class);
        // Added the following line
        movieDto.setMovie(movie);
        System.out.println(movie.toString());
        when(movieService.updateMovie(anyInt(), anyString(), anyInt(), anyString(), anyInt())).thenReturn(movie);
        Map<String, MovieDto> map = movieController.updateMovie(2, movieDto);
        System.out.println(map.toString());
        assertEquals("Title should be Lots", "Lotr", map.get("movie").getName());
    }

看起来没有设置电影控制器的某些依赖项。

java.lang.NullPointerException
at ro.ubb.stcatalog.web.controller.MovieController.createMovie(MovieController.java:54)

您需要先填充 movieDto。当你在实际代码中获取时,就像你正在做一个空值,它给出了一个空指针。而且这两个值应该相同。在条件中,您将movieId告知为anyInt(),而在实际方法调用中,您将它作为2。那行不通。两者应与下面相同。

when(movieService.updateMovie(**2**, anyString(), anyInt(), anyString(), anyInt())).thenReturn(movie);
        Map<String, MovieDto> map = movieController.updateMovie(**2**, movieDto);

相关内容

  • 没有找到相关文章

最新更新