EasyMockを使用すると、クラスJarFileの模擬オブジェクトを作成できます。模擬オブジェクトでは、テストで呼び出されるメソッドと、実際にファイルシステム上にJARファイルを作成する必要なしに返される値を指定します。
次に、モックJarFileインスタンスでgetTestXml()メソッドを呼び出します。
これに慣れるまでには時間が必要ですが、努力する価値はあると思います。ここで
public class JarFileUser {
public Map<String, Document> getTestXml(JarFile jarFile) throws IOException, ParserConfigurationException, SAXException {
Map<String, Document> result = new HashMap<String, Document>();
Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
String name = jarEntry.getName();
if (name.endsWith(".class") && !name.contains("$")) {
String testClassName = name.replace(".class", "").replace("/", ".");
String testXmlFilename = "TEST-" + testClassName + ".xml";
InputStream testXmlInputStream = jarFile.getInputStream(jarFile.getJarEntry(testXmlFilename));
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document testXmlDocument = documentBuilder.parse(testXmlInputStream);
result.put(testClassName, testXmlDocument);
}
}
return result;
}
}
はEasyMockとテストです::追加のライブラリの
public class JarFileUserTest {
private JarFile mockJarFile;
private Enumeration<JarEntry> mockJarEntries;
private JarFileUser jarFileUser;
private JarEntry first;
private JarEntry second;
private JarEntry firstXml;
@Before
public void setUp() throws Exception {
jarFileUser = new JarFileUser();
// Create a mock for the JarFile parameter
mockJarFile = createMock(JarFile.class);
// User Vector to provide an Enumeration of JarEntry-Instances
Vector<JarEntry> entries = new Vector<JarEntry>();
first = createMock(JarEntry.class);
second = createMock(JarEntry.class);
entries.add(first);
entries.add(second);
expect(first.getName()).andReturn("mocktest.JarFileUser.class");
expect(second.getName()).andReturn("mocktest.Ignore$Me.class");
mockJarEntries = entries.elements();
expect(mockJarFile.entries()).andReturn(mockJarEntries);
// JarEntry for the XML file
firstXml = createMock(JarEntry.class);
expect(mockJarFile.getJarEntry("TEST-mocktest.JarFileUser.xml")).andReturn(firstXml);
// XML contents
ByteArrayInputStream is = new ByteArrayInputStream("<test>This is a test.</test>".getBytes("UTF-8"));
expect(mockJarFile.getInputStream(firstXml)).andReturn(is);
replay(mockJarFile);
replay(first);
replay(second);
replay(firstXml);
}
@Test
public void testGetTestXml() throws IOException, ParserConfigurationException, SAXException {
Map<String, Document> map = jarFileUser.getTestXml(mockJarFile);
verify(mockJarFile);
verify(first);
verify(second);
verify(firstXml);
assertEquals(1, map.size());
Document doc = map.get("mocktest.JarFileUser");
assertNotNull(doc);
final Element root = (Element) doc.getDocumentElement();
assertNotNull(root);
assertEquals("test", root.getNodeName());
assertEquals("This is a test.", root.getTextContent());
}
}
注
更新 与えられたソースコードはコンパイルバージョンですので、ここでは、コンパイルされません。 JarFileはクラスでインターフェイスではないため、EasyMock installation docsによれば、クラスパスにとcglibがあります。
私は現在JMockitを使用しています。私がするJarFileをモックしようとすると、私が手:「メイン」java.lang.NoClassDefFoundErrorのスレッドで 例外:ORG/JUnitの/内部/ランナー/モデル/ MultipleFailureException あなたはEasyMockがするJarFileを処理することができますか? –
はいEasyMockはJarFileを処理できます。私は自分の答えを更新しました。 – vanje