????TestNG
??????????testng.xml??????TestNG?????????????????????????в?????????
<xml version="1.0" encoding="UTF-8"?>
<suite name="ParameterExampleSuite" parallel="false">
<test name="MathCheckerTest">
<classes>
<parameter name="num" value="3"></parameter>
<class name="com.stormpath.demo.MathCheckerTest"/>
</classes>
</test>
<test name="MathCheckerTest1">
<classes>
<parameter name="num" value="7"></parameter>
<class name="com.stormpath.demo.MathCheckerTest"/>
</classes>
</test>
</suite>
???????????????????????£????????????Σ??3??7????Ρ????????XML??????????????????????????????DataProvider????????????????????
????JUnit
??????TestNG??????????????????????????JUnit?????????????????MathChecker???JUnit??????????
@RunWith(Parameterized.class)
public class MathCheckerTest {
private int inputNumber;
private Boolean expected;
private MathChecker mathChecker;
@Before
public void setup(){
mathChecker = new MathChecker();
}
// Inject via constructor
public MathCheckerTest(int inputNumber?? Boolean expected) {
this.inputNumber = inputNumber;
this.expected = expected;
}
@Parameterized.Parameters
public static Collection<Object[]> getTestData() {
return Arrays.asList(new Object[][]{
{1?? true}??
{2?? false}??
{3?? true}??
{4?? false}??
{5?? true}
});
}
@Test
public void testisOdd() {
System.out.println("Running test for:"+inputNumber);
assertEquals(mathChecker.isOdd(inputNumber)?? expected);
}
}
????????????????????в?????????????getTestData??????????????????????????????????????????????????????????????
????5.???????????Print???
??????????????????????????д???????дSystem.out.println????????????????????С????????????????????????????????2??????????????????????????????????????????????????????????????????????????С???????????????????????????????
????????? StringUti ????????????????????????????????????????????????
????public class StringUtil {
????public String concat(String a??String b) {
????return a + b;
????}
????}
????????????????????????????????
@Test
public void testStringUtil_Bad() {
String result = stringUtil.concat("Hello "?? "World");
System.out.println("Result is "+result);
}
@Test
public void testStringUtil_Good() {
String result = stringUtil.concat("Hello "?? "World");
assertEquals("Hello World"?? result);
}
????testStringUtil\_Bad????????????????ж?????????????????????????????????????????????????????????????????????????????testStringUtil\_Good??????
????6.????????????????????
?????Щ??????????????????????÷????????????????????????????ζ??????????磬???????′?????????????????????????????и?????????????????????λ?????????
public class DemoLogic {
private void veryComplexFunction(){
//This is a complex function that has a lot of database access and is time consuming
//To demo this method?? I am going to add a Thread.sleep for a random number of milliseconds
try {
int time = (int) (Math.random()*100);
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public long calculateTime(){
long time = 0;
long before = System.currentTimeMillis();
veryComplexFunction();
long after = System.currentTimeMillis();
time = after - before;
return time;
}
}
??????????????£??????? calculateTime ????????????????????????????÷?????д???????????????κ??????????÷???????????????????????????????????κ??????е??????
????7.???????????????????????????????
??????????????????????????????????д?????????????????ó??????????????????????????????????????????????????????????????????????Ч?????????????????磬???????????????????????????8????????????????????????????????????????????????????????
?????????????????????????????????
???????????????
???????????????С??8??????????
????????????????????????????????????????????磬??????????????1??100???????????1??100???????????Щ????в???????????????