Skip to content

Instantly share code, notes, and snippets.

View dmcg's full-sized avatar
💭
Splendid

Duncan McGregor dmcg

💭
Splendid
View GitHub Profile
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: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();
import org.junit.Test;
import static org.junit.Assert.*;
public class FakeValueTest {
public abstract class ValueToBeFaked {
public abstract String name();
public abstract String operation(int arg);
public abstract String getProperty();
@dmcg
dmcg / javadiff
Last active August 29, 2015 14:19
Uses javap to see whether there are any semantic differences between Java classes in different classpaths
#!/bin/bash
set -e
# Uses javap to see whether there are any semantic differences between Java classes in different classpaths
classpath1=$1
classpath2=$2
result=0
for class in ${@:3}; do
@dmcg
dmcg / gist:5baf07cd34a2a4721ce3
Created June 25, 2015 21:10
JMock Java8 syntax
public class JMock8Test {
Mockery8 mockery = new Mockery8();
Service service = mockery.mock(Service.class);
public static interface Service {
public int add(int a, int b);
}