Skip to content

Commit 9896a26

Browse files
rhowespullara
authored andcommitted
Update tests for jUnit 4.x
Introduce use of assertThrows() from jUnit 4.x Clean up exception declarations in tests
1 parent 01054a7 commit 9896a26

10 files changed

+234
-248
lines changed

compiler/src/test/java/com/github/mustachejava/AbstractClassTest.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import org.junit.Test;
44

55
import java.io.IOException;
6-
import java.io.OutputStreamWriter;
76
import java.io.StringReader;
7+
import java.io.StringWriter;
88
import java.io.Writer;
99
import java.util.ArrayList;
1010
import java.util.HashMap;
1111
import java.util.List;
1212

13+
import static org.junit.Assert.assertEquals;
14+
1315
public class AbstractClassTest {
1416
static abstract class AbstractFoo {
1517
public abstract String getValue();
@@ -42,12 +44,13 @@ public void testAbstractClass() throws IOException {
4244
containers.add(new Container(new Foo()));
4345
containers.add(new Container(new Bar()));
4446
HashMap<String, Object> scopes = new HashMap<>();
45-
Writer writer = new OutputStreamWriter(System.out);
47+
Writer writer = new StringWriter();
4648
MustacheFactory mf = new DefaultMustacheFactory();
4749
Mustache mustache = mf.compile(new StringReader("{{#containers}} {{foo.value}} {{/containers}}"), "example");
4850
scopes.put("containers", containers);
4951
mustache.execute(writer, scopes);
5052
writer.flush();
53+
assertEquals(" I am Foo I am Bar ", writer.toString());
5154
}
5255

5356
@Test
@@ -56,11 +59,12 @@ public void testAbstractClassNoDots() throws IOException {
5659
containers.add(new Container(new Foo()));
5760
containers.add(new Container(new Bar()));
5861
HashMap<String, Object> scopes = new HashMap<>();
59-
Writer writer = new OutputStreamWriter(System.out);
62+
Writer writer = new StringWriter();
6063
MustacheFactory mf = new DefaultMustacheFactory();
6164
Mustache mustache = mf.compile(new StringReader("{{#containers}} {{#foo}}{{value}}{{/foo}} {{/containers}}"), "example");
6265
scopes.put("containers", containers);
6366
mustache.execute(writer, scopes);
6467
writer.flush();
68+
assertEquals(" I am Foo I am Bar ", writer.toString());
6569
}
6670
}

compiler/src/test/java/com/github/mustachejava/ArraysIndexesTest.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import java.util.Set;
1313

1414
import static org.junit.Assert.assertEquals;
15-
import static org.junit.Assert.fail;
15+
import static org.junit.Assert.assertThrows;
1616

1717
/**
1818
* Shows a simple way to add indexes for arrays.
@@ -66,8 +66,8 @@ public Object coerce(final Object object) {
6666
}
6767

6868
@Test
69-
public void testThrowMustacheExceptionInCaseOfNullHandler() throws Exception {
70-
try {
69+
public void testThrowMustacheExceptionInCaseOfNullHandler() {
70+
MustacheException expected = assertThrows(MustacheException.class, () -> {
7171
String template = "<ol>\n" +
7272
" <li>{{test.1}}</li>\n" +
7373
" <li>{{test.0}}</li>\n" +
@@ -79,16 +79,14 @@ public void testThrowMustacheExceptionInCaseOfNullHandler() throws Exception {
7979
"{{/test}}\n" +
8080
"</ol>";
8181
Object scope = new Object() {
82-
String[] test = new String[]{ "a", "b", "c", "d" };
82+
String[] test = new String[]{"a", "b", "c", "d"};
8383
};
8484
DefaultMustacheFactory mf = new DefaultMustacheFactory();
8585
mf.setObjectHandler(null);
8686
Mustache m = mf.compile(new StringReader(template), "template");
8787
m.execute(new StringWriter(), scope).flush();
88-
fail("should have thrown MustacheException");
89-
} catch (MustacheException expected) {
90-
assertEquals("Failed to get value for test.1 @[template:2]", expected.getMessage());
91-
}
88+
});
89+
assertEquals("Failed to get value for test.1 @[template:2]", expected.getMessage());
9290
}
9391

9492
private static class ArrayMap extends AbstractMap<Object, Object> implements Iterable<Object> {

compiler/src/test/java/com/github/mustachejava/ConcurrencyTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import java.util.concurrent.atomic.AtomicInteger;
1515

1616
import static com.github.mustachejavabenchmarks.BenchmarkTest.skip;
17-
import static junit.framework.Assert.assertEquals;
17+
import static org.junit.Assert.assertEquals;
1818

1919
/**
2020
* Inspired by an unconfirmed bug report.

compiler/src/test/java/com/github/mustachejava/ConvertMethodsToFunctionsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import java.util.List;
1919
import java.util.function.Function;
2020

21-
import static junit.framework.Assert.assertEquals;
21+
import static org.junit.Assert.assertEquals;
2222

2323
public class ConvertMethodsToFunctionsTest {
2424

compiler/src/test/java/com/github/mustachejava/FailOnMissingTest.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
import com.github.mustachejava.util.Wrapper;
77
import org.junit.Test;
88

9-
import java.io.IOException;
109
import java.io.StringReader;
1110
import java.io.StringWriter;
1211
import java.util.List;
1312

1413
import static org.junit.Assert.assertEquals;
15-
import static org.junit.Assert.fail;
14+
import static org.junit.Assert.assertThrows;
1615

1716
public class FailOnMissingTest {
1817
@Test
@@ -34,19 +33,15 @@ protected synchronized Wrapper getWrapper(String name, List<Object> scopes) {
3433
};
3534
DefaultMustacheFactory dmf = new DefaultMustacheFactory();
3635
dmf.setObjectHandler(roh);
37-
try {
36+
MustacheException me = assertThrows(MustacheException.class, () -> {
3837
Mustache test = dmf.compile(new StringReader("{{test}}"), "test");
3938
StringWriter sw = new StringWriter();
4039
test.execute(sw, new Object() {
4140
String test = "ok";
4241
}).close();
4342
assertEquals("ok", sw.toString());
4443
test.execute(new StringWriter(), new Object());
45-
fail("Should have failed");
46-
} catch (MustacheException me) {
47-
assertEquals("test not found in [test:1] @[test:1]", me.getCause().getMessage());
48-
} catch (IOException e) {
49-
e.printStackTrace();
50-
}
44+
});
45+
assertEquals("test not found in [test:1] @[test:1]", me.getCause().getMessage());
5146
}
5247
}

compiler/src/test/java/com/github/mustachejava/FallbackTest.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
import java.io.*;
77
import java.util.HashMap;
88
import java.util.Map;
9-
import java.util.concurrent.ExecutionException;
109

1110
import static com.github.mustachejava.TestUtil.getContents;
12-
import static junit.framework.Assert.fail;
1311
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertThrows;
1413

1514
public class FallbackTest {
1615

1716
private static File rootDefault;
1817
private static File rootOverride;
1918

2019
@Test
21-
public void testDefaultPage1() throws MustacheException, IOException, ExecutionException, InterruptedException {
20+
public void testDefaultPage1() throws MustacheException, IOException {
2221
MustacheFactory c = new FallbackMustacheFactory(rootDefault, rootDefault); // no override
2322
Mustache m = c.compile("page1.html");
2423
StringWriter sw = new StringWriter();
@@ -29,7 +28,7 @@ public void testDefaultPage1() throws MustacheException, IOException, ExecutionE
2928
}
3029

3130
@Test
32-
public void testOverridePage1() throws MustacheException, IOException, ExecutionException, InterruptedException {
31+
public void testOverridePage1() throws MustacheException, IOException {
3332
MustacheFactory c = new FallbackMustacheFactory(rootOverride, rootDefault);
3433
Mustache m = c.compile("page1.html");
3534
StringWriter sw = new StringWriter();
@@ -40,7 +39,7 @@ public void testOverridePage1() throws MustacheException, IOException, Execution
4039
}
4140

4241
@Test
43-
public void testOverridePage2() throws MustacheException, IOException, ExecutionException, InterruptedException {
42+
public void testOverridePage2() throws MustacheException, IOException {
4443
MustacheFactory c = new FallbackMustacheFactory(rootOverride, rootDefault);
4544
Mustache m = c.compile("page2.html");
4645
StringWriter sw = new StringWriter();
@@ -53,13 +52,11 @@ public void testOverridePage2() throws MustacheException, IOException, Execution
5352
@Test
5453
public void testMustacheNotFoundException() {
5554
String nonExistingMustache = "404";
56-
try {
55+
MustacheNotFoundException e = assertThrows(MustacheNotFoundException.class, () -> {
5756
MustacheFactory c = new FallbackMustacheFactory(rootOverride, rootDefault);
5857
c.compile(nonExistingMustache);
59-
fail("Didn't throw an exception");
60-
} catch (MustacheNotFoundException e) {
61-
assertEquals(nonExistingMustache, e.getName());
62-
}
58+
});
59+
assertEquals(nonExistingMustache, e.getName());
6360
}
6461

6562
@BeforeClass

0 commit comments

Comments
 (0)