如何在 JUNIT 的方法中为使用字符串和数组总和的 IF 语句编写测试

How can I write a test for an IF statement that uses a String and the sum of an Array in a method with JUNIT

我需要测试 BasketBallGame class,我编写了多个有效的测试,现在我想测试方法中的最终 IF 语句:public BasketResult play(String category) {}

我已经为其他两个 IF 语句编写了测试,并使用 Mockito 模拟了 ShotAttempt 方法。

@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 语句和 return 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());
}