古典的な例では、TestNGユニットのテストフレームワークを使い始める方法を示します。

使用されるツール:

  1. TestNG 6.8.7

  2. Maven 3

  3. Eclipse IDE

1. TestNGの依存関係

`pom.xml`にTestNGライブラリを追加します。

pom.xml

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.7</version>
        <scope>test</scope>
    </dependency>

2. TestNGの例

簡単なクラスを見直し、固定メール ”

[email protected]

“を返す方法があります。

RandomEmailGenerator.java

package com.mkyong.testng.project.service.email;

import org.springframework.stereotype.Service;

public class RandomEmailGenerator {

    public String generate() {
        return "[email protected]";
    }

}

次のようなテストケースを作成します。

TestHelloWorld.java

package com.mkyong.testng.examples.helloworld;

import org.testng.Assert;
import org.testng.annotations.Test;
import com.mkyong.testng.project.service.email.RandomEmailGenerator;

public class TestHelloWorld {

    @Test()
    public void testEmailGenerator() {

        RandomEmailGenerator obj = new RandomEmailGenerator();
        String email = obj.generate();

        Assert.assertNotNull(email);
        Assert.assertEquals(email, "[email protected]");

    }

}

このテストで `RandomEmailGenerator.generate()`が常に ”

[email protected]

“を返すことを確認します。

3. TestNG Eclipseプラグイン

Eclipse IDEで上記のテストを実行するには、TestNG Eclipseプラグインをインストールする必要があります。このhttp://testng.org/doc/eclipse.html[公式TestNG Eclipseプラグインガイド]に従ってインストールしてください。

TestNGテストを実行するには、テストクラスを右クリックし、 “TestNG Test”として実行します。


testng-eclipse-plugin、width = 640、height = 379

結果


testng-hello-world、width = 493、height = 347

参考文献

ドキュメンテーション]