ということで、EclipseでTestNGを使ってみます。
題材は前回のStringからDocument、DocumentからStringに変換するクラスでやってみました。
まず、TestNGをダウンロードします。
で、クラスパスを通しておく。
プロジェクトのルートを右クリック>プロパティ>Java Build Path
と辿っていってLibrariesタブのところでさっきダウンロードしたjarにパスを通す。
次にEclipseのプラグインをインストールする。
Help>Software Update>Find and Install>Search for new...>New Remote Site
と辿っていって
http://beust.com/eclipse
に設定する。
あとはこの辺を参考にしてみてください。
基本的にはtestng.xmlを書いて(筆者はプロジェクトルート直下に置いている)、テストコード書くだけ。
XMLUtilsのテストだとこんな感じになる。
testng.xml
==================
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="coreTest" verbose="1">
<test name="xmlUtilsTest" >
<groups>
<run>
<include name="functest" />
</run>
</groups>
<classes>
<class name="lang.XmlUtilsTest" />
</classes>
</test>
</suite>
=================-
XmlUtilsTest.java
=================-
package lang;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import jp.mufu.core.lang.XMLUtils;
import org.testng.annotations.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class XmlUtilsTest {
@Test(groups = { "functest" })
public void testDocument2String() throws Exception {
String docString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<test><answer>hello!</answer></test>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource src = new InputSource(new StringReader(docString));
Document doc = builder.parse(src);
assert docString.equals(XMLUtils.document2String(doc));
}
@Test(groups = { "functest" })
public void testString2Document() throws Exception {
String docString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<test>hello!</test>";
Document doc = XMLUtils.string2Document(docString);
assert "hello!".equals(doc.getFirstChild().getTextContent());
}
}
==============-
・追記(2008/04/29)
テスト結果を書くの忘れてた。
実行するとコンソールにこんな感じででる。
別途EclipseのプラグインのGUIにもっとかっこよくでる。
=========
[Parser] Running:
C:\Dev\Java\Workspace\mufu-core\testng.xml
===============================================
coreTest
Total tests run: 2, Failures: 0, Skips: 0
===============================================
=========