以下是一個使用Theories的test case:
IdentificationNumberValidatorTheoriesTest
package com.gss.gmo.cao.validator.constraints.impl;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;
import java.util.ArrayList;
import java.util.Collection;
import org.junit.BeforeClass;
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 IdentificationNumberValidatorTheoriesTest {
@DataPoints
public static DataBean[] identificationNumber;
@BeforeClass
public static void prepareData() {
Collection<DataBean> data = new ArrayList<DataBean>();
data.add(new DataBean(true, null));
data.add(new DataBean(false, ""));
data.add(new DataBean(false, "1qazxsw2"));
data.add(new DataBean(true, "H120178472"));
data.add(new DataBean(true, "h120178472"));
data.add(new DataBean(false, "h120178470"));
identificationNumber = data.toArray(new DataBean[] {});
}
private IdentificationNumberValidator identificationNumberValidator = new IdentificationNumberValidator();
@Theory
public void testTrue(DataBean data) {
assumeTrue(data.isResult());
assertTrue(identificationNumberValidator.isValid(data.getIdentificationNumber(), null));
}
@Theory
public void testFalse(DataBean data) {
assumeFalse(data.isResult());
assertFalse(identificationNumberValidator.isValid(data.getIdentificationNumber(), null));
}
static class DataBean {
private final boolean result;
private final String identificationNumber;
DataBean(boolean result, String identificationNumber) {
this.result = result;
this.identificationNumber = identificationNumber;
}
public boolean isResult() {
return result;
}
public String getIdentificationNumber() {
return identificationNumber;
}
}
}
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;
import java.util.ArrayList;
import java.util.Collection;
import org.junit.BeforeClass;
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 IdentificationNumberValidatorTheoriesTest {
@DataPoints
public static DataBean[] identificationNumber;
@BeforeClass
public static void prepareData() {
Collection<DataBean> data = new ArrayList<DataBean>();
data.add(new DataBean(true, null));
data.add(new DataBean(false, ""));
data.add(new DataBean(false, "1qazxsw2"));
data.add(new DataBean(true, "H120178472"));
data.add(new DataBean(true, "h120178472"));
data.add(new DataBean(false, "h120178470"));
identificationNumber = data.toArray(new DataBean[] {});
}
private IdentificationNumberValidator identificationNumberValidator = new IdentificationNumberValidator();
@Theory
public void testTrue(DataBean data) {
assumeTrue(data.isResult());
assertTrue(identificationNumberValidator.isValid(data.getIdentificationNumber(), null));
}
@Theory
public void testFalse(DataBean data) {
assumeFalse(data.isResult());
assertFalse(identificationNumberValidator.isValid(data.getIdentificationNumber(), null));
}
static class DataBean {
private final boolean result;
private final String identificationNumber;
DataBean(boolean result, String identificationNumber) {
this.result = result;
this.identificationNumber = identificationNumber;
}
public boolean isResult() {
return result;
}
public String getIdentificationNumber() {
return identificationNumber;
}
}
}