From 49154ee19674564d9014d80ef573e8464a2e62db Mon Sep 17 00:00:00 2001 From: leaderli <429243408@qq.com> Date: Tue, 5 Mar 2024 01:38:07 +0800 Subject: [PATCH] add recordbeans --- .../litool/test/AbstractRecorder.java | 17 +++-- .../java/io/leaderli/litool/test/LiMock.java | 6 +- .../io/leaderli/litool/test/MockBeans.java | 4 +- .../io/leaderli/litool/test/RecordBean.java | 2 +- .../litool/test/RecordBeanInterface.java | 22 ++++++ .../io/leaderli/litool/test/RecordBeans.java | 68 +++++++++++++++++++ .../io/leaderli/litool/test/LiMockTest.java | 27 +++++++- 7 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 litool-test/src/main/java/io/leaderli/litool/test/RecordBeanInterface.java create mode 100644 litool-test/src/main/java/io/leaderli/litool/test/RecordBeans.java diff --git a/litool-test/src/main/java/io/leaderli/litool/test/AbstractRecorder.java b/litool-test/src/main/java/io/leaderli/litool/test/AbstractRecorder.java index 2b34bc44..3e0d082c 100644 --- a/litool-test/src/main/java/io/leaderli/litool/test/AbstractRecorder.java +++ b/litool-test/src/main/java/io/leaderli/litool/test/AbstractRecorder.java @@ -43,13 +43,13 @@ public AbstractRecorder(Class mockClass) { public T called() { actualMethodCall.remove(currentMethod); recordMethodCall.add(currentMethod); - methodAsserts.get(currentMethod).add((method, _this, args, _return) -> actualMethodCall.add(currentMethod)); + add((method, _this, args, _return) -> actualMethodCall.add(currentMethod)); return (T) this; } public T arg(int index, Object arg) { - methodAsserts.get(currentMethod).add((method, _this, args, _return) -> { + add((method, _this, args, _return) -> { Assertions.assertTrue(args.length > index); Assertions.assertEquals(arg, args[index]); }); @@ -57,7 +57,7 @@ public T arg(int index, Object arg) { } public T argAssert(int index, Consumer argAssert, Class paraType) { - methodAsserts.get(currentMethod).add((method, _this, args, _return) -> { + add((method, _this, args, _return) -> { Assertions.assertTrue(args.length > index); Assertions.assertEquals(ClassUtil.primitiveToWrapper(method.getParameterTypes()[index]), ClassUtil.primitiveToWrapper(paraType)); argAssert.accept((R) args[index]); @@ -67,14 +67,21 @@ public T argAssert(int index, Consumer argAssert, Class paraType) { public T args(Object... compareArgs) { - methodAsserts.get(currentMethod).add((method, _this, args, _return) -> Assertions.assertArrayEquals(compareArgs, args)); + add((method, _this, args, _return) -> Assertions.assertArrayEquals(compareArgs, args)); return (T) this; } public T assertReturn(Object compareReturn) { - methodAsserts.get(currentMethod).add((method, _this, args, _return) -> Assertions.assertEquals(compareReturn, _return)); + add((method, _this, args, _return) -> Assertions.assertEquals(compareReturn, _return)); return (T) this; } + protected void add(MethodAssert methodAssert) { + if (currentMethod == null) { + throw new IllegalStateException("method may not called , or it's abstract"); + } + methodAsserts.get(currentMethod).add(methodAssert); + } + } diff --git a/litool-test/src/main/java/io/leaderli/litool/test/LiMock.java b/litool-test/src/main/java/io/leaderli/litool/test/LiMock.java index 5930863f..b1570b6e 100644 --- a/litool-test/src/main/java/io/leaderli/litool/test/LiMock.java +++ b/litool-test/src/main/java/io/leaderli/litool/test/LiMock.java @@ -394,10 +394,14 @@ public static Recorder recorder(Class mockClass) { return new Recorder(mockClass); } - public static RecordBean recordBean(Class mockClass) { + public static RecordBeanInterface, T> recordBean(Class mockClass) { return new RecordBean<>(mockClass); } + @SafeVarargs + public static RecordBeanInterface, T> recordBeans(Class... mockClasses) { + return new RecordBeans<>(mockClasses); + } public static Mocker mocker(Class mockClass) { return new Mocker(mockClass, true); } diff --git a/litool-test/src/main/java/io/leaderli/litool/test/MockBeans.java b/litool-test/src/main/java/io/leaderli/litool/test/MockBeans.java index 6901b59a..7d6ffd23 100644 --- a/litool-test/src/main/java/io/leaderli/litool/test/MockBeans.java +++ b/litool-test/src/main/java/io/leaderli/litool/test/MockBeans.java @@ -22,8 +22,8 @@ public MockBeans(Class... mockClasses) { } @Override - public MockBeans consume(Consumer call) { - mockBeans.forEach(mockBean -> mockBean.consume(call)); + public MockBeans consume(Consumer consumer) { + mockBeans.forEach(mockBean -> mockBean.consume(consumer)); return this; } diff --git a/litool-test/src/main/java/io/leaderli/litool/test/RecordBean.java b/litool-test/src/main/java/io/leaderli/litool/test/RecordBean.java index 1f725bf4..84932370 100644 --- a/litool-test/src/main/java/io/leaderli/litool/test/RecordBean.java +++ b/litool-test/src/main/java/io/leaderli/litool/test/RecordBean.java @@ -8,7 +8,7 @@ import java.util.function.Consumer; import java.util.function.Function; -public class RecordBean extends AbstractRecorder> { +public class RecordBean extends AbstractRecorder> implements RecordBeanInterface, T> { private final T instance; diff --git a/litool-test/src/main/java/io/leaderli/litool/test/RecordBeanInterface.java b/litool-test/src/main/java/io/leaderli/litool/test/RecordBeanInterface.java new file mode 100644 index 00000000..fe365985 --- /dev/null +++ b/litool-test/src/main/java/io/leaderli/litool/test/RecordBeanInterface.java @@ -0,0 +1,22 @@ +package io.leaderli.litool.test; + +import java.util.function.Consumer; +import java.util.function.Function; + +public interface RecordBeanInterface { + + B consume(Consumer call); + + B when(Function call); + + B called(); + + B arg(int index, Object arg); + + B args(Object... args); + + B assertReturn(Object compareReturn); + + T build(); +} + diff --git a/litool-test/src/main/java/io/leaderli/litool/test/RecordBeans.java b/litool-test/src/main/java/io/leaderli/litool/test/RecordBeans.java new file mode 100644 index 00000000..b69bff7b --- /dev/null +++ b/litool-test/src/main/java/io/leaderli/litool/test/RecordBeans.java @@ -0,0 +1,68 @@ +package io.leaderli.litool.test; + +import io.leaderli.litool.core.exception.LiAssertUtil; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.Consumer; +import java.util.function.Function; + +public class RecordBeans implements RecordBeanInterface, T> { + private final List> recordBeans = new ArrayList<>(); + private final List>> recordBeanAction = new ArrayList<>(); + + + @SuppressWarnings("unchecked") + public RecordBeans(Class... mockClasses) { + + for (Class mockClass : mockClasses) { + recordBeans.add(new RecordBean<>((Class) mockClass)); + } + } + + public RecordBeans consume(Consumer consumer) { + recordBeanAction.add(recordbean -> recordbean.consume(consumer)); + return this; + } + + public RecordBeans when(Function function) { + recordBeanAction.add(recordbean -> recordbean.when(function)); + return this; + } + + @Override + public RecordBeans called() { + recordBeanAction.add(AbstractRecorder::called); + return this; + } + + @Override + public RecordBeans arg(int index, Object arg) { + recordBeanAction.add(recordbean -> recordbean.arg(index, arg)); + return this; + } + + @Override + public RecordBeans args(Object... args) { + recordBeanAction.add(recordbean -> recordbean.args(args)); + return this; + } + + @Override + public RecordBeans assertReturn(Object compareReturn) { + recordBeanAction.add(recordbean -> recordbean.assertReturn(compareReturn)); + return this; + } + + public T build() { + T instance = null; + for (RecordBean mockBean : recordBeans) { + recordBeanAction.forEach(action -> action.accept(mockBean)); + instance = mockBean.build(); + } + LiAssertUtil.assertFalse(instance == null, new IllegalArgumentException()); + return instance; + } + + +} diff --git a/litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java b/litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java index 0d45a273..de112443 100644 --- a/litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java +++ b/litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java @@ -249,7 +249,7 @@ void testWhenBean3() { @Test @ExtendWith(SkipWhenJacocoExecutionCondition.class) - void testWhenBeans() { + void testMockerBeans() { Supplier supplier = LiMock.mockerBeans(Supplier1.class, Supplier2.class).other(Supplier::get, (m, a) -> 100).build(); Assertions.assertEquals(100, supplier.get()); @@ -258,6 +258,18 @@ void testWhenBeans() { // } + @Test + @ExtendWith(SkipWhenJacocoExecutionCondition.class) + void testRecordBeans() { + + Supplier supplier = LiMock.mockerBeans(Supplier1.class, Supplier2.class).other(Supplier::get, (m, a) -> 100).build(); + LiMock.recordBeans(Supplier1.class, Supplier2.class).consume(Supplier::get).assertReturn(100).build(); + Assertions.assertEquals(100, supplier.get()); + Assertions.assertEquals(100, new Supplier1().get()); + Assertions.assertEquals(100, new Supplier2().get()); +// + } + @SuppressWarnings({"rawtypes", "unchecked"}) @Test void testWhenInterface() { @@ -420,6 +432,19 @@ void testDelegate() { Assertions.assertEquals(2, new Foo().m1()); } + @Test + void testGenerate() { + + System.out.println(LiMock.getCtClass(Bean2.class).getRefClasses()); + + Bean2 bean2 = LiMock.mockerBean(Bean2.class).when(b -> b.m1(1), "2").build(); + LiMock.recordBean(Bean2.class).when(b -> b.m1(1)).called().build(); + + bean2.m1(1); + + + } + static class Delegate { public static int m1() { return 2;