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 4765e4c6..2b34bc44 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 @@ -15,6 +15,7 @@ public class AbstractRecorder { public static final Set recordMethodCall = new HashSet<>(); public static final Set actualMethodCall = new HashSet<>(); + public static final List assertThrow = new ArrayList<>(); protected final Class mockClass; protected final Map> methodAsserts = new HashMap<>(); private Method currentMethod; 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 faa3be77..2dba1908 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 @@ -303,6 +303,12 @@ public static void assertMethodCalled() { } } + public static void assertDoesNotThrow() { + for (Throwable throwable : Recorder.assertThrow) { + throw new RuntimeException(throwable); + } + } + /** * 情况方法调用记录 * @@ -313,6 +319,7 @@ public static void clearMethodCalled() { } public static void reset() { + Recorder.assertThrow.clear(); originClasses.forEach((k, v) -> { try { detach(k); diff --git a/litool-test/src/main/java/io/leaderli/litool/test/LiMockAfterEachCallback.java b/litool-test/src/main/java/io/leaderli/litool/test/LiMockAfterEachCallback.java index 839527b6..9554841a 100644 --- a/litool-test/src/main/java/io/leaderli/litool/test/LiMockAfterEachCallback.java +++ b/litool-test/src/main/java/io/leaderli/litool/test/LiMockAfterEachCallback.java @@ -6,7 +6,8 @@ public class LiMockAfterEachCallback implements AfterEachCallback { @Override public void afterEach(ExtensionContext context) { - LiMock.reset(); LiMock.assertMethodCalled(); + LiMock.assertDoesNotThrow(); + LiMock.reset(); } } diff --git a/litool-test/src/main/java/io/leaderli/litool/test/MockMethodInvoker.java b/litool-test/src/main/java/io/leaderli/litool/test/MockMethodInvoker.java index 86d492fc..590848c1 100644 --- a/litool-test/src/main/java/io/leaderli/litool/test/MockMethodInvoker.java +++ b/litool-test/src/main/java/io/leaderli/litool/test/MockMethodInvoker.java @@ -34,7 +34,12 @@ public static void record(String uuid, Object _this, Object[] args, Object _retu if (tuple == null) { return; } - tuple._1.apply(tuple._2, _this, args, _return); + try { + tuple._1.apply(tuple._2, _this, args, _return); + } catch (Throwable throwable) { + Recorder.assertThrow.add(throwable); + throw throwable; + } } public static Object zero(Class returnClass) { 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 8106eca4..6be20d19 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 @@ -1,7 +1,9 @@ package io.leaderli.litool.test; +import io.leaderli.litool.core.io.IOUtils; import io.leaderli.litool.core.meta.Either; import io.leaderli.litool.core.meta.LiBox; +import io.leaderli.litool.core.meta.Lira; import io.leaderli.litool.core.type.LiTypeToken; import io.leaderli.litool.core.type.MethodFilter; import org.junit.jupiter.api.Assertions; @@ -391,6 +393,27 @@ void testRecord() { } + + private static void liraMap(int a) { + + } + + @LiTest + void testRecordInLira() { + + System.setErr(IOUtils.emptyPrintStream()); + Lira1 lira1 = new Lira1(); + Assertions.assertDoesNotThrow(() -> Lira.of(1, 2, 3).mapIgnoreError(lira1::m1).get()); + LiMock.recorder(Lira1.class).when(lira1.m1(1)).assertReturn(100).build(); + Assertions.assertDoesNotThrow(() -> { + Lira.of(1, 2, 3).mapIgnoreError(lira1::m1).get(); + }); + + Assertions.assertThrows(Throwable.class, LiMock::assertDoesNotThrow); + Recorder.assertThrow.clear(); + + } + @SuppressWarnings("ConstantValue") static class Error { static { @@ -572,4 +595,10 @@ public Integer get() { return 2; } } + + static class Lira1 { + int m1(int a) { + return a; + } + } }