/
JUnitFormatterTest.java
73 lines (61 loc) · 3.06 KB
/
JUnitFormatterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package cucumber.runtime.formatter;
import cucumber.runtime.Backend;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.io.ClasspathResourceLoader;
import gherkin.formatter.model.Step;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Test;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class JUnitFormatterTest {
@Test
public void featureSimpleTest() throws Exception {
File report = runFeaturesWithJunitFormatter(asList("cucumber/runtime/formatter/JUnitFormatterTest_1.feature"));
assertXmlEqual("cucumber/runtime/formatter/JUnitFormatterTest_1.report.xml", report);
}
@Test
public void featureWithBackgroundTest() throws Exception {
File report = runFeaturesWithJunitFormatter(asList("cucumber/runtime/formatter/JUnitFormatterTest_2.feature"));
assertXmlEqual("cucumber/runtime/formatter/JUnitFormatterTest_2.report.xml", report);
}
@Test
public void featureWithOutlineTest() throws Exception {
File report = runFeaturesWithJunitFormatter(asList("cucumber/runtime/formatter/JUnitFormatterTest_3.feature"));
assertXmlEqual("cucumber/runtime/formatter/JUnitFormatterTest_3.report.xml", report);
}
private File runFeaturesWithJunitFormatter(final List<String> featurePaths) throws IOException {
File report = File.createTempFile("cucumber-jvm-junit", "xml");
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
final ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(classLoader);
List<String> args = new ArrayList<String>();
args.add("--format");
args.add("junit:" + report.getAbsolutePath());
args.addAll(featurePaths);
RuntimeOptions runtimeOptions = new RuntimeOptions(new Properties(), args.toArray(new String[args.size()]));
Backend backend = mock(Backend.class);
when(backend.getSnippet(any(Step.class))).thenReturn("TEST SNIPPET");
final cucumber.runtime.Runtime runtime = new Runtime(resourceLoader, classLoader, asList(backend), runtimeOptions);
runtime.run();
return report;
}
private void assertXmlEqual(String expected, File actual) throws IOException, ParserConfigurationException, SAXException {
XMLUnit.setIgnoreWhitespace(true);
InputStreamReader control = new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(expected), "UTF-8");
Diff diff = new Diff(control, new FileReader(actual));
assertTrue("XML files are similar " + diff, diff.identical());
}
}