Skip to content

Commit 104770c

Browse files
committed
[issue 49] EasyMock -> Mockito
Upgrade to Mockito for a nicer mock API.
1 parent a079d09 commit 104770c

16 files changed

Lines changed: 112 additions & 139 deletions

File tree

jsonschema2pojo-cli/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@
127127
<artifactId>junit</artifactId>
128128
</dependency>
129129
<dependency>
130-
<groupId>org.easymock</groupId>
131-
<artifactId>easymock</artifactId>
130+
<groupId>org.mockito</groupId>
131+
<artifactId>mockito-all</artifactId>
132132
</dependency>
133133
</dependencies>
134134

jsonschema2pojo-cli/src/test/java/com/googlecode/jsonschema2pojo/cli/ArgumentsTest.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616

1717
package com.googlecode.jsonschema2pojo.cli;
1818

19-
import static org.hamcrest.MatcherAssert.*;
20-
import static org.hamcrest.Matchers.*;
19+
import static org.hamcrest.MatcherAssert.assertThat;
20+
import static org.hamcrest.Matchers.containsString;
21+
import static org.hamcrest.Matchers.is;
22+
import static org.hamcrest.Matchers.notNullValue;
23+
import static org.hamcrest.Matchers.nullValue;
2124

2225
import java.io.ByteArrayOutputStream;
2326
import java.io.IOException;
2427
import java.io.PrintStream;
2528

26-
import org.easymock.Capture;
2729
import org.junit.After;
2830
import org.junit.Before;
2931
import org.junit.Test;
@@ -53,7 +55,7 @@ public void parseRecognisesValidArguments() {
5355
"--source", "/home/source", "--target", "/home/target", "--package", "mypackage", "--generate-builders", "--use-primitives"
5456
});
5557

56-
assertThat(args.getStatus().hasCaptured(), is(false));
58+
assertThat(args.didExit(), is(false));
5759
assertThat(args.getSource().getAbsolutePath(), is("/home/source"));
5860
assertThat(args.getTargetDirectory().getAbsolutePath(), is("/home/target"));
5961
assertThat(args.getTargetPackage(), is("mypackage"));
@@ -67,7 +69,7 @@ public void parseRecognisesShorthandArguments() {
6769
"-s", "/home/source", "-t", "/home/target", "-p", "mypackage", "-b", "-P"
6870
});
6971

70-
assertThat(args.getStatus().hasCaptured(), is(false));
72+
assertThat(args.didExit(), is(false));
7173
assertThat(args.getSource().getAbsolutePath(), is("/home/source"));
7274
assertThat(args.getTargetDirectory().getAbsolutePath(), is("/home/target"));
7375
assertThat(args.getTargetPackage(), is("mypackage"));
@@ -91,7 +93,7 @@ public void packageIsOptional() {
9193
"--source", "/home/source", "--target", "/home/target"
9294
});
9395

