无效使用参数匹配器!需要2个匹配器,但只记录了1个。

huangapple go评论75阅读模式
英文:

Invalid use of argument matchers! 2 matchers expected, 1 recorded

问题

以下是您提供的代码的翻译部分:

package com.service;

import com.model.Bin;
import com.model.Trolley;
import com.model.dao.NewTrolleyDao;
import com.model.enums.BinType;
import com.model.enums.TrolleyType;

import com.repository.TrolleyRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TrolleyServiceTests {

    @MockBean
    private TrolleyRepository trolleyRepository;

    private TrolleyService trolleyService;

    private static final long SH_CODE = 123;
    private static final TrolleyType trolleyType = TrolleyType.QS;
    private List<Trolley> trolleyList = new ArrayList<>();

    @Before
    public void setUp() {
        Answer<Trolley> answer = new Answer<Trolley>() {
            public Trolley answer(InvocationOnMock invocation) {
                Trolley trolley = invocation.getArgument(0, Trolley.class);
                trolleyList.add(trolley);
                System.out.println(trolleyList.size());
                return trolley;
            }
        };
        Mockito.when(trolleyRepository.findAllByStoreHouseCode(SH_CODE, any(PageRequest.class))).
                thenReturn(getTrolleyList());
        Mockito.when(trolleyRepository.save(any(Trolley.class))).
                thenAnswer(answer);
        trolleyService = new TrolleyService(trolleyRepository);
    }

    private Page<Trolley> getTrolleyList() {
        Page<Trolley> trolleys = new PageImpl<Trolley>(trolleyList);
        return trolleys;
    }

    private NewTrolleyDao getNewTrolleyDao(int size) {
        NewTrolleyDao newTrolleyDao = new NewTrolleyDao();
        newTrolleyDao.setStoreHouseCode(SH_CODE);
        newTrolleyDao.setType(trolleyType);
        newTrolleyDao.setSize(size);
        return newTrolleyDao;
    }

    @Test
    public void testCreateTrolleys() {
        NewTrolleyDao trolleyDao = getNewTrolleyDao(30);
        Page<Trolley> res = trolleyService.createTrolleys(trolleyDao);
        assertThat(res.getTotalElements()).isEqualTo(30);
    }

}

请注意,这只是您提供的代码的翻译部分。如果您有其他问题或需要进一步的帮助,请随时提问。

英文:

I am writing a test to validate creations of trolleys. So,it is just a service method which basically creates number of trolleys in the database. I am mocking the repository using mockito.

So, What i am doing is I am mocking and save and getAll functionality of Repository.

Here is what my code looks like:-

package com.service;
import com.model.Bin;
import com.model.Trolley;
import com.model.dao.NewTrolleyDao;
import com.model.enums.BinType;
import com.model.enums.TrolleyType;
import com.repository.TrolleyRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TrolleyServiceTests {
@MockBean
private TrolleyRepository trolleyRepository;
private TrolleyService trolleyService;
private static final long SH_CODE = 123;
private static final TrolleyType trolleyType = TrolleyType.QS;
private List&lt;Trolley&gt; trolleyList = new ArrayList&lt;&gt;();
@Before
public void setUp() {
Answer&lt;Trolley&gt; answer = new Answer&lt;Trolley&gt;() {
public Trolley answer(InvocationOnMock invocation) {
Trolley trolley = invocation.getArgument(0, Trolley.class);
trolleyList.add(trolley);
System.out.println(trolleyList.size());
return trolley;
}
};
Mockito.when(trolleyRepository.findAllByStoreHouseCode(SH_CODE, any(PageRequest.class))).
thenReturn(getTrolleyList());
Mockito.when(trolleyRepository.save(any(Trolley.class))).
thenAnswer(answer);
trolleyService = new TrolleyService(trolleyRepository);
}
private Page&lt;Trolley&gt; getTrolleyList() {
Page&lt;Trolley&gt; trolleys = new PageImpl&lt;Trolley&gt;(trolleyList);
return  trolleys;
}
private NewTrolleyDao getNewTrolleyDao(int size) {
NewTrolleyDao newTrolleyDao = new NewTrolleyDao();
newTrolleyDao.setStoreHouseCode(SH_CODE);
newTrolleyDao.setType(trolleyType);
newTrolleyDao.setSize(size);
return newTrolleyDao;
}
@Test
public void testCreateTrolleys() {
NewTrolleyDao trolleyDao = getNewTrolleyDao(30);
Page&lt;Trolley&gt; res = trolleyService.createTrolleys(trolleyDao);
assertThat(res.getTotalElements()).isEqualTo(30);
}
}

So initially i am just taking an empty array list and mocking the save method to add to the list. Because service method calls the save method which takes a trolley and save it to db for each trolley it creates and it returns Page Of Trolley which calls the findAllByStoreHouseCode which takes storehousecode and a PageRequest.

Error occurs on the line where i am using the any ArgumentMatcher.
I am new to testing could any one please tell me where i am wrong or this approach is wrong and i should implement test in some other way.

Here is the error:-

-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Test set: com.service.TrolleyServiceTests
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.948 s &lt;&lt;&lt; FAILURE! - in com.service.TrolleyServiceTests
testCreateTrolleys  Time elapsed: 0.037 s  &lt;&lt;&lt; ERROR!
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-&gt; at com.service.TrolleyServiceTests.setUp(TrolleyServiceTests.java:52)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), &quot;raw String&quot;);
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq(&quot;String by matcher&quot;));
For more info see javadoc for Matchers class.
at com.service.TrolleyServiceTests.setUp(TrolleyServiceTests.java:52)

答案1

得分: 6

尝试将您的Mockito设置更改为以下内容:

Mockito.when(trolleyRepository.findAllByStoreHouseCode(Mockito.eq(SH_CODE), Mockito.any(PageRequest.class))).thenReturn(getTrolleyList());
Mockito.when(trolleyRepository.save(Mockito.any(Trolley.class))).thenAnswer(answer);
英文:

Try changing your Mockito setup to below

Mockito.when(trolleyRepository.findAllByStoreHouseCode(Mockito.eq(SH_CODE), Mockito.any(PageRequest.class))).thenReturn(getTrolleyList());
Mockito.when(trolleyRepository.save(Mockito.any(Trolley.class))).thenAnswer(answer);

huangapple
  • 本文由 发表于 2020年8月26日 15:10:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63592299.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定