0
這些是我現在要測試的方法。如何在JUnit測試用例中測試方法中的方法?
public void addGrade(String newGrade) {
if (numGrades >= grades.length) {
increaseGradesCapacity();
}
grades[numGrades] = newGrade;
numGrades++;
}
public void increaseGradesCapacity() {
String[] newGrades = new String[grades.length + 1];
for (int i = 0; i < grades.length; i++) {
newGrades[i] = grades[i];
}
grades = newGrades;
}
目前,我有一個,而不必調用increaseGradesCapacity()方法測試一個測試案例:
@Test public void addGradeTest() {
String[] myGrades1 = {"a90", "a100", "q90", "q80",
"p100", "e87.5"};
String[] myGrades2 = {"a90", "a100", "q90", "q80",
"p100", "e87.5", "q100"};
WeightedGrades2 wg = new WeightedGrades2("Aegon Targaryen", 6, myGrades1);
wg.addGrade("q100");
Assert.assertArrayEquals("Test for addGrade.",
myGrades2, wg.getGrades());
}
但我試圖找出如何創建調用一個測試用例增加GradesCapacity()方法。這就是我現在所擁有的,但它仍然不能滿足Web-CAT的要求。有人有主意嗎?
@Test public void addGradeTest2() {
String[] myGrades1 = {"a90", "a100", "q90", "q80",
"p100", "e87.5"};
String[] myGrades2 = {"a90", "a100", "q90", "q80",
"p100", "e87.5", null, "q100"};
WeightedGrades2 wg = new WeightedGrades2("Anakin Skywalker", 7, myGrades1);
wg.increaseGradesCapacity();
wg.addGrade("q100");
Assert.assertArrayEquals("Test for addGrade.",
myGrades2, wg.getGrades());
}
設計查詢:應該'increaseGradesCapacity'公開嗎?這看起來像內部API。但無論哪種方式,調用'addGrade'都會污染增加容量的測試......爲什麼不直接調用increaseGradesCapacity並測試該數組是否更大並且已正確保持內容? – Ash