gpt4 book ai didi

java - Junit 测试填充数组并计算无法填充的项目的方法

转载 作者:行者123 更新时间:2023-12-01 16:34:30 25 4
gpt4 key购买 nike

我有以下方法,需要进行 Junit 测试,但不知道如何开始。我希望有人可以帮助我。

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

最佳答案

所以通常您会查看该类以了解它的作用。显然,您有某种与 Shops 相关的 arrayList 实现。您可以向其中添加商店。

ShoppingMall 已创建并具有初始大小。你应该测试一下。例如,测试如果您使用负数或非常大的数字调用构造函数会发生什么。该代码是否执行您期望的操作?

接下来,addShops 函数有一些您应该测试的逻辑。

休息一下,想一下它可能不起作用的某些场景。您预计会发生什么?例如,您可以尝试向 ShoppingMall 添加超出允许数量的商店。应该发生什么?哪些商店应该添加,哪些不应该添加?该方法将返回什么?

关于java - Junit 测试填充数组并计算无法填充的项目的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61987486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com