如何获取被测试函数调用的通过模拟注入的方法的方法参数?

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

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&lt;String, Integer&gt; map = new Hashmap&lt;&gt;();
        File fp = ResourceUtils.getFile(&quot;classpath:test.txt&quot;);
        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) invocation -> {
String key = invocation.getArgument(0);
});

英文:
when(mapvalues.get(any())).thenAnswer((Answer&lt;String&gt;) invocation -&gt; {
    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(&quot;KEY-VALUE&quot;);

Before doing the test call, in your setup, stub the method that you expect to be invoked

    when(mapvalues.getval(any())).then(new Answer&lt;Integer&gt;() {
		@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(&quot;KEY-VALUE&quot;));

huangapple
  • 本文由 发表于 2020年5月5日 21:05:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61613881.html
匿名

发表评论

匿名网友

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

确定