Skip to content

Instantly share code, notes, and snippets.

View dmcg's full-sized avatar
💭
Splendid

Duncan McGregor dmcg

💭
Splendid
View GitHub Profile
@dmcg
dmcg / Examples.scala
Created June 10, 2011 14:22
GivenWhenThenFeatureSpec
class LoginFeature extends GivenWhenThenFeatureSpec with ShouldMatchers with OneInstancePerTest {
feature("Admin Login") {
scenario("Incorrect password") {
// You can provide the code for a step in a block
given("user visits", the[AdminHomePage]) {
startPage(pageClass)
}
@dmcg
dmcg / junit-quickcheck
Last active December 19, 2015 03:29
Maven build of junit-quickcheck
$ git clone [email protected]:dmcg/junit-quickcheck.git
Cloning into 'junit-quickcheck'...
remote: Counting objects: 4804, done.
remote: Compressing objects: 100% (1269/1269), done.
remote: Total 4804 (delta 2245), reused 4769 (delta 2215)
Receiving objects: 100% (4804/4804), 779.89 KiB | 452 KiB/s, done.
Resolving deltas: 100% (2245/2245), done.
~/Documents/Work/approvals
$ cd junit-quickcheck/
~/Documents/Work/approvals/junit-quickcheck
mockery.when(() -> servlet.process(
ImmutableMap.of(
"code", new String[]{ codeValue},
"state", new String[]{ state }),
"http://rescyoume/oauth",
response)).
then(expect -> {
expect.oneOf(stravaAuthenticator).fetchDetails(codeValue, oAuthAccount, clientState, "http://rescyoume/oauth");
expect.will(returnValue(oAuthDetails));
expect.oneOf(db).save(oAuthDetails);
@dmcg
dmcg / gist:74628586fa79b3a3350f
Last active June 21, 2019 22:19
Nasty hack Hamcrest Stream matcher
public class StreamMatchers {
public static class StreamContainingInAnyOrderMatcher<T> extends DelegatingStreamMatcher<T> {
public StreamContainingInAnyOrderMatcher(T... items) {
super(org.hamcrest.Matchers.arrayContainingInAnyOrder(items), "Stream of ");
}
}
public static class EmptyStreamMatcher<T> extends DelegatingStreamMatcher<T> {
public EmptyStreamMatcher() {
super(Matchers.emptyArray(), "Stream like ");
@dmcg
dmcg / gist:8ddf275688fd450e977e
Last active August 29, 2015 14:10
Hamcrest Matcher transformed by function
public class TransformingMatcher<U, T> extends TypeSafeMatcher<U> {
private final Matcher<T> base;
private final Function<? super U, ? extends T> function;
public TransformingMatcher(Matcher<T> base, Function<? super U, ? extends T> function) {
this.base = base;
this.function = function;
}
@Override
@dmcg
dmcg / gist:9270a42c3e078549ca24
Created November 22, 2014 21:32
Functional test data builder with lambdas and Lombok @wither
@Value public class User {
public final Optional<String> id;
public final DateTime createdDate;
public final String firstName;
public final String lastName;
@Wither public final Country country;
@Wither public final PhoneNumber phoneNumber;
@Wither public final String email;
@Wither public final Optional<String> stravaId;
@dmcg
dmcg / gist:57527540ceb01a455fcf
Last active August 29, 2015 14:10
JMock, Hamcrest, Guava and Java8 lambdas collide in expressive ways
@Test
public void saves_results() throws IOException {
ImmutableList<Associate> associates = ImmutableList.of(
new Associate("id1", "fn1", "ln2"),
new Associate("id2", "fn1", "ln2"));
mockery.given((given) -> {
given.oneOf(source).findAssociates("token");
given.will(returnValue(associates.stream()));
});
String user = "{" +
"isAccountNonLocked : true," +
"isEnabled : true," +
"isAccountNonExpired : true," +
"isPasswordProvided : true," +
"isCredentialsNonExpired : true," +
"}";
mockery.checking(new JsonExpectations(user));
@dmcg
dmcg / gist:2be1699a04a5c3784298
Created March 4, 2015 22:21
ValueExpectations
private User activeUser(Customer customer) {
return mock(User.class,
"isAccountNonLocked", true,
"isEnabled", true,
"isAccountNonExpired", true,
"isPasswordProvided", true,
"isCredentialsNonExpired", true,
"getCustomer", customer,
"getAuthorities", ImmutableList.of()
);
@dmcg
dmcg / gist:89aa33d535794464b5df
Last active August 29, 2015 14:16
ValueExpectations2
private User activeUser(final Customer customer) {
return mock(User.class,
new Object() {
boolean isAccountNonLocked = true;
boolean isEnabled = true;
boolean isAccountNonExpired = true;
boolean isPasswordProvided = true;
boolean isCredentialsNonExpired = true;
Customer getCustomer = customer;
ImmutableList<?> getAuthorities = ImmutableList.of();