Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何调用私有方法来测试它是否具有Tuple5参数?_Java_Spring_Testing - Fatal编程技术网

Java 如何调用私有方法来测试它是否具有Tuple5参数?

Java 如何调用私有方法来测试它是否具有Tuple5参数?,java,spring,testing,Java,Spring,Testing,我需要这个测试来完成这个方法。我有这门课: public class MyClass { private void myClassMethod(Tuple5<List<Index>, List<Category>, Optional<Category>, List<CategoryIndex>, List<TopIndex>> objects) { List<Index> i

我需要这个测试来完成这个方法。我有这门课:

public class MyClass {
      private void myClassMethod(Tuple5<List<Index>, List<Category>, Optional<Category>, List<CategoryIndex>, List<TopIndex>> objects) {

            List<Index> indexes = objects.getT1();
            List<Category> categories = objects.getT2();
            Optional<Category> defaultCategory = objects.getT3();
            List<CategoryIndex> categoryIndexes = objects.getT4();
            List<TopIndex> topIndexes = objects.getT5();              
}
公共类MyClass{
私有void myClassMethod(Tuple5对象){
列表索引=objects.getT1();
List categories=objects.getT2();
可选的defaultCategory=objects.getT3();
List CategoryIndex=objects.getT4();
List topIndexes=objects.getT5();
}
我不允许更改此类。我不能将该方法的可见性从私有更改为公共,也不能向该类添加其他方法。我不允许在此类中写入任何字符

所以我写了这个测试:

// here are some mocks...
@InjectMocks
private MyClass myClass;

@Test
public void testMyClassMethod() throws Exception {

    Class[] cArg = new Class[1];
    cArg[0] = Tuple5.class;

    Method method = MyClass.class.getDeclaredMethod("myClass", cArg);
    method.setAccessible(true);

    List<Index> indexList = new ArrayList<>();
    List<Category> categoryList = new ArrayList<>();
    Optional<Category> categoryOptional = Optional.empty();
    List<CategoryIndex> categoryIndexList = new ArrayList<>();
    List<TopIndex> topIndexList = new ArrayList<>();

    Quintet<List<Index>, List<Category>, Optional<Category>, List<CategoryIndex>, List<TopIndex>>[] arr = new Quintet[1];
    arr[0] = Quintet.with(indexList, categoryList, categoryOptional, categoryIndexList, topIndexList);

    
    method.invoke(myClass, arr);
}
//下面是一些模拟。。。
@注射模拟
私人MyClass MyClass;
@试验
public void testMyClassMethod()引发异常{
类别[]cArg=新类别[1];
cArg[0]=Tuple5.class;
方法Method=MyClass.class.getDeclaredMethod(“MyClass”,cArg);
方法setAccessible(true);
列表索引列表=新的ArrayList();
List categoryList=新建ArrayList();
Optional categoryOptional=Optional.empty();
List categoryIndexList=新建ArrayList();
List topIndexList=new ArrayList();
五重奏[]arr=新五重奏[1];
arr[0]=带有(索引列表、类别列表、类别可选、类别索引列表、topIndexList)的五重奏;
调用(myClass,arr);
}
如果我尝试运行,最后一行(
method.invoke(myClass,arr)
)会引发以下异常:

java.lang.IllegalArgumentException:处的参数类型不匹配 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于 位于的sun.reflect.NativeMethodAccessorImpl.invoke(未知源) sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于 java.lang.reflect.Method.invoke(未知源代码)位于 myTestPackage.testMyClassMethod(MyClassTest.java:118)位于 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于 位于的sun.reflect.NativeMethodAccessorImpl.invoke(未知源) sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于 java.lang.reflect.Method.invoke(未知源代码)位于 org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:675) 在 org.junit.jupiter.engine.execution.MethodInvocation.procedue(MethodInvocation.java:60) 在 org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.procedue(InvocationInterceptorChain.java:125) 在 org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:132) 在 org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:124) 在 org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:74) 在 org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115) 在 org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105) 在 org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.procedue(InvocationInterceptorChain.java:104) 在 org.junit.jupiter.engine.execution.InvocationInterceptorChain.procedure(InvocationInterceptorChain.java:62) 在 org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:43) 在 org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:35) 在 org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104) 在 org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98) 在 org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:202) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:198) 在 org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135) 在 org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:69) 在 org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$ExecuteCursive$5(NodeTestTask.java:135) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$executecurively$7(NodeTestTask.java:125) 在 org.junit.platform.engine.support.hierarchy.Node.around(Node.java:135) 在 org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$ExecuteCursive$8(NodeTestTask.java:123) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchy.NodeTestTask.ExecuteCursive(NodeTestTask.java:122) 在 org.junit.platform.engine.support.hierarchy.NodeTestTask.execute(NodeTestTask.java:80) 位于java.util.ArrayList.forEach(未知源代码) org.junit.platform.engine.support.hierarchy.samethreadherarchycalTestExecutorService.invokeAll(samethreadherarchycalTestExecutorService.java:38) 在 org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$executecurively$5(NodeTestTask.java:139) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$executecurively$7(NodeTestTask.java:125) 在 org.junit.platform.engine.support.hierarchy.Node.around(Node.java:135) 在 org.junit.platform.engine.support.hierarchy.NodeTestTask.lambda$ExecuteCursive$8(NodeTestTask.java:123) 在 org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) 在 org.junit.platform.engine.support.hierarchy.NodeTestTask.ExecuteCursive(NodeTestTask.java:122) 在 org.junit.platform.engine.support.hierarchy.No
class Gun {
   void shoot(Person p) {
       ...
   }
}
class Camera {
    void shoot(Person p) {
        ...
    }
}
interface Shooter {
    void shoot(Person p) { ... }
}
Shooter s = new Camera(); // nope