Created
March 4, 2015 21:35
-
-
Save dmcg/e0c8bd293177055f87bd to your computer and use it in GitHub Desktop.
This file contains 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
String user = "{" + | |
"isAccountNonLocked : true," + | |
"isEnabled : true," + | |
"isAccountNonExpired : true," + | |
"isPasswordProvided : true," + | |
"isCredentialsNonExpired : true," + | |
"}"; | |
mockery.checking(new JsonExpectations(user)); | |
public class JsonExpectations implements ExpectationBuilder { | |
private final JSONObject json; | |
public JsonExpectations(JSONObject json) { | |
this.json = json; | |
} | |
public JsonExpectations(String json) { | |
this(parse(json)); | |
} | |
@Override | |
public void buildExpectations(Action action, ExpectationCollector expectationCollector) { | |
try { | |
for (int i = 0; i < json.length(); i++) { | |
String name = json.names().getString(i); | |
Object value = json.get(name); | |
expectationCollector.add(expecationBuilderFor(name, value).toExpectation(action)); | |
} | |
} catch (JSONException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private InvocationExpectationBuilder expecationBuilderFor(String name, Object value) { | |
InvocationExpectationBuilder builder = new InvocationExpectationBuilder(); | |
builder.method(name); | |
builder.setAction(new ReturnValueAction(value)); | |
return builder; | |
} | |
private static JSONObject parse(String user) { | |
try { | |
return new JSONObject(user); | |
} catch (JSONException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment