- テスト対象
package com.mycompany.sandbox;
public class App {
public static void main(String[] args) {
App thisObj = new App();
System.out.println(thisObj.factorial(Integer.parseInt(args[0])));
}
/**
* x! を求めます.
*
* @param x 数値
* @return x!
*/
public int factorial(final int x) {
return recursive(x);
}
/**
* 再帰的に x * recursive(x-1) を実行します.
*
* @param x 数値
* @return x!
*/
private int recursive(final int x) {
if (x == 1) {
return x;
}
return x * recursive(x - 1);
}
}
- テストプログラム
package com.mycompany.sandbox;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest {
@Test
public void test() {
App app = new App();
assertThat(app.factorial(10), is(3628800));
}
}
- pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>Sandbox</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Sandbox</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.2</version>
<configuration>
<locales>ja</locales>
<inputEncoding>UTF-8</inputEncoding>
<outputEncoding>UTF-8</outputEncoding>
<reportPlugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.0.201210061924</version>
</plugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
hamcrest-core は、junit 4.11 が依存しているので、勝手にライブラリに入る
hamcrest = matchers