Java Maven Testを実行する

photo of person typing on computer keyboard

MavenでJavaプログラムのJunitを実行する簡単なチュートリアルです.

目次

pom.xml

Mavenでjunitを呼び出すための依存設定をpom.xmlに記載します.Mavenでプロジェクト作成した場合は恐らく最初から書かれているはずです.

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

Code

package org.example;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        new App().printMessage("Taro");
    }

    public String getMessage(String name) {
        return "Hi," + name + ". Welcome to Maven World!";
    }

    public void printMessage(String name) {
        System.out.printf("%n***** App class *****%n");
        System.out.println(this.getMessage(name));
        System.out.printf("%n***** App class *****%n");
    }
    public int multiplyInteger(int x, int y) {
        return x * y;
    }
}
package org.example;

import junit.framework.TestCase;
import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class AppTest extends TestCase
{
    private App app = null;

    public AppTest(String testName) {
        super(testName);
        app = new App();
    }

    @Test
    public void testApp() {
        assertNotNull(app);
    }

    @Test
    public void testGetMessage() {
        String name = "Taro";
        String msg = "Hi," + name + ". Welcome to Maven World!";
        assertEquals(app.getMessage("Taro"), msg);
    }

    @Test
    public void testMultiplyInteger() {
        int x = 3;
        int y = 5;
        assertEquals(app.multiplyInteger(x, y), 15);
    }
    /**
     * Rigorous Test :-)
     */
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }
}

実行

mvn test
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< org.example:mvn-sample-intellij >-------------------
[INFO] Building mvn-sample-intellij 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ mvn-sample-intellij ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/xxx/IdeaProjects/mvn-sample-intellij/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ mvn-sample-intellij ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ mvn-sample-intellij ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/xxx/IdeaProjects/mvn-sample-intellij/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ mvn-sample-intellij ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/xxx/IdeaProjects/mvn-sample-intellij/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ mvn-sample-intellij ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.example.AppTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.017 s - in org.example.AppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.136 s
[INFO] Finished at: 2022-12-26T00:18:52+09:00
[INFO] ------------------------------------------------------------------------
ブログランキングに参加しています。記事が役に立ったら是非クリックで応援お願いします。

  ブログランキング・にほんブログ村へ
よかったらシェアしてね!
目次