如何为使用JUNIT的方法中使用String和Array之和的IF语句编写测试



我需要测试BasketBallGame类,我写了多个测试,现在我想测试方法中的最终IF语句:public BasketResult play(字符串类别({}

我已经为另外两个IF语句编写了测试,并使用Mockito来模拟ShotTempt方法。

@ParameterizedTest
@CsvSource({ "0, 1,0", "1,1,1", "0, 0,1" })
public void MockShotAttempt(int firstValue, int secondValue, int derdeWaarde) {
Mockito.when(inHoop.ShotAttempt(3)).thenReturn(new int[] {firstValue,secondValue,derdeWaarde});         
}
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = {"        ", "TodDDleR", "LoWWeR","#!|@" })
public void Invalid_EmptyCategory(String category) {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
BasketBallGame.Play(category);
});

}

现在我真的不确定如何使用数组值和字符串类别的总和来测试最后一个IF语句并返回BasketResult。


public class BasketResult {
private final boolean won;
private final int amountInHoop;
BasketResultaat(boolean won, int amountInHoop) {
this.won = won;
this.amountInHoop = amountInHoop;
}
public boolean isWon() {
return won;
}
public int getAmountInHoop() {
return amountInHoop;
}
}
import java.security.SecureRandom;
public class InHoop {
private final SecureRandom random = new SecureRandom();
public int[] shotAttempt(int amountAttempts) {
int[] score = new int[amountAttempts];
for (int index = 0; index < amountAttempts; index++) {
score[index] = random.nextInt(2); // 1 = in hoop, 0 = not in hoop
}
return score;
}
}
import java.util.Arrays;
public class BasketBallGame {
private InHoop inHoop;
private final int AMOUNT_TRIES = 3;
private final String TODDLER = "toddler";
private final String LOWER = "lower";
public BasketBallGame() {
this(new InHoop());
}
public BasketBallGame(InHoop inHoop) {
this.inHoop = inHoop;
}
public double Calculate(int x, double y) {
if (x <= 0 || y < 0)
throw new IllegalArgumentException("x must be stricly positive and y must be positive");
return (AMOUNT_TRIES * 10) - x - y;
}
public BasketResult play(String category) {
if (category == null || category.isBlank()) {
throw new IllegalArgumentException("category must be filled in");
}
if (!categorie.equalsIgnoreCase(TODDLER) && !categorie.equalsIgnoreCase(LOWER)) {
throw new IllegalArgumentException("INVALID category");
}
int[] result = inHoop.shotAttempt(AMOUNT_TRIES);
int amountInHoop = Arrays.stream(result).sum();
// IF toddler And 1x in hoop ==> WIN 
// IF LOWER AND 2X IN HOOP ==> WIN
if ((category.equals(TODDLER) && amountInHoop >= 1)
|| (categorie.equals(LOWER) && amountInHoop >= 2)) {
return new BasketResult(true, amountInHoop);
}
// did not win 
return new BasketResult(false, amountInHoop);
}
}

注意@InjectMocks@Mock
此外,语句initMocks也是必需的。

@InjectMocks
BasketBallGame basketBallGame;
@Mock
private InHoop inHoop;
@BeforeEach
void before() {
MockitoAnnotations.initMocks(this);
}
@ParameterizedTest
@CsvSource({
"toddler, '0,0,0', false",
"toddler, '1,0,0', true",
"toddler, '1,1,0', true",
"lower,   '1,0,0', false",
"lower,   '1,1,0', true",
"lower,   '1,1,1', true",
})
public void TestLastIfStatement(String category, String scoreList, Boolean winExp) {
int[] scoreArr = Arrays.stream(scoreList.split(",")).map(String::trim).mapToInt(Integer::parseInt).toArray();
int sumExp = Arrays.stream(scoreArr).sum();
when(inHoop.shotAttempt(anyInt())).thenReturn(scoreArr);
BasketResult result = basketBallGame.play(category);
assertEquals(winExp, result.isWon());
assertEquals(sumExp, result.getAmountInHoop());
}

最新更新