From f298362e48186a3a0aa2997cc054ea6d880a5450 Mon Sep 17 00:00:00 2001 From: Ryan Bergman Date: Sun, 10 Dec 2023 13:16:52 -0600 Subject: [PATCH] allow adding the verify to the expects chain --- .../main/java/kong/unirest/core/Expectation.java | 2 ++ .../kong/unirest/core/ExpectedResponseRecord.java | 2 +- .../main/java/kong/unirest/core/Invocation.java | 14 +++++++++++--- .../src/test/java/kong/tests/VerifyTimesTest.java | 13 +++++++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/unirest-mocks/src/main/java/kong/unirest/core/Expectation.java b/unirest-mocks/src/main/java/kong/unirest/core/Expectation.java index 9e2bd248..1e6325ca 100644 --- a/unirest-mocks/src/main/java/kong/unirest/core/Expectation.java +++ b/unirest-mocks/src/main/java/kong/unirest/core/Expectation.java @@ -112,4 +112,6 @@ public interface Expectation { void verify(); void verify(Times times); + + Expectation times(Times never); } diff --git a/unirest-mocks/src/main/java/kong/unirest/core/ExpectedResponseRecord.java b/unirest-mocks/src/main/java/kong/unirest/core/ExpectedResponseRecord.java index 46e83e1f..a4abf5b2 100644 --- a/unirest-mocks/src/main/java/kong/unirest/core/ExpectedResponseRecord.java +++ b/unirest-mocks/src/main/java/kong/unirest/core/ExpectedResponseRecord.java @@ -85,7 +85,7 @@ public ExpectedResponse thenReturn(Supplier supplier){ @Override public void verify() { - verify(Times.atLeastOnce()); + verify(null); } @Override diff --git a/unirest-mocks/src/main/java/kong/unirest/core/Invocation.java b/unirest-mocks/src/main/java/kong/unirest/core/Invocation.java index b2bbafcb..179da157 100644 --- a/unirest-mocks/src/main/java/kong/unirest/core/Invocation.java +++ b/unirest-mocks/src/main/java/kong/unirest/core/Invocation.java @@ -45,6 +45,7 @@ class Invocation implements Expectation { private MatchStatus expectedBodyStatus; private ExpectedResponseRecord expectedResponse = new ExpectedResponseRecord(this); private Function, ExpectedResponse> functionalResponse = r -> expectedResponse; + private Times expectedTimes = Times.atLeastOnce(); public Invocation(Routes routes){ this.routes = routes; @@ -144,19 +145,26 @@ public ExpectedResponse thenReturn() { @Override public void verify() { - verify(Times.atLeastOnce()); + verify(null); } @Override public void verify(Times times) { - Times.EvaluationResult match = times.matches(requests.size()); + var thisTime = times == null ? expectedTimes : times; + var match = thisTime.matches(requests.size()); if(!match.isSuccess()){ throw new UnirestAssertion("%s\n%s", match.getMessage(), details()); } } + @Override + public Expectation times(Times times) { + this.expectedTimes = times; + return this; + } + private String details() { - StringBuilder sb = new StringBuilder(routes.getMethod().name()) + var sb = new StringBuilder(routes.getMethod().name()) .append(" ") .append(routes.getPath()).append(lineSeparator()); if(expectedHeaders.size() > 0){ diff --git a/unirest-mocks/src/test/java/kong/tests/VerifyTimesTest.java b/unirest-mocks/src/test/java/kong/tests/VerifyTimesTest.java index 46c7478a..3093e9f4 100644 --- a/unirest-mocks/src/test/java/kong/tests/VerifyTimesTest.java +++ b/unirest-mocks/src/test/java/kong/tests/VerifyTimesTest.java @@ -134,4 +134,17 @@ void verifyAtMost() { Unirest.get(path).asEmpty(); } + + @Test + void buildExpectationTimesIntoChain() { + var expect = client.expect(GET, path).times(Times.never()).thenReturn("hi"); + + assertDoesNotThrow(() -> expect.verify()); + + Unirest.get(path).asEmpty(); + + var ex = assertThrows(UnirestAssertion.class, () -> expect.verify()); + assertEquals("Expected exactly 0 invocations but got 1\n" + + "GET http://basic\n", ex.getMessage()); + } }