Java Mockito.invokeMethod:我可以将函数接口的实现作为参数传递吗?
我有一个私有方法,我正在尝试使用powermock进行测试。方法参数是功能接口和对象。根据文档,Whitebox.invokeMethod只接受对象作为参数。传递函数接口的实现是否有某种技巧 即 上面的代码段很理想,但它不会编译,因为方法引用,我很确定所有函数接口的实现都不是对象 编辑: 我试图通过的方法声明:Java Mockito.invokeMethod:我可以将函数接口的实现作为参数传递吗?,java,unit-testing,mockito,powermock,Java,Unit Testing,Mockito,Powermock,我有一个私有方法,我正在尝试使用powermock进行测试。方法参数是功能接口和对象。根据文档,Whitebox.invokeMethod只接受对象作为参数。传递函数接口的实现是否有某种技巧 即 上面的代码段很理想,但它不会编译,因为方法引用,我很确定所有函数接口的实现都不是对象 编辑: 我试图通过的方法声明: public WarehouseOrderProcessorResult process(WarehouseOrderEntity entity, Configuration confi
public WarehouseOrderProcessorResult process(WarehouseOrderEntity entity, Configuration config) throws Exception {...}
我尝试调用的方法的声明:
private void processEntity(ProcessEntityFunc processLambda, WarehouseOrderEntity entity) throws Exception {...}
以及功能接口:
@FunctionalInterface
public interface ProcessEntityFunc {
WarehouseOrderProcessorResult process(WarehouseOrderEntity entity, Configuration config) throws Exception;
}
关于方法引用和函数接口的实现,您是不对的,您可以将lambda放入数组:
Object myObject = new Object();
Consumer<Object> consumer = (Object item) -> System.out.println("Hello world");
Object[] arguments = new Object[]{consumer, myObject};
WarehouseOrderFieldFormattingProcessor warehouseOrderFieldFormattingProcessor = new WarehouseOrderFieldFormattingProcessor();
ProcessEntityFunc testFunction = warehouseOrderFieldFormattingProcessor::process;
Object[] arguments = new Object[]{testFunction, warehouseOrderEntity};
Whitebox.invokeMethod(processor, "processEntity", arguments);
关于方法引用和函数接口的实现,您是不对的,您可以将lambda放入数组:
Object myObject = new Object();
Consumer<Object> consumer = (Object item) -> System.out.println("Hello world");
Object[] arguments = new Object[]{consumer, myObject};
WarehouseOrderFieldFormattingProcessor warehouseOrderFieldFormattingProcessor = new WarehouseOrderFieldFormattingProcessor();
ProcessEntityFunc testFunction = warehouseOrderFieldFormattingProcessor::process;
Object[] arguments = new Object[]{testFunction, warehouseOrderEntity};
Whitebox.invokeMethod(processor, "processEntity", arguments);
你们能给我看一下你们的测试方法可以接受的功能接口吗?你们能给我看一下你们的测试方法可以接受的功能接口吗?