Mockito 和 Java Spring - 测试中的存储库 [英] Mockito and Java Spring - repository in tests

查看:38
本文介绍了Mockito 和 Java Spring - 测试中的存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 mockito/Java Spring 的新手,我尝试进行测试.我有一个管理控制器,其中包含此方法:

I am new to mockito / Java Spring and I tried to make a test. I have an admin controller, with this method in it :

@RequestMapping(value="/admin/users", method = RequestMethod.GET)
public ResponseEntity<List<User>>users(){
    List<User> students=this.userService.getAll();
    if(students.isEmpty())
        return new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);
    return  new ResponseEntity<List<User>>(students,HttpStatus.OK);
}

现在,我试着做一个测试,看看它是否有效,就像这样:

Now, I tried to make a test, to see if it works, something like this :

public class AdminControllerTest {

    @InjectMocks
    private AdminController controller;

    @InjectMocks
    private UserServiceImpl userService = new UserServiceImpl();

    @Mock
    private UserRepository userRepository;

    private MockMvc mockMvc;

    @Before
    public void setup(){

        MockitoAnnotations.initMocks(this);

        mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
    }

    @Test
    public void test() throws Exception {

        User user = new User();
        user.setId(5);
        user.setActive(true);
        user.setLastName("Test1");
        user.setName("Test1");
        user.setPassword("123123");
        user.setRole(User.Role.student);
        user.setEmail("test@gmail.com");
        when(userService.save(user)).thenReturn(user);
        userService.save(user);
        mockMvc.perform(get("/admin/users")).andDo(print());

    }

}

问题是我不确定如何让 Test 类向存储库添加项目.我以这种方式尝试过,但我得到了 NullPointerExceptions.我猜这是因为存储库是空的,当调用 .getAll() 方法时,它会返回错误.

The problem is that I am not sure how to make the Test class add items to the repository. I tried it this way but I get NullPointerExceptions. I am guessing it is because the repository is empty and when the .getAll() method is called it returns the error.

推荐答案

由于您已经模拟了存储库,因此您无需将内容保存到其中.您需要做的就是指定调用某些方法时存储库将返回的内容.

Since you've mocked out the repository, you don't need to save things to it. All you need to do is specify what the repository would return when certain methods are called.

代替:

when(userService.save(user)).thenReturn(user);

...尝试使用:

when(userRepository.findAll()).thenReturn(Collections.singletonList(user));

为什么它不起作用

您收到 NullPointerException 的原因之一是:

The reason you were getting NullPointerException was one of:

  1. 您在一个甚至不是模拟的对象上设置 when,和/或
  2. 您没有为 userRepository.findAll() 方法提供 when,因此在您调用它时返回 null:

  1. you are setting up a when on an object that isn't even a mock, and/or
  2. you didn't provide a when for the userRepository.findAll() method, so it returned null when you called it:

List<User> students=this.userService.getAll(); <-- returned null

... 然后取消引用并抛出 NPE:

... which was then dereferenced and threw the NPE:

if(students.isEmpty()) <-- throws NPE when students is null

这篇关于Mockito 和 Java Spring - 测试中的存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