我正在自学Junit测试,并且正在努力为以下代码创建测试
private ArrayList<AirbnbListing> List;
public int getAvgReviews(){
int sums = 0;
for (int i = 0 ; i < List.size(); i++){
AirbnbListing houses = List.get(i);
if(houses.getPrice() >= fromValue && houses.getPrice() <= toValue){
sums += houses.getNumberOfReviews();
}
}
return (sums/getNoOfProperties());
}
有人可以帮我吗
答案 0 :(得分:0)
首先,您的列表未初始化。您可能希望更改功能以接受以like开头的列表
public int getAvgReviews(List<AirbnbListing> listings){ ... }
从那里,我们可以根据需要编写简单或复杂的JUnit。例如:
@Test
public void simpleTest1(){
List<AirbnbListing> list = new List<AirbnbListing>();
// populate your list here with test elements
// ie list.add(new AirbnbListing(...)); as many as needed
// now if your list is 3 elements long with review ratings of [4, 5, 6],
// we would want to test the assertion that getAvgReviews(list) is equal to 5
assertTrue(getAvgReviews(list) == 5);
// ... more assertions here if needed
}