如何用Mockery模拟接收不同字符串数组参数的testMethod方法?
testMethod(String[] args) for Specific Array Content Scenarios Got it, let's work through this mocking problem together! Since you're dealing with a Java method that accepts a String[] and returns an instance of X, Mockito is the go-to framework here. The key challenge is matching the array based on its contained elements (not just reference equality), so let's break down the solution step by step.
Step 1: Add Mockito Dependency (if not already included)
If you're using Maven, add this to your pom.xml to pull in Mockito:
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>4.11.0</version> <!-- Use the latest stable version available --> <scope>test</scope> </dependency>
Step 2: Mock the Method with Custom Argument Matching
Mockito's default array matching checks for reference equality, which doesn't work when we just care about the array containing specific values. Instead, we'll use argThat with lambda expressions (Java 8+) to define custom matching logic for each scenario.
Full Test Case Example
import org.junit.Test; import org.mockito.Mockito; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class YourTestClass { @Test public void testMockedTestMethodBehavior() { // 1. Create a mock of the class that contains testMethod YourClass mockInstance = Mockito.mock(YourClass.class); // 2. Prepare your return instances x1 and x2 (customize these as needed) X x1 = new X(); X x2 = new X(); // 3. Mock Scenario 1: Return x1 when args contains both "AAA" and "BBB" Mockito.when(mockInstance.testMethod(Mockito.argThat(args -> { if (args == null) return false; Set<String> elements = new HashSet<>(Arrays.asList(args)); return elements.contains("AAA") && elements.contains("BBB"); }))).thenReturn(x1); // 4. Mock Scenario 2: Return x2 when args contains both "CCC" and "DDD" Mockito.when(mockInstance.testMethod(Mockito.argThat(args -> { if (args == null) return false; Set<String> elements = new HashSet<>(Arrays.asList(args)); return elements.contains("CCC") && elements.contains("DDD"); }))).thenReturn(x2); // 5. Verify the mock behaves as expected assert mockInstance.testMethod(new String[]{"AAA", "BBB"}).equals(x1); assert mockInstance.testMethod(new String[]{"XXX", "AAA", "BBB", "YYY"}).equals(x1); assert mockInstance.testMethod(new String[]{"CCC", "DDD"}).equals(x2); assert mockInstance.testMethod(new String[]{"DDD", "CCC", "ZZZ"}).equals(x2); } // Dummy class definitions (replace these with your actual classes) static class YourClass { public X testMethod(String[] args) { return null; // Real implementation would go here } } static class X {} }
Key Notes for Customization
- Order doesn't matter: The
HashSetapproach ignores the order of elements in the array. If you need to match a strict order (e.g., the array must be exactly["AAA", "BBB"]), replace the custom matcher withMockito.eq(new String[]{"AAA", "BBB"}). - Reusable matchers: If you need these matching conditions in multiple tests, extract them into separate
ArgumentMatcherclasses instead of inline lambdas for cleaner code. - Null safety: The matcher checks for a null input array first to avoid
NullPointerExceptionduring tests.
内容的提问来源于stack exchange,提问作者pankaj_ar

