Skip to content

Instantly share code, notes, and snippets.

@0x6e6562
Created April 24, 2015 17:43
Show Gist options
  • Save 0x6e6562/fc29dca7f3dad98f1084 to your computer and use it in GitHub Desktop.
Save 0x6e6562/fc29dca7f3dad98f1084 to your computer and use it in GitHub Desktop.
package se.qall.middleton.services;
import com.jayway.restassured.RestAssured;
import com.relops.snowflake.Snowflake;
import com.xebialabs.restito.server.StubServer;
import org.glassfish.grizzly.http.Method;
import org.glassfish.grizzly.http.server.Response;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static com.jayway.restassured.RestAssured.expect;
import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp;
import static com.xebialabs.restito.semantics.Action.custom;
import static com.xebialabs.restito.semantics.Action.ok;
import static com.xebialabs.restito.semantics.Condition.*;
import static org.junit.Assert.assertNotEquals;
public class RestitoTest {
private StubServer server;
private Snowflake flake = new Snowflake(77);
@Before
public void start() {
server = new StubServer().run();
RestAssured.port = server.getPort();
}
@After
public void stop() {
server.stop();
}
@Test
public void snowflakesMustBeUnique() {
// Restito
whenHttp(server).
match(get("/flake")).
then(ok(), custom((Response response) -> {
String next = flake.next() + "";
try {
response.getOutputStream().write(next.getBytes());
} catch (IOException e) {
throw new RuntimeException(e);
}
return response;
}));
// Rest-assured
String first = expect().statusCode(200).when().get("/flake").then().extract().body().asString();
String second = expect().statusCode(200).when().get("/flake").then().extract().body().asString();
// JUnit
assertNotEquals(first, second);
// Restito
verifyHttp(server).times(2, method(Method.GET), uri("/flake"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment