mirror of
https://github.com/alibaba/testable-mock.git
synced 2026-08-22 11:13:31 +08:00
Add interface static test case
This commit is contained in:
@@ -1,10 +1,7 @@
|
|||||||
package com.alibaba.demo.lambda;
|
package com.alibaba.demo.lambda;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
@@ -18,13 +15,18 @@ public class StaticInstanceReference {
|
|||||||
private static final StaticClassA STATIC_CLASS_A = new StaticClassA();
|
private static final StaticClassA STATIC_CLASS_A = new StaticClassA();
|
||||||
|
|
||||||
public void staticMethodReference() {
|
public void staticMethodReference() {
|
||||||
StaticClassA a = new StaticClassA();
|
//StaticClassA a = new StaticClassA();
|
||||||
consumesRun(a::doIt);
|
//consumesRun(a::doIt);
|
||||||
consumesRun(STATIC_CLASS_A::doIt);
|
consumesRun(STATIC_CLASS_A::doIt);
|
||||||
consumesFunction1(STATIC_CLASS_A::function1);
|
consumesFunction1(STATIC_CLASS_A::function1);
|
||||||
consumesFunction2(STATIC_CLASS_A::function2);
|
consumesFunction2(STATIC_CLASS_A::function2);
|
||||||
consumesFunction3(STATIC_CLASS_A::function3);
|
consumesFunction3(STATIC_CLASS_A::function3);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void collectionInterfaceDefaultOrStatic() {
|
||||||
blackHole(invokeInterfaceTest());
|
blackHole(invokeInterfaceTest());
|
||||||
|
blackHole(interfaceStaticMethodTest());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void interfaceDefault() {
|
public void interfaceDefault() {
|
||||||
@@ -33,6 +35,11 @@ public class StaticInstanceReference {
|
|||||||
consumesFunction1(l::function1);
|
consumesFunction1(l::function1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void interfaceStatic() {
|
||||||
|
consumesRun(ILambda::staticRun);
|
||||||
|
consumesFunction1(ILambda::staticFunction1);
|
||||||
|
}
|
||||||
|
|
||||||
private void consumesRun(Runnable r) {
|
private void consumesRun(Runnable r) {
|
||||||
r.run();
|
r.run();
|
||||||
}
|
}
|
||||||
@@ -96,6 +103,16 @@ public class StaticInstanceReference {
|
|||||||
.reduce(BigDecimal::add);
|
.reduce(BigDecimal::add);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Object interfaceStaticMethodTest() {
|
||||||
|
List<String[]> zz = new ArrayList<>();
|
||||||
|
zz.add(new String[]{"1"});
|
||||||
|
return zz.stream()
|
||||||
|
.flatMap(Arrays::stream)
|
||||||
|
.map(Double::valueOf)
|
||||||
|
.map(BigDecimal::new)
|
||||||
|
.reduce(BigDecimal::add);
|
||||||
|
}
|
||||||
|
|
||||||
private void blackHole(Object... ignore) {}
|
private void blackHole(Object... ignore) {}
|
||||||
|
|
||||||
public interface ILambda {
|
public interface ILambda {
|
||||||
@@ -106,6 +123,14 @@ public class StaticInstanceReference {
|
|||||||
default void function1(String s) {
|
default void function1(String s) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void staticRun() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void staticFunction1(String s) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class LambdaFoo implements ILambda {
|
public static class LambdaFoo implements ILambda {
|
||||||
|
|||||||
@@ -5,6 +5,9 @@ import com.alibaba.testable.core.annotation.MockInvoke;
|
|||||||
import com.alibaba.testable.core.model.LogLevel;
|
import com.alibaba.testable.core.model.LogLevel;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
|
import static com.alibaba.testable.core.matcher.InvocationVerifier.verifyInvoked;
|
||||||
|
|
||||||
|
|
||||||
@@ -22,24 +25,40 @@ public class StaticInstanceReferenceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@MockInvoke(targetClass = StaticInstanceReference.ILambda.class, targetMethod = "run")
|
@MockInvoke(targetClass = StaticInstanceReference.ILambda.class, targetMethod = "run")
|
||||||
private void mockFooRun() {
|
private void mockILambdaRun() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@MockInvoke(targetClass = StaticInstanceReference.ILambda.class, targetMethod = "function1")
|
@MockInvoke(targetClass = StaticInstanceReference.ILambda.class, targetMethod = "function1")
|
||||||
private void mockIFunction1(String s) {
|
private void mockILambdaFunction1(String s) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@MockInvoke(targetClass = Collection.class, targetMethod = "stream")
|
||||||
|
<E> Stream<E> mockStream() {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldMockT1() {
|
public void shouldMockDoIt() {
|
||||||
instance.staticMethodReference();
|
instance.staticMethodReference();
|
||||||
verifyInvoked("mockDoIt").withTimes(2);
|
verifyInvoked("mockDoIt").withTimes(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldMockFooRun() {
|
public void shouldMockCollectionStream() {
|
||||||
|
instance.collectionInterfaceDefaultOrStatic();
|
||||||
|
verifyInvoked("mockStream").withTimes(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldMockInterfaceDefault() {
|
||||||
instance.interfaceDefault();
|
instance.interfaceDefault();
|
||||||
verifyInvoked("mockFooRun").withTimes(1);
|
verifyInvoked("mockILambdaRun").withTimes(1);
|
||||||
verifyInvoked("mockIFunction1").withTimes(1);
|
verifyInvoked("mockILambdaFunction1").withTimes(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldMockInterfaceStatic() {
|
||||||
|
instance.interfaceStatic();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user