94-
assertThat(args.getStatus().hasCaptured(), is(false));
96+
assertThat(args.didExit(), is(false));
9597
assertThat(args.getSource().getAbsolutePath(), is("/home/source"));
9698
assertThat(args.getTargetDirectory().getAbsolutePath(), is("/home/target"));
9799
assertThat(args.getTargetPackage(), is(nullValue()));
@@ -103,8 +105,7 @@ public void packageIsOptional() {
103105
public void missingArgsCausesHelp() throws IOException {
104106
ArgsForTest args = (ArgsForTest) new ArgsForTest().parse(new String[] {});
105107

106-
assertThat(args.getStatus().hasCaptured(), is(true));
107-
assertThat(args.getStatus().getValue(), is(1));
108+
assertThat(args.status, is(1));
108109
assertThat(new String(systemErrCapture.toByteArray(), "UTF-8"), is(containsString("--target")));
109110
assertThat(new String(systemErrCapture.toByteArray(), "UTF-8"), is(containsString("--source")));
110111
assertThat(new String(systemOutCapture.toByteArray(), "UTF-8"), is(containsString("Usage: jsonschema2pojo")));
@@ -114,22 +115,21 @@ public void missingArgsCausesHelp() throws IOException {
114115
public void requestingHelpCausesHelp() throws IOException {
115116
ArgsForTest args = (ArgsForTest) new ArgsForTest().parse(new String[] {"--help"});
116117

117-
assertThat(args.getStatus().hasCaptured(), is(true));
118+
assertThat(args.status, is(notNullValue()));
118119
assertThat(new String(systemOutCapture.toByteArray(), "UTF-8"), is(containsString("Usage: jsonschema2pojo")));
119120
}
120121

121122
private static class ArgsForTest extends Arguments {
122-
private Capture<Integer> status = new Capture<Integer>();
123+
protected Integer status;
123124

124125
@Override
125126
protected void exit(int status) {
126-
this.status.setValue(status);
127+
this.status = status;
127128
}
128-
129-
public Capture<Integer> getStatus() {
130-
return status;
129+
130+
protected boolean didExit() {
131+
return (status != null);
131132
}
132-
133133
}
134134

135135
}

jsonschema2pojo-core/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
<artifactId>hamcrest-all</artifactId>
4444
</dependency>
4545
<dependency>
46-
<groupId>org.easymock</groupId>
47-
<artifactId>easymock</artifactId>
46+
<groupId>org.mockito</groupId>
47+
<artifactId>mockito-all</artifactId>
4848
</dependency>
4949
</dependencies>
5050

jsonschema2pojo-core/src/test/java/com/googlecode/jsonschema2pojo/SchemaMapperImplTest.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
package com.googlecode.jsonschema2pojo;
1818

19-
import static org.easymock.EasyMock.*;
19+
import static org.mockito.Mockito.*;
2020
import static org.hamcrest.MatcherAssert.*;
2121
import static org.hamcrest.Matchers.*;
2222

2323
import java.io.IOException;
2424
import java.net.URL;
2525

26-
import org.easymock.Capture;
2726
import org.junit.Test;
27+
import org.mockito.ArgumentCaptor;
2828

2929
import com.fasterxml.jackson.databind.JsonNode;
3030
import com.googlecode.jsonschema2pojo.rules.JsonSchemaRule;
@@ -41,7 +41,7 @@ public class SchemaMapperImplTest {
4141
@Test
4242
public void generateReadsSchemaAsObject() throws IOException {
4343

44-
final JsonSchemaRule mockSchemaRule = createMock(JsonSchemaRule.class);
44+
final JsonSchemaRule mockSchemaRule = mock(JsonSchemaRule.class);
4545

4646
final RuleFactory ruleFactory = new RuleFactoryImpl(null) {
4747
@Override
@@ -50,22 +50,18 @@ public SchemaRule<JClassContainer, JType> getSchemaRule() {
5050
}
5151
};
5252

53-
Capture<JPackage> capturePackage = new Capture<JPackage>();
54-
Capture<JsonNode> captureNode = new Capture<JsonNode>();
55-
56-
expect(mockSchemaRule.apply(eq("Address"), capture(captureNode), capture(capturePackage), isNull(Schema.class))).andReturn(null);
5753

5854
URL schemaContent = this.getClass().getResource("/schema/address.json");
5955

60-
replay(mockSchemaRule);
61-
6256
new SchemaMapperImpl(ruleFactory).generate(new JCodeModel(), "Address", "com.example.package", schemaContent);
6357

64-
verify(mockSchemaRule);
58+
ArgumentCaptor<JPackage> capturePackage = ArgumentCaptor.forClass(JPackage.class);
59+
ArgumentCaptor<JsonNode> captureNode = ArgumentCaptor.forClass(JsonNode.class);
6560

66-
assertThat(capturePackage.hasCaptured(), is(true));
61+
verify(mockSchemaRule).apply(eq("Address"), captureNode.capture(), capturePackage.capture(), isNull(Schema.class));
62+
6763
assertThat(capturePackage.getValue().name(), is("com.example.package"));
68-
assertThat(captureNode.hasCaptured(), is(true));
64+
assertThat(captureNode.getValue(), is(notNullValue()));
6965

7066
}
7167

jsonschema2pojo-core/src/test/java/com/googlecode/jsonschema2pojo/SchemaTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616

1717
package com.googlecode.jsonschema2pojo;
1818

19-
import static org.apache.commons.lang.StringUtils.*;
20-
import static org.easymock.EasyMock.*;
21-
import static org.hamcrest.Matchers.*;
22-
import static org.junit.Assert.*;
19+
import static org.apache.commons.lang.StringUtils.removeEnd;
20+
import static org.hamcrest.Matchers.equalTo;
21+
import static org.hamcrest.Matchers.is;
22+
import static org.hamcrest.Matchers.not;
23+
import static org.hamcrest.Matchers.notNullValue;
24+
import static org.hamcrest.Matchers.sameInstance;
25+
import static org.junit.Assert.assertThat;
26+
import static org.mockito.Mockito.mock;
2327

2428
import java.net.URI;
2529
import java.net.URISyntaxException;
@@ -29,6 +33,7 @@
2933

3034
import com.sun.codemodel.JDefinedClass;
3135
import com.sun.codemodel.JType;
36+
3237
public class SchemaTest {
3338

3439
@Before
@@ -111,8 +116,8 @@ public void schemaAlreadyReadIsReused() throws URISyntaxException {
111116
@Test
112117
public void setIfEmptyOnlySetsIfEmpty() throws URISyntaxException {
113118

114-
JType firstClass = createMock(JDefinedClass.class);
115-
JType secondClass = createMock(JDefinedClass.class);
119+
JType firstClass = mock(JDefinedClass.class);
120+
JType secondClass = mock(JDefinedClass.class);
116121

117122
URI schemaUri = getClass().getResource("/schema/address.json").toURI();
118123

jsonschema2pojo-core/src/test/java/com/googlecode/jsonschema2pojo/rules/AdditionalPropertiesRuleTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.googlecode.jsonschema2pojo.rules;
1818

19-
import static org.easymock.EasyMock.*;
19+
import static org.mockito.Mockito.*;
2020
import static org.hamcrest.MatcherAssert.*;
2121
import static org.hamcrest.Matchers.*;
2222

@@ -120,7 +120,7 @@ public void applyWithAdditionalPropertiesStringSchema() throws JClassAlreadyExis
120120
ObjectNode node = new ObjectMapper().createObjectNode();
121121
node.put("type", "string");
122122

123-
JDefinedClass result = rule.apply("node", node, jclass, createMock(Schema.class));
123+
JDefinedClass result = rule.apply("node", node, jclass, mock(Schema.class));
124124

125125
StringWriter output = new StringWriter();
126126
result.declare(new JFormatter(output));
@@ -135,7 +135,7 @@ public void applyWithAdditionalPropertiesObjectSchema() throws JClassAlreadyExis
135135
ObjectNode node = new ObjectMapper().createObjectNode();
136136
node.put("type", "object");
137137

138-
JDefinedClass result = rule.apply("node", node, jclass, createMock(Schema.class));
138+
JDefinedClass result = rule.apply("node", node, jclass, mock(Schema.class));
139139

140140
StringWriter output = new StringWriter();
141141
result.declare(new JFormatter(output));

jsonschema2pojo-core/src/test/java/com/googlecode/jsonschema2pojo/rules/ArrayRuleTest.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.googlecode.jsonschema2pojo.rules;
1818

19-
import static org.easymock.EasyMock.*;
19+
import static org.mockito.Mockito.*;
2020
import static org.hamcrest.MatcherAssert.*;
2121
import static org.hamcrest.Matchers.*;
2222

@@ -37,7 +37,7 @@
3737

3838
public class ArrayRuleTest {
3939

40-
private final GenerationConfig config = createNiceMock(GenerationConfig.class);
40+
private final GenerationConfig config = mock(GenerationConfig.class);
4141
private final ArrayRule rule = new ArrayRule(new RuleFactoryImpl(config));
4242

4343
@Before
@@ -59,9 +59,7 @@ public void arrayWithUniqueItemsProducesSet() {
5959
propertyNode.put("uniqueItems", true);
6060
propertyNode.put("items", itemsNode);
6161

62-
replay(config);
63-
64-
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, createMock(Schema.class));
62+
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, mock(Schema.class));
6563

6664
assertThat(propertyType, notNullValue());
6765
assertThat(propertyType.erasure(), is(codeModel.ref(Set.class)));
@@ -82,9 +80,8 @@ public void arrayWithNonUniqueItemsProducesList() {
8280
propertyNode.put("uniqueItems", false);
8381
propertyNode.put("items", itemsNode);
8482

85-
Schema schema = createNiceMock(Schema.class);
86-
expect(schema.getId()).andReturn(URI.create("http://example/nonUniqueArray")).anyTimes();
87-
replay(schema);
83+
Schema schema = mock(Schema.class);
84+
when(schema.getId()).thenReturn(URI.create("http://example/nonUniqueArray"));
8885

8986
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
9087

@@ -107,10 +104,9 @@ public void arrayOfPrimitivesProducesCollectionOfWrapperTypes() {
107104
propertyNode.put("uniqueItems", false);
108105
propertyNode.put("items", itemsNode);
109106

110-
Schema schema = createNiceMock(Schema.class);
111-
expect(schema.getId()).andReturn(URI.create("http://example/nonUniqueArray")).anyTimes();
112-
expect(config.isUsePrimitives()).andReturn(true).anyTimes();
113-
replay(schema, config);
107+
Schema schema = mock(Schema.class);
108+
when(schema.getId()).thenReturn(URI.create("http://example/nonUniqueArray"));
109+
when(config.isUsePrimitives()).thenReturn(true);
114110

115111
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
116112

@@ -133,9 +129,8 @@ public void arrayDefaultsToNonUnique() {
133129
propertyNode.put("uniqueItems", false);
134130
propertyNode.put("items", itemsNode);
135131

136-
Schema schema = createNiceMock(Schema.class);
137-
expect(schema.getId()).andReturn(URI.create("http://example/defaultArray")).anyTimes();
138-
replay(schema);
132+
Schema schema = mock(Schema.class);
133+
when(schema.getId()).thenReturn(URI.create("http://example/defaultArray"));
139134

140135
JClass propertyType = rule.apply("fooBars", propertyNode, jpackage, schema);
141136

jsonschema2pojo-core/src/test/java/com/googlecode/jsonschema2pojo/rules/EnumRuleTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.googlecode.jsonschema2pojo.rules;
1818

19-
import static org.easymock.EasyMock.*;
19+
import static org.mockito.Mockito.*;
2020
import static org.hamcrest.MatcherAssert.*;
2121
import static org.hamcrest.Matchers.*;
2222

@@ -135,7 +135,7 @@ public void applyFailsWhenEnumAlreadyExists() throws JClassAlreadyExistsExceptio
135135

136136
jclass._enum("ExistingEnum");
137137

138-
rule.apply("existingEnum", new ObjectMapper().createObjectNode(), jclass, createNiceMock(Schema.class));
138+
rule.apply("existingEnum", new ObjectMapper().createObjectNode(), jclass, mock(Schema.class));
139139
}
140140

141141
@Test
@@ -149,7 +149,7 @@ public void applyCreatesTextEnum() throws JClassAlreadyExistsException {
149149
enumNode.add("valueTwo");
150150
enumNode.add("valueThree");
151151

152-
rule.apply("newEnum", enumNode, jclass, createNiceMock(Schema.class));
152+
rule.apply("newEnum", enumNode, jclass, mock(Schema.class));
153153

154154
StringWriter output = new StringWriter();
155155
jclass.declare(new JFormatter(output));
@@ -169,7 +169,7 @@ public void applyCreatesTextEnumWithSpaces() throws JClassAlreadyExistsException
169169
enumNode.add("value two");
170170
enumNode.add("value three");
171171

172-
rule.apply("newEnum", enumNode, jclass, createNiceMock(Schema.class));
172+
rule.apply("newEnum", enumNode, jclass, mock(Schema.class));
173173

174174
StringWriter output = new StringWriter();
175175
jclass.declare(new JFormatter(output));
@@ -188,7 +188,7 @@ public void applyCreatesNumberEnum() throws JClassAlreadyExistsException {
188188
enumNode.add("200");
189189
enumNode.add("300");
190190

191-
rule.apply("newEnum", enumNode, jclass, createNiceMock(Schema.class));
191+
rule.apply("newEnum", enumNode, jclass, mock(Schema.class));
192192

193193
StringWriter output = new StringWriter();
194194
jclass.declare(new JFormatter(output));

jsonschema2pojo-core/src/test/java/com/googlecode/jsonschema2pojo/rules/FormatRuleTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.googlecode.jsonschema2pojo.rules;
1818

1919
import static java.util.Arrays.*;
20-
import static org.easymock.EasyMock.*;
20+
import static org.mockito.Mockito.*;
2121
import static org.hamcrest.Matchers.*;
2222
import static org.junit.Assert.*;
2323

@@ -39,7 +39,7 @@
3939
@RunWith(Parameterized.class)
4040
public class FormatRuleTest {
4141

42-
private GenerationConfig config = createMock(GenerationConfig.class);
42+
private GenerationConfig config = mock(GenerationConfig.class);
4343
private FormatRule rule = new FormatRule(new RuleFactoryImpl(config));
4444

4545
private final String formatValue;

0 commit comments

Comments
 (0)