英文:
How to retrieve the method parameters of a mock-injected method, which is called by the function being tested?
问题
public class SearchServiceTest {
@InjectMocks
SearchService searchService;
@Mock
Mapvalues mapvalues;
@Before
public void setUp() throws FileNotFoundException {
MockitoAnnotations.initMocks(this);
Map<String, Integer> map = new HashMap<>();
File fp = ResourceUtils.getFile("classpath:test.txt");
Scanner sc = new Scanner(fp);
while (sc.hasNextLine()) {
String line = sc.nextLine();
map.put(line, 300);
}
// Rest of your setup code...
}
@Test
public void testDoSomething() {
// Modify this part to capture the key passed to the method
ArgumentCaptor<String> keyCaptor = ArgumentCaptor.forClass(String.class);
when(mapvalues.getval(keyCaptor.capture())).thenReturn(300);
searchService.doSomething();
// Retrieve the captured key
String capturedKey = keyCaptor.getValue();
int retrievedValue = map.get(capturedKey); // Use the captured key to get the value from the map
// Assertions or verifications...
}
}
Note: The code provided includes modifications to capture the key parameter passed to mapvalues.getval()
using Mockito's ArgumentCaptor
and then retrieve the corresponding value from the map
. The comments in the code highlight the relevant changes. Please ensure that you import the necessary classes and interfaces for the code to work properly.
英文:
public class SearchServiceTest
{
@InjectMocks
SearchService searchService;
@Mock
Mapvalues mapvalues;
@Before
public void setUp() throws FileNotFoundException
{
MockitoAnnotations.initMocks(this);
Map<String, Integer> map = new Hashmap<>();
File fp = ResourceUtils.getFile("classpath:test.txt");
Scanner sc = new Scanner(fp);
while (sc.hasNextLine())
{
String line = sc.nextLine();
map.put(line, 300);
}
}
@Test
public void testDoSomething()
{
searchService.doSomething();
//so basically this doSomething() method calls the method mapvalues.getval(String key),
//but instead I want to perform map.get(key) when the method is called.
}
}
So the doSomething() method calls the mapvalues.getval(String key) method, which returns an integer value, but I want to pass the key value to map.get(key) when the method is called. How do I retrieve that parameter?
答案1
得分: 0
当(mapvalues.get(any())).thenAnswer((Answer
String key = invocation.getArgument(0);
});
英文:
when(mapvalues.get(any())).thenAnswer((Answer<String>) invocation -> {
String key = invocation.getArgument(0);
});
答案2
得分: 0
你正在测试 searchService.doSomething();
我将假设这个方法的主体包含语句 mapvalues.getval("KEY-VALUE");
在进行测试调用之前,在你的设置中,对你期望被调用的方法进行存根处理:
when(mapvalues.getval(any())).then(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return map.get(invocation.getArgument(0, String.class));
}
});
在测试调用之后,你希望确保期望的方法已使用预期的参数值进行了调用:
verify(mapvalues).getval(eq("KEY-VALUE"));
英文:
You're testing searchService.doSomething();
I will assume that the body of this method contains the statement mapvalues.getval("KEY-VALUE");
Before doing the test call, in your setup, stub the method that you expect to be invoked
when(mapvalues.getval(any())).then(new Answer<Integer>() {
@Override
public Integer answer(InvocationOnMock invocation) throws Throwable {
return map.get(invocation.getArgument(0, String.class));
}
});
After the test call, you want to make sure the wanted method has been invoked with the expected parameter values.
verify(mapvalues).getval(eq("KEY-VALUE"));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论