Created
May 2, 2012 15:15
-
-
Save rschumm/2577376 to your computer and use it in GitHub Desktop.
JUnit Parametrized
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ch.schumm.h2; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* JUnitDataProvider | |
* @author Rémy Schumm - 2010 | |
*/ | |
public class JUnitDataProvider { | |
private Connection conn; | |
/** | |
* @throws ClassNotFoundException | |
* @throws SQLException | |
* | |
*/ | |
public JUnitDataProvider() throws ClassNotFoundException, SQLException { | |
Class.forName("org.h2.Driver"); | |
conn = DriverManager.getConnection("jdbc:h2:mem:"); | |
loadData(); | |
} | |
private void loadData() throws SQLException { | |
Statement insertStatement = conn.createStatement(); | |
String insert = "CREATE TABLE produkte(a int, b int, p int) AS SELECT * FROM CSVREAD('produkte.csv', NULL, NULL, ';')"; | |
insertStatement.execute(insert); | |
} | |
/** | |
* liefert die Testdaten, die von JUnit in den Konstruktor | |
* ProduktTest(int a, int b, int p) abgefüllt werden. | |
* @return eine Collection von Object-Arrays der Form int a, int b, int p | |
* @throws SQLException | |
*/ | |
public List<Object[]> getJUnitParameters() throws SQLException { | |
ArrayList<Object[]> ret = new ArrayList<Object[]>(); | |
Statement suchStatement = conn.createStatement(); | |
String suche = "Select * from produkte"; | |
ResultSet resultSet = suchStatement.executeQuery(suche); | |
while (resultSet.next()){ | |
int a = resultSet.getInt(resultSet.findColumn("a")); | |
int b = resultSet.getInt(resultSet.findColumn("b")); | |
int p = resultSet.getInt(resultSet.findColumn("p")); | |
//Object-Array der Form int a, int b, int p | |
//für den Konstruktor des JUnit-Tests. | |
ret.add(new Object[]{a, b, p}); | |
} | |
System.out.println("Testdaten gelesen."); | |
return ret; | |
} | |
/** | |
* zu H2-Testzwecken. | |
* @param args | |
* @throws ClassNotFoundException | |
* @throws SQLException | |
*/ | |
public static void main(String[] args) throws SQLException, ClassNotFoundException { | |
List<Object[]> jUnitParameters = new JUnitDataProvider().getJUnitParameters(); | |
System.out.println(jUnitParameters.toString()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ch.schumm.junit; | |
import static org.junit.Assert.assertEquals; | |
import java.sql.SQLException; | |
import java.util.Collection; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.junit.runners.Parameterized; | |
import org.junit.runners.Parameterized.Parameters; | |
import ch.schumm.h2.JUnitDataProvider; | |
/** | |
* ProduktTest | |
* @author Rémy Schumm - 2010 | |
*/ | |
@RunWith(Parameterized.class) | |
public class ProduktTest { | |
private int aInput; | |
private int bInput; | |
private int pErwartet; | |
public ProduktTest(int a, int b, int p){ | |
super(); | |
this.aInput = a; | |
this.bInput = b; | |
this.pErwartet = p; | |
} | |
/** | |
* liefert die Testdaten, die von JUnit in den Konstruktor | |
* ProduktTest(int a, int b, int p) abgefüllt werden. | |
* @return eine Collection von Object-Arrays der Form int a, int b, int p | |
* @throws SQLException | |
* @throws ClassNotFoundException | |
*/ | |
@Parameters | |
public static Collection<Object[]> holeProduktTestdaten() throws SQLException, ClassNotFoundException{ | |
return new JUnitDataProvider().getJUnitParameters(); | |
} | |
@Test | |
public void testProdukt(){ | |
int produkt = aInput * bInput; | |
assertEquals("Falsches Produkt von " + aInput + " und " + bInput, pErwartet, produkt); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment