我正在尝试对在 Consumer 功能接口中作为回调运行的代码进行单元测试。
@Component
class SomeClass {
@Autowired
private SomeInteface toBeMockedDependency;
public method() {
toBeMockedDependency.doSomething(message -> {
// Logic under test goes here
// (implements java.util.function.Consumer interface)
...
});
}
}
@RunWith(MockitoJUnitRunner.class)
public class SomeClassTest {
@InjectMocks
private SomeClass someClass;
@Mock
private SomeInteface toBeMockedDependency;
@Test
public void testMethod() {
...
someClass.method();
...
}
}
Run Code Online (Sandbox Code Playgroud)
本质上,我想通过“toBeMockedDependency”向测试代码提供一些经过测试的“消息”。
如何模拟“toBeMockedDependency”以提供预定义的消息?
这是正确的方法吗?
我有点困惑为什么这不起作用。我有一个简单Iterable的问题String,我想通过toSortedSet()自己的方式进行排序。我想像这样将 lambda 传递给它:
myStringIterable.toSortedSet({a,b -> a.compareTo(b)})
Run Code Online (Sandbox Code Playgroud)
然而,这似乎不起作用。错误说
类型不匹配。必需的 kotlin.Comparator <String>
找到:(String,String) -> Int
Comparator 是一个Functional Interface,所以我应该能够将它作为 Lambda 传递,不是吗?
我正在使用分别用 Java 和 Kotlin 编码的两个模块,因此需要一些互操作性。
在其中一种情况下,java 模块返回类型为 的函数接口java.util.function.Function,该接口将由 kotlin 代码使用。
我可以按原样使用 java 类型。但是,我希望能够将其转换为 kotlin 的本机类型,无论是它kotlin.Function还是kotlin.reflect.KFunction. 这样做的原因是能够支持与 java 模块类似的 kotlin 模块,该模块为客户端提供功能。
有什么办法可以做到这一点吗?
我想获得一些帮助,以了解有关 Http4korg.http4k.core包中使用的功能接口的 Kotlin 代码片段
typealias HttpHandler = (Request) -> Response
fun interface Filter : (HttpHandler) -> HttpHandler {
companion object
}
Run Code Online (Sandbox Code Playgroud)
我不了解Filter界面,尤其是companion object部分。一个典型的功能界面是这样的
fun interface IntPredicate {
fun accept(i: Int): Boolean
}
Run Code Online (Sandbox Code Playgroud)
你可以创建一个 lambda isEven
val isEven = IntPredicate { it % 2 == 0 }
Run Code Online (Sandbox Code Playgroud)
根据这个简单的例子,看起来接口Filter扩展了另一个接口(HttpHandler) -> HttpHandler?然后它定义了一个函数签名companion object?这样对吗?这部分的companion object真正含义是什么?
在Java 8中,提供了不同的谓词接口(例如DoublePredicate,LongPredicate,IntPredicate等).现在,如果您要实现接口并在其中编写自己的代码,那么拥有不同谓词接口的优势是什么?为什么不只是一个谓词接口?
我在https://dzone.com/articles/supplier-interface上看到了一些使用供应商界面的例子.
我的问题是,如果在上面的例子中,我可以做一些简单的事情:
driveVehicle(new Vehicle());
driveVehicle(new Car());
Run Code Online (Sandbox Code Playgroud)
为什么要使用供应商界面,如果它只是调用方法,而不接受任何参数.
在isBigOrder方法中,如果订单中产品的总价格大于1000,则必须返回true.我怎么用java 8编写它?我写了总和部分,但我无法完成它.
public Function<Order, Boolean> isBigOrder() {
Function<Order, Optional<Long>> sum = a -> a.getProducts()
.stream()
.map(P -> P.getPrice())
.reduce((p1,p2)->p1+p2);
Predicate <Optional<Long>> isBig = x -> x.get() > 1000 ;
return ????;
}
Run Code Online (Sandbox Code Playgroud)
如果需要其他类:
enum OrderState { CONFIRMED, PAID, WAREHOUSE_PROCESSED, READY_TO_SEND, DELIVERED }
enum ProductType { NORMAL, BREAKABLE, PERISHABLE }
public class Product {
private String code;
private String title;
private long price;
private ProductState state;
private ProductType type;
//all fields have getter and setter
public Product price(long price) …Run Code Online (Sandbox Code Playgroud) java functional-programming predicate java-8 functional-interface
如何将Runnable其转换为Supplier?
public <T> T useSupplier(Supplier<T> supplier) {
// Does something with supplier and returns supplied value
...
return value;
}
public void useRunnable(Runnable runnable) {
// Somehow convert runnable to Supplier
...
useSupplier(supplier);
}
Run Code Online (Sandbox Code Playgroud)
在这里我想重用的方法useSupplier进行useRunnable,例如,因为我不想重复的代码.useSupplier对于这个问题,行为无关紧要,假设它包装抛出异常,或者在同步块中使用供应商.
编辑:为了澄清,该方法useSupplier不与提供的值交互,它只返回它.功能useSupplier是在某些上下文中从供应商检索值,在我的情况下它捕获(特定)RuntimeExceptions,创建一个新的异常,并将其作为原因并抛出它:
public <T> T useSupplier(Supplier<T> supplier) {
try {
return supplier.get();
}
catch (RuntimeException runtimeException) {
throw new MyException("Supplier threw exception", runtimeException);
}
}
Run Code Online (Sandbox Code Playgroud)
下面的解决方案并没有(在Java中8)工作:
useSupplier(runnable);
useSupplier(runnable::run); …Run Code Online (Sandbox Code Playgroud) 在初始化集合(如TreeMap,TreeSet等)时,我们可以添加自定义比较器。该代码如下所示:
Map<Integer, String> map1 = new TreeMap<>(new Comparator<Integer>() {
public int compare(Integer x, Integer y) {
return x-y;
}
});
Run Code Online (Sandbox Code Playgroud)
现在,我们可以用lambda表达式替换此匿名实现。代码如下所示:
Map<Integer, String> map2 = new TreeMap<>((x,y) -> x-y);
Run Code Online (Sandbox Code Playgroud)
Java-8允许您通过功能接口将lambda表达式存储在变量中。因此,我将上面的代码修改为以下代码:
BiFunction<Integer, Integer, Integer> myComparator = (x,y) -> x-y;
Map<Integer, String> map3 = new TreeMap<>(myComparator);
Run Code Online (Sandbox Code Playgroud)
但是最后一次尝试没有用!它给出了以下错误:
无法推断TreeMap <>的类型参数
为什么在上一个示例中无法解析类型?
注意:为了确认这不是IDE错误,我使用进行了原始编译javac,但仍给出相同的错误。
考虑以下两个功能接口(java.lang.Runnable and java.util.concurrent.Callable<V>):
public interface Runnable {
void run();
}
public interface Callable<V> {
V call();
}
Run Code Online (Sandbox Code Playgroud)
假设您有overloaded如下方法调用:
void invoke(Runnable r) {
r.run();
}
<T> T invoke(Callable<T> c) {
return c.call();
}
Run Code Online (Sandbox Code Playgroud)
考虑以下方法调用
String s = invoke(() -> "done");
Run Code Online (Sandbox Code Playgroud)
这会打电话给invoke(Callable)。但是如何?编译器如何确定Type为可调用类型?我从阅读的Oracle文档中看不懂。