【问题标题】:About @InjectMocks and @Mock关于@InjectMocks 和@Mock
【发布时间】:2021-11-17 09:23:35
【问题描述】:

我想测试requestListServiceImpl这个类。它包含requestListDao。

RequestListServiceImpl 代码

@Override
    public PageInfo<RequestList> getAllRequest(int startPage, int pageSize, String accountName,String userName) {
        PageHelper.startPage(startPage, pageSize);
        List<RequestList> list=requestListDao.getAllRequest(accountName,userName);
        for(RequestList requestList:list) {
            switch (requestList.getStatus()) {
            case "0":
                requestList.setStatus("Waiting");
                break;
            case "1":
                requestList.setStatus("Closed");        
                break;
            case "2":
                requestList.setStatus("Cancel");
                break;
            default:
                requestList.setStatus("NAN");
                break;
            }
        }
        PageInfo<RequestList> pageInfo = new PageInfo<RequestList>(list);
        return pageInfo;
    }

@Override
    public void createRequest(RequestList requestList) {
        Integer coount=requestListDao.getTime(requestList.getCreateName());
//      AWSSnsUtil.sendMassageToSns(requestList.getAccountName());
        requestList.setTime(++coount);
        requestListDao.createRequest(requestList);
    }

测试代码:

    @InjectMocks
    RequestListServiceImpl requestListServiceImpl;
    @Mock
    RequestListDao requestListDao;

 @Before
    public void setup(){
        MockitoAnnotations.openMocks(this);
    }


    @Test  //it's ok 
    public void testGetAllRequest() throws Exception {
        RequestList r=new RequestList();
        r.setStatus("0");
        when(requestListDao.getAllRequest(anyString(), anyString())).thenReturn(Arrays.<RequestList>asList(r));
        PageInfo<RequestList> res=requestListServiceImpl.getAllRequest(1,1,"1","1");
        Assert.assertNotNull(res);


    }

 @Test   //this test is error, the error is requestListServiceImpl is not mock.
    public void testCreateRequest() throws Exception {
        when(requestListDao.getTime(anyString())).thenReturn(0);
        RequestList r=new RequestList();
        r.setCreateName("demo");
        requestListServiceImpl.createRequest(r);
        verify(requestListServiceImpl).createRequest(r);

    }

错误信息

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to verify() is of type RequestListServiceImpl and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
    verify(mock).someMethod();
    verify(mock, times(10)).someMethod();
    verify(mock, atLeastOnce()).someMethod();

当我将@InjectMocks 更改为@Mock 时,第二次测试没问题,但第一次测试它是错误的。 我需要添加到存根 when(requestListServiceImpl.getAllRequest(anyInt(),anyInt(),anyString(),anyString())).thenReturn(new PageInfo&lt;&gt;());

我不知道该怎么做,

injectmocks 有必要吗?

【问题讨论】:

    标签: java unit-testing junit mockito


    【解决方案1】:

    这里:

    verify(requestListServiceImpl)
    

    @InjectMocks
    RequestListServiceImpl requestListServiceImpl;
    

    信息非常明确:您验证 模拟 Mockito 为您创建的对象。

    @InjectMocks 确实创建模拟。而是将测试类中的模拟注入真实对象中。

    所以,这里有什么必要:阅读手册或教程。含义:不要立即将 Mockito 应用到您的项目中。相反,退后一步,看看简单的例子来理解如何它应该被使用。

    在您当前的设置中:requestListServiceImpl 是一个真实对象。因此,您应该检查该类是否为您提供验证其内部状态的方法。你触发了一些动作,但因为它是一个真实的对象,Mockito 不知道你做了什么。但是如何实际解决这个问题取决于您的代码库。

    【讨论】:

    • 你的意思是我能做到吗? \n``` @Test public void testCreateRequest() throws Exception { doReturn(1).when(requestListDao).getTime(anyString()); int res=requestListDao.getTime("1");断言.assertEquals(1,res); }```
    • 提示:不要请求许可。去试试吧。你通过尝试学得最好。再说一遍:最好退后一步。与其尝试将 Mockito 与您的设置相结合,首先了解 mockito 的工作原理。如果我没记错的话,那个 DAO 对象是一个 MOCK。 “验证”模拟对象本身没有意义。因此,只涉及 MOCK 的测试是没有意义的。
    【解决方案2】:

    你想测试requestListServiceImpl 你应该使用@InjectMocks 所以mockito 调用requestListServiceImpl 上的真实方法。当您使用@Mock 时,默认情况下不会调用该方法。 @InjectMocks 将与您使用 new requestListServiceImpl(mock(requestListDao)) 自己创建一样

    当您使用verify(mock).someMethod(); 时,您必须将模拟传递给该方法,而不是@InjectMocks。 您想验证是否在您的测试类中的模拟上调用了某个方法(requestListServiceImpl)。

    除了@InjectMocks 之外,还有@Spy,这两者都可以,你可以模拟requestListServiceImpl 中的方法,但也可以调用真实的方法。

    【讨论】:

    • 你的意思是我能做到吗? ``` @Test public void testCreateRequest() 抛出异常 { doReturn(1).when(requestListDao).getTime(anyString()); int res=requestListDao.getTime("1");断言.assertEquals(1,res); }```
    猜你喜欢
    • 2013-12-29
    • 1970-01-01
    • 2023-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    相关资源
    最近更新 更多