Junit 测试填充数组并计算无法填充的项目的方法
Junit Test for a Method that fills an Array and counts the items it cant fill in
我有以下方法,需要一个 Junit 测试,但不知道如何start.i希望有人能帮助我。
package edu.hm.cs.swe2.shoppingmall;
public class ShoppingMall {
protected Shop[] shops;
public ShoppingMall(int shopCount) {
if (shopCount < 1 || shopCount > 10) {
shopCount = 10;
}
this.shops = new Shop[shopCount];
}
public int size() {
return shops.length;
}
public int addShops(Shop... shopsToAdd) {
int notAddedShops = 0;
int addedShops = 0;
for (Shop shop : shopsToAdd) {
for (int i = 0; i <= shops.length - 1; i++) {
if (shops[i] == null) {
shops[i] = shop;
addedShops++;
break;
}
}
if (addedShops <= shopsToAdd.length) {
notAddedShops = shopsToAdd.length - addedShops;
}
}
return notAddedShops;
}
所以通常你会看一下 class 看看它做了什么。显然,您对 Shops 进行了某种 arrayList 实现。您可以向其中添加商店。
创建的 ShoppingMall 具有初始大小。你应该测试一下。例如,测试如果您使用负数或非常大的数字调用构造函数会发生什么。代码是否按照您的预期执行?
接下来,addShops 函数有一些您应该测试的逻辑。
休息一下,想一想它可能不起作用的某些场景。你期望发生什么?例如,您可以尝试向 ShoppingMall 添加比允许的更多的商店。应该发生什么?哪些店铺应该加哪些不加? return 的方法是什么?
我有以下方法,需要一个 Junit 测试,但不知道如何start.i希望有人能帮助我。
package edu.hm.cs.swe2.shoppingmall;
public class ShoppingMall {
protected Shop[] shops;
public ShoppingMall(int shopCount) {
if (shopCount < 1 || shopCount > 10) {
shopCount = 10;
}
this.shops = new Shop[shopCount];
}
public int size() {
return shops.length;
}
public int addShops(Shop... shopsToAdd) {
int notAddedShops = 0;
int addedShops = 0;
for (Shop shop : shopsToAdd) {
for (int i = 0; i <= shops.length - 1; i++) {
if (shops[i] == null) {
shops[i] = shop;
addedShops++;
break;
}
}
if (addedShops <= shopsToAdd.length) {
notAddedShops = shopsToAdd.length - addedShops;
}
}
return notAddedShops;
}
所以通常你会看一下 class 看看它做了什么。显然,您对 Shops 进行了某种 arrayList 实现。您可以向其中添加商店。
创建的 ShoppingMall 具有初始大小。你应该测试一下。例如,测试如果您使用负数或非常大的数字调用构造函数会发生什么。代码是否按照您的预期执行?
接下来,addShops 函数有一些您应该测试的逻辑。
休息一下,想一想它可能不起作用的某些场景。你期望发生什么?例如,您可以尝试向 ShoppingMall 添加比允许的更多的商店。应该发生什么?哪些店铺应该加哪些不加? return 的方法是什么?