package com.mycompany.sandbox;
public class XY {
private int pX;
private int pY;
public XY(int x, int y) {
pX = x;
pY = y;
}
/**
* ¾Ý¸Â¤òµá¤á¤ë.
* X¼´¡¢Y¼´¾å¤ÎÅÀ¤Ï 0 ¤òÊÖ¤·¤Þ¤¹
* @return ¾Ý¸Â
*/
public int getQuadrand() {
if (pX > 0 && pY > 0) {
return 1;
} else if (pX < 0 && pY > 0) {
return 2;
} else if (pX < 0 && pY < 0) {
return 3;
} else if (pX > 0 && pY < 0) {
return 4;
} else {
return 0;
}
}
}
package com.mycompany.sandbox;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
@RunWith(Theories.class)
public class XYTest {
public static class Fixture {
int x;
int y;
int expect;
public Fixture(int x, int y, int q) {
this.x = x;
this.y = y;
this.expect = q;
}
}
@DataPoints
public static Fixture[] PARAMs = new Fixture[]{
new Fixture( 1, 1, 1),
new Fixture(-1, 1, 2),
new Fixture(-1,-1, 3),
new Fixture( 1,-1, 4),
new Fixture( 0, 0, 0)
};
@Theory
public void test(Fixture f) {
System.out.println(
String.format("(%d,%d) expects %c", f.x , f.y, f.expect + 'µ' - 1));
XY xy = new XY(f.x, f.y);
assertThat(xy.getQuadrand(), is(f.expect));
}
}
package com.mycompany.sandbox;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.prefs.CsvPreference;
@RunWith(Theories.class)
public class XYTestWithCSV {
public static class Fixture {
public static final CellProcessor[] processors = new CellProcessor[] {
new ParseInt(), // testNo
new ParseInt(), // x
new ParseInt(), // y
new ParseInt(), // expect
null // explanation
};
private int testNo;
private int x;
private int y;
private int expect;
private String explanation;
public int getTestNo() {
return testNo;
}
public void setTestNo(int testNo) {
this.testNo = testNo;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getExpect() {
return expect;
}
public void setExpect(int expect) {
this.expect = expect;
}
public String getExplanation() {
return explanation;
}
public void setExplanation(String explanation) {
this.explanation = explanation;
}
}
@DataPoints
public static Fixture[] getParams() {
Reader reader;
try {
reader = new InputStreamReader(new FileInputStream(clazzpath2filepath("xy.csv")), "MS932");
CsvBeanReader csv = new CsvBeanReader(reader, CsvPreference.EXCEL_PREFERENCE);
final String[] header = csv.getHeader(true);
List<Fixture> lst = new ArrayList<Fixture>();
Fixture fixture = null;
while((fixture = csv.read(Fixture.class, header, Fixture.processors)) != null){
lst.add(fixture);
}
return lst.toArray(new Fixture[0]);
} catch (Throwable e) {
// Æɤ߹þ¤ß¥¨¥é¡¼¤Î¸¶°øµæÌÀ¤Î¤¿¤á¤Ë Throwable ¤ò Catch ¤¹¤ë
e.printStackTrace();
throw new RuntimeException(e);
}
};
@Theory
public void test(Fixture f) {
System.out.println(String.format("test %d : %s", f.testNo, f.explanation));
XY xy = new XY(f.x, f.y);
assertThat(xy.getQuadrand(), is(f.expect));
}
private static String clazzpath2filepath(final String clazzpath) {
try {
ClassLoader loader = XYTestWithCSV.class.getClassLoader();
URL url = loader.getResource(clazzpath);
System.out.println(url);
File inputFile = new File(url.toURI());
return inputFile.getAbsolutePath();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
<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>
<dependency>
<groupId>net.sf.supercsv</groupId>
<artifactId>super-csv</artifactId>
<version>2.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package com.mycompany.sandbox;
import java.io.File;
import java.io.FileInputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
@RunWith(Theories.class)
public class XYTestWithExcel {
public static class Fixture {
int testNo;
int x;
int y;
int expect;
String explanation;
public Fixture(Row row) {
testNo = (int) row.getCell(0).getNumericCellValue();
x = (int) row.getCell(1).getNumericCellValue();
y = (int) row.getCell(2).getNumericCellValue();
expect = (int) row.getCell(3).getNumericCellValue();
explanation = row.getCell(4).getStringCellValue();
}
}
@DataPoints
public static Fixture[] getParams() {
try {
List<Fixture> lst = new ArrayList<Fixture>();
// ¥Õ¥¡¥¤¥ë¤Î¼ïÎà¤Ë¤è¤Ã¤Æ XSSFWorkbook(2007·Á¼°), HSSFWorkbook(97-2003·Á¼°)
// ¤Î¤É¤Á¤é¤«¤¬¤Ç¤¤ë
// (¤É¤Á¤é¤â¥¤¥ó¥¿¥Õ¥§¡¼¥¹ Workbook ¤Î¼ÂÁõ)
Workbook wb = WorkbookFactory.create(new FileInputStream(
clazzpath2filepath("xy.xls")));
Sheet sheet = wb.getSheetAt(0);
for (int rowNo = (sheet.getFirstRowNum() + 1); rowNo <= sheet
.getLastRowNum(); rowNo++) {
Row row = sheet.getRow(rowNo);
Cell idxCell = row.getCell(0);
if (idxCell == null
|| idxCell.getCellType() != Cell.CELL_TYPE_NUMERIC) {
// ¹Ô¤òºï½ü¤·¤Æ¤â getLastRowNum() ¤Ï¤½¤Î¤Þ¤Þ¤Ê¤Î¤Ç¡¢¥Æ¥¹¥È¥±¡¼¥¹ÈÖ¹æ¤Î¤¢¤ê¤Ê¤·¤Ç¤â
// ½ªÎ»È½Äꤹ¤ë
break;
}
lst.add(new Fixture(row));
}
return lst.toArray(new Fixture[0]);
} catch (Throwable e) {
// Æɤ߹þ¤ß¥¨¥é¡¼¤Î¸¶°øµæÌÀ¤Î¤¿¤á¤Ë Throwable ¤ò Catch ¤¹¤ë
e.printStackTrace();
throw new RuntimeException(e);
}
};
@Theory
public void test(Fixture f) {
System.out.println(String.format("test %d : %s", f.testNo,
f.explanation));
XY xy = new XY(f.x, f.y);
assertThat(xy.getQuadrand(), is(f.expect));
}
private static String clazzpath2filepath(final String clazzpath) {
try {
ClassLoader loader = XYTestWithExcel.class.getClassLoader();
URL url = loader.getResource(clazzpath);
System.out.println(url);
File inputFile = new File(url.toURI());
return inputFile.getAbsolutePath();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
package com.mycompany.sandbox;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.yaml.snakeyaml.Yaml;
@RunWith(Theories.class)
public class XYTestWithYaml {
public static class Fixture {
public int x;
public int y;
public int expect;
}
@DataPoints
@SuppressWarnings("unchecked")
public static Fixture[] getParams() {
InputStream in;
try {
in = new FileInputStream(clazzpath2filepath("xy.yaml"));
List<Fixture> lst = (List<Fixture>) (new Yaml().load(in));
return lst.toArray(new Fixture[0]);
} catch (Throwable e) {
// Æɤ߹þ¤ß¥¨¥é¡¼¤Î¸¶°øµæÌÀ¤Î¤¿¤á¤Ë Throwable ¤ò Catch ¤¹¤ë
e.printStackTrace();
throw new RuntimeException(e);
}
};
@Theory
public void test(Fixture f) {
System.out.println(
String.format("(%d,%d) expects %c", f.x , f.y, f.expect + 'µ' - 1));
XY xy = new XY(f.x, f.y);
assertThat(xy.getQuadrand(), is(f.expect));
}
private static String clazzpath2filepath(final String clazzpath) {
try {
ClassLoader loader = XYTestWithYaml.class.getClassLoader();
URL url = loader.getResource(clazzpath);
System.out.println(url);
File inputFile = new File(url.toURI());
return inputFile.getAbsolutePath();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
¢ª Maven ¤á¤Ã¤Á¤ã¤¿¤¯¤µ¤ó¥Æ¥¹¥È¤ò¤·¤Æ¥á¥â¥êÉÔ¤ˤʤë»þ¤ÎÂкö (Covertura, Surefire)
surefire ¤¬¡¢¥Ç¥Õ¥©¥ë¥ÈÀßÄê¤Ç¤Ï Stack Trace ¤ò¥È¥ê¥à¤¹¤ë¤Î¤¬¸¶°ø