英文:
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<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);
}
}
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 <<< FAILURE! - in com.service.TrolleyServiceTests
testCreateTrolleys Time elapsed: 0.037 s <<< ERROR!
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.service.TrolleyServiceTests.setUp(TrolleyServiceTests.java:52)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论