Skip to content

Commit

Permalink
add assertDoesNotThrow in after litest
Browse files Browse the repository at this point in the history
  • Loading branch information
leaderli committed Mar 4, 2024
1 parent 8d8eec4 commit bdec99f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class AbstractRecorder<T> {

public static final Set<Method> recordMethodCall = new HashSet<>();
public static final Set<Method> actualMethodCall = new HashSet<>();
public static final List<Throwable> assertThrow = new ArrayList<>();
protected final Class<?> mockClass;
protected final Map<Method, List<MethodAssert>> methodAsserts = new HashMap<>();
private Method currentMethod;
Expand Down
7 changes: 7 additions & 0 deletions litool-test/src/main/java/io/leaderli/litool/test/LiMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ public static void assertMethodCalled() {
}
}

public static void assertDoesNotThrow() {
for (Throwable throwable : Recorder.assertThrow) {
throw new RuntimeException(throwable);
}
}

/**
* 情况方法调用记录
*
Expand All @@ -313,6 +319,7 @@ public static void clearMethodCalled() {
}

public static void reset() {
Recorder.assertThrow.clear();
originClasses.forEach((k, v) -> {
try {
detach(k);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
public class LiMockAfterEachCallback implements AfterEachCallback {
@Override
public void afterEach(ExtensionContext context) {
LiMock.reset();
LiMock.assertMethodCalled();
LiMock.assertDoesNotThrow();
LiMock.reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -572,4 +595,10 @@ public Integer get() {
return 2;
}
}

static class Lira1 {
int m1(int a) {
return a;
}
}
}

0 comments on commit bdec99f

Please sign in to comment.