-
Notifications
You must be signed in to change notification settings - Fork 26
What's new in 1.3.0
Hi there! Jadler 1.3.0 brings many goodies you don't want to miss:
So, the verification failed. And now what? Jadler 1.3.0
brings a new feature useful for reasoning about the verification failure. The exact reason of the failure is now logged on the INFO
level. Let's say you have the following verification:
verifyThatRequest()
.havingMethodEqualTo("GET")
.havingPathEqualTo("/accounts/1")
.havingBody(isEmptyOrNullString())
.receivedTimes(1);
So basically you expect exactly one GET
request on /accounts/1
with an empty body. But instead of that your test issued just one DELETE
request on /accounts/1
and one POST
request on /accounts
with a nonempty body.
After the verification fails you can find the reason in your test logs on the INFO
level:
[INFO] JadlerMocker.java(454) net.jadler.JadlerMocker: Verification failed, here is a list of requests received so far:
Request #1: {method=DELETE, URI=http://localhost:37709/accounts, parameters=[], headers=[host: localhost:37709, user-agent: Jakarta Commons-HttpClient/3.1], encoding=<none>, body=<empty>}
matching predicates:
body is (null or an empty string)
clashing predicates:
REQUIRED: method is equalToIgnoringCase("GET") BUT was DELETE
REQUIRED: Path is "/accounts/1" BUT was "/accounts"
Request #2: {method=POST, URI=http://localhost:37709/accounts, parameters=[], headers=[content-length: 14, host: localhost:37709, user-agent: Jakarta Commons-HttpClient/3.1], encoding=<none>, body=<nonempty>}
matching predicates: <none>
clashing predicates:
REQUIRED: method is equalToIgnoringCase("GET") BUT was POST
REQUIRED: Path is "/accounts/1" BUT was "/accounts"
REQUIRED: body is (null or an empty string) BUT was "{\"account\":{}}"
And that's it, you can find where the problem is easily now.
The jUnit JadlerRule
class now allows to configure the response defaults (status, encoding, content type, headers) and the requests recording in the same way as in the Jadler facade:
@Rule
public final JadlerRule rule = new JadlerRule()
.withDefaultResponseStatus(200)
.withDefaultResponseEncoding(Charset.forName("UTF-8"))
.withDefaultResponseContentType("text/html; charset=UTF-8")
.withDefaultResponseHeader("X-HEADER1", "value1")
.withDefaultResponseHeader("X-HEADER2", "value2")
.withRequestsRecordingDisabled();
It also allows to set up a specific stub server instance now:
public final JadlerRule rule = new JadlerRule(new JdkStubHttpServer());
Please note the facade now uses the same with*
semantics as the Jadler rule:
initJadler()
.withDefaultResponseStatus(200)
.withDefaultResponseEncoding(Charset.forName("UTF-8"))
.withDefaultResponseContentType("text/html; charset=UTF-8")
.withDefaultResponseHeader("X-HEADER1", "value1")
.withDefaultResponseHeader("X-HEADER2", "value2")
.withRequestsRecordingDisabled();
The old way of initialization using the that()
clause is now deprecated an will be removed in the 2.0.0
release:
initJadler()
.that()
.respondsWithDefaultStatus(200)
.respondsWithDefaultEncoding(Charset.forName("UTF-8"))
.respondsWithDefaultContentType("text/html; charset=UTF-8")
.respondsWithDefaultHeader("X-HEADER1", "value1")
.respondsWithDefaultHeader("X-HEADER2", "value2")
.skipsRequestsRecording();
A reset method was added to the Jadler facade. Please read its javadoc for details and use it as little as possible.
And finally a verification related bug was fixed in 1.3.0
. Two completely identical received requests are counted as one during a verification in 1.2.0
(and 1.2.0
only, no other versions are affected).
So basically this test will fail in 1.2.0
:
@Test
public void twoIdenticalRequests() throws IOException {
final GetMethod method = new GetMethod("http://localhost:" + port() + "/");
this.client.executeMethod(method);
this.client.executeMethod(method);
verifyThatRequest().receivedTimes(2);
}
If you use the 1.2.0
version and do verifications please upgrade to 1.3.0
as soon as possible to avoid mysteriously failing tests.
Enjoy the new Jadler version and feel free to contribute, your help will be much appreciated!