0

这些是我现在正在尝试测试的方法。

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());                           
}

但我试图弄清楚如何创建一个调用 increaseGradesCapacity() 方法的测试用例。这就是我现在所拥有的,但它仍然不能满足 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());
}
4

1 回答 1

0

我不知道在这种情况下必须满足什么“Web-CAT 要求”,但我会给你一个一般性的答案,希望能回答你的问题。

鉴于所讨论的方法 ,increaseGradesCapacity()负责增加grades数组的大小,我将简单地在调用此方法之前和之后测试大小。因此,您的测试可能如下所示:

@Test public void addGradeTest2() {
  String[] myGrades1 = {"a90", "a100", "q90", "q80", "p100", "e87.5"};
  WeightedGrades2 wg = new WeightedGrades2("Anakin Skywalker", 7, myGrades1);

  int sizeBeforeIncrease = wg.getGrades().length;

  wg.increaseGradesCapacity();

  int sizeAfterIncrease = wg.getGrades().length;

  Assert.assertTrue("Expecting size after increase to be larger.", 
                       sizeAfterIncrease > sizeBeforeIncrease);
}

注意我是如何放弃这wg.addGrade('q100');条线的。如果您的第二个测试完全负责测试increaseGradesCapacity(),并且该addGrade方法修改了您最终要检查其大小的等级数组,那么它会使不准确,或者至少不集中并包含在一个概念中。

于 2013-10-31T03:49:23.990 回答