Skip to content

Commit 5bcc5c7

Browse files
committed
Updated test case to use mocks for testing
1 parent ce3b261 commit 5bcc5c7

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

src/test/java/org/culturegraph/mf/stream/converter/JsonEncoderTest.java

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
*/
1616
package org.culturegraph.mf.stream.converter;
1717

18-
import static org.junit.Assert.assertEquals;
18+
import static org.mockito.Mockito.verify;
1919

20-
import org.culturegraph.mf.framework.DefaultObjectReceiver;
20+
import org.culturegraph.mf.framework.ObjectReceiver;
21+
import org.junit.After;
22+
import org.junit.Before;
2123
import org.junit.Test;
24+
import org.mockito.Mock;
25+
import org.mockito.MockitoAnnotations;
2226

2327
/**
2428
* Test for {@link JsonEncoder}.
@@ -28,48 +32,56 @@
2832
*/
2933
public final class JsonEncoderTest {
3034

31-
private static final String SIMPLE_JSON = "{\"lit1\":\"val1\",\"ent1\":{\"lit2\":\"val2\",\"lit3\":\"val3\"}}";
32-
private static final String LIST_JSON = "{\"list\":[\"1\",\"2\",\"3\"],\"lit\":\"val\"}";
33-
34-
@Test
35-
public void testSimpleJson() {
36-
final JsonEncoder encoder = new JsonEncoder();
37-
encoder.setReceiver(new DefaultObjectReceiver<String>() {
38-
@Override
39-
public void process(final String str) {
40-
assertEquals(SIMPLE_JSON, str);
41-
}
42-
});
35+
private JsonEncoder encoder;
36+
37+
@Mock
38+
private ObjectReceiver<String> receiver;
39+
40+
@Before
41+
public void setup() {
42+
MockitoAnnotations.initMocks(this);
4343

44+
encoder = new JsonEncoder();
45+
encoder.setReceiver(receiver);
46+
}
47+
48+
@After
49+
public void cleanup() {
50+
encoder.closeStream();
51+
}
52+
53+
@Test
54+
public void testShouldEncodeLiteralsAndEntities() {
4455
encoder.startRecord("");
4556
encoder.literal("lit1", "val1");
4657
encoder.startEntity("ent1");
4758
encoder.literal("lit2", "val2");
4859
encoder.literal("lit3", "val3");
4960
encoder.endEntity();
5061
encoder.endRecord();
51-
encoder.closeStream();
62+
63+
verify(receiver).process(fixQuotes("{'lit1':'val1','ent1':{'lit2':'val2','lit3':'val3'}}"));
5264
}
5365

5466
@Test
55-
public void testListJson() {
56-
final JsonEncoder encoder = new JsonEncoder();
57-
encoder.setReceiver(new DefaultObjectReceiver<String>() {
58-
@Override
59-
public void process(final String str) {
60-
assertEquals(LIST_JSON, str);
61-
}
62-
});
63-
67+
public void testShouldEncodeMarkedEntitiesAsList() {
6468
encoder.startRecord("");
6569
encoder.startEntity("list[]");
66-
encoder.literal("", "1");
67-
encoder.literal("", "2");
68-
encoder.literal("", "3");
70+
encoder.literal("a", "1");
71+
encoder.literal("b", "2");
72+
encoder.literal("c", "3");
6973
encoder.endEntity();
70-
encoder.literal("lit", "val");
7174
encoder.endRecord();
72-
encoder.closeStream();
75+
76+
verify(receiver).process(fixQuotes("{'list':['1','2','3']}"));
7377
}
7478

79+
/*
80+
* Utility method which replaces all single quotes in a string with double quotes.
81+
* This allows to specify the JSON output in the test cases without having to wrap
82+
* each bit of text in escaped double quotes.
83+
*/
84+
private String fixQuotes(final String str) {
85+
return str.replace('\'', '"');
86+
}
7587
}

0 commit comments

Comments
 (0)