Last active
September 2, 2016 20:16
-
-
Save efenderbosch/bfb8e84f62722215c279ef52db5a7c08 to your computer and use it in GitHub Desktop.
Hamcrest matcher for a Play Future Result status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import akka.util.Timeout; | |
import org.hamcrest.Description; | |
import org.hamcrest.TypeSafeMatcher; | |
import play.api.mvc.Result; | |
import play.api.test.Helpers; | |
import scala.concurrent.Future; | |
import java.util.concurrent.TimeUnit; | |
/** | |
* Created by eric.fenderbosch on 9/2/16. | |
*/ | |
public class ResultStatusMatcher extends TypeSafeMatcher<Future<Result>> { | |
private final int status; | |
private final long wait; | |
private final TimeUnit unit; | |
public ResultStatusMatcher(int status, long wait, TimeUnit unit) { | |
this.status = status; | |
this.wait = wait; | |
this.unit = unit; | |
} | |
@Override | |
protected boolean matchesSafely(Future<Result> result) { | |
return status == Helpers.status(result, Timeout.apply(wait, unit)); | |
} | |
@Override | |
public void describeTo(Description description) { | |
} | |
public static Builder hasStatus(int status) { | |
return new Builder(status); | |
} | |
private static class Builder { | |
private int status; | |
private long wait; | |
public Builder(int status) { | |
this.status = status; | |
} | |
public Builder wait(int wait) { | |
this.wait = wait; | |
return this; | |
} | |
public ResultStatusMatcher seconds() { | |
return new ResultStatusMatcher(status, wait, TimeUnit.SECONDS); | |
} | |
public ResultStatusMatcher millis() { | |
return new ResultStatusMatcher(status, wait, TimeUnit.MILLISECONDS); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment