Skip to content

Commit 02a66fb

Browse files
Add LockAssert.TestHelper.makeAllAssertsPass
1 parent f5e3093 commit 02a66fb

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

shedlock-core/src/main/java/net/javacrumbs/shedlock/core/LockAssert.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,22 @@ public static void assertLocked() {
4747
throw new IllegalStateException("The task is not locked.");
4848
}
4949
}
50+
51+
static class TestHelper {
52+
/**
53+
* If pass is set to true, all LockAssert.assertLocked calls in current thread will pass.
54+
* To be used in unit tests only
55+
*
56+
* <code>
57+
* LockAssert.TestHelper.makeAllAssertsPass(true)
58+
* </code>
59+
*/
60+
public static void makeAllAssertsPass(boolean pass) {
61+
if (pass) {
62+
LockAssert.startLock("net.javacrumbs.shedlock.core.test-lock");
63+
} else {
64+
LockAssert.endLock();
65+
}
66+
}
67+
}
5068
}

shedlock-core/src/test/java/net/javacrumbs/shedlock/core/LockAssertTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.junit.jupiter.api.Test;
44

5-
import java.time.Instant;
65
import java.util.Optional;
76

87
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@@ -29,4 +28,12 @@ void assertLockedShouldNotFailIfLockHeld() {
2928
);
3029
}
3130

31+
@Test
32+
void assertShouldNotFailIfConfiguredForTests() {
33+
LockAssert.TestHelper.makeAllAssertsPass(true);
34+
LockAssert.assertLocked();
35+
36+
LockAssert.TestHelper.makeAllAssertsPass(false);
37+
assertThatThrownBy(LockAssert::assertLocked).isInstanceOf(IllegalStateException.class);
38+
}
3239
}

0 commit comments

Comments
 (0)