如何编写一个使用String和数组总和的IF语句的测试方法,使用JUNIT。

huangapple go评论117阅读模式
英文:

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

问题

I need to test the BasketBallGame class, ive written multiple tests who work and now I want to test the final IF statement in the method: public BasketResult play(String category) {}

I have written test for the other two IF statements and used Mockito to mock the ShotAttempt method.

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

Now i'm not really sure how I can use the sum of the array value, and the string category in order to test the last IF statement and return a BasketResult.

英文:

I need to test the BasketBallGame class, ive written multiple tests who work and now I want to test the final IF statement in the method: public BasketResult play(String category) {}

I have written test for the other two IF statements and used Mockito to mock the ShotAttempt method.

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

Now i'm not really sure how I can use the sum of the array value, and the string category in order to test the last IF statement and return a BasketResult.

  1. public class BasketResult {
  2. private final boolean won;
  3. private final int amountInHoop;
  4. BasketResultaat(boolean won, int amountInHoop) {
  5. this.won = won;
  6. this.amountInHoop = amountInHoop;
  7. }
  8. public boolean isWon() {
  9. return won;
  10. }
  11. public int getAmountInHoop() {
  12. return amountInHoop;
  13. }
  14. }
  15. import java.security.SecureRandom;
  16. public class InHoop {
  17. private final SecureRandom random = new SecureRandom();
  18. public int[] shotAttempt(int amountAttempts) {
  19. int[] score = new int[amountAttempts];
  20. for (int index = 0; index < amountAttempts; index++) {
  21. score[index] = random.nextInt(2); // 1 = in hoop, 0 = not in hoop
  22. }
  23. return score;
  24. }
  25. }
  26. import java.util.Arrays;
  27. public class BasketBallGame {
  28. private InHoop inHoop;
  29. private final int AMOUNT_TRIES = 3;
  30. private final String TODDLER = "toddler";
  31. private final String LOWER = "lower";
  32. public BasketBallGame() {
  33. this(new InHoop());
  34. }
  35. public BasketBallGame(InHoop inHoop) {
  36. this.inHoop = inHoop;
  37. }
  38. public double Calculate(int x, double y) {
  39. if (x <= 0 || y < 0)
  40. throw new IllegalArgumentException("x must be stricly positive and y must be positive");
  41. return (AMOUNT_TRIES * 10) - x - y;
  42. }
  43. public BasketResult play(String category) {
  44. if (category == null || category.isBlank()) {
  45. throw new IllegalArgumentException("category must be filled in");
  46. }
  47. if (!categorie.equalsIgnoreCase(TODDLER) && !categorie.equalsIgnoreCase(LOWER)) {
  48. throw new IllegalArgumentException("INVALID category");
  49. }
  50. int[] result = inHoop.shotAttempt(AMOUNT_TRIES);
  51. int amountInHoop = Arrays.stream(result).sum();
  52. // IF toddler And 1x in hoop ==> WIN
  53. // IF LOWER AND 2X IN HOOP ==> WIN
  54. if ((category.equals(TODDLER) && amountInHoop >= 1)
  55. || (categorie.equals(LOWER) && amountInHoop >= 2)) {
  56. return new BasketResult(true, amountInHoop);
  57. }
  58. // did not win
  59. return new BasketResult(false, amountInHoop);
  60. }
  61. }
  62. </details>
  63. # 答案1
  64. **得分**: 1
  65. Pay attention to `@InjectMocks` and `@Mock`.
  66. In addition, the statement `initMocks` is necessary.
  67. ```java
  68. @InjectMocks
  69. BasketBallGame basketBallGame;
  70. @Mock
  71. private InHoop inHoop;
  72. @BeforeEach
  73. void before() {
  74. MockitoAnnotations.initMocks(this);
  75. }
  76. @ParameterizedTest
  77. @CsvSource({
  78. "toddler, '0,0,0', false",
  79. "toddler, '1,0,0', true",
  80. "toddler, '1,1,0', true",
  81. "lower, '1,0,0', false",
  82. "lower, '1,1,0', true",
  83. "lower, '1,1,1', true",
  84. })
  85. public void TestLastIfStatement(String category, String scoreList, Boolean winExp) {
  86. int[] scoreArr = Arrays.stream(scoreList.split(",")).map(String::trim).mapToInt(Integer::parseInt).toArray();
  87. int sumExp = Arrays.stream(scoreArr).sum();
  88. when(inHoop.shotAttempt(anyInt())).thenReturn(scoreArr);
  89. BasketResult result = basketBallGame.play(category);
  90. assertEquals(winExp, result.isWon());
  91. assertEquals(sumExp, result.getAmountInHoop());
  92. }
英文:

Pay attention to @InjectMocks and @Mock.
In addition, the statement initMocks is necessary.

  1. @InjectMocks
  2. BasketBallGame basketBallGame;
  3. @Mock
  4. private InHoop inHoop;
  5. @BeforeEach
  6. void before() {
  7. MockitoAnnotations.initMocks(this);
  8. }
  9. @ParameterizedTest
  10. @CsvSource({
  11. &quot;toddler, &#39;0,0,0&#39;, false&quot;,
  12. &quot;toddler, &#39;1,0,0&#39;, true&quot;,
  13. &quot;toddler, &#39;1,1,0&#39;, true&quot;,
  14. &quot;lower, &#39;1,0,0&#39;, false&quot;,
  15. &quot;lower, &#39;1,1,0&#39;, true&quot;,
  16. &quot;lower, &#39;1,1,1&#39;, true&quot;,
  17. })
  18. public void TestLastIfStatement(String category, String scoreList, Boolean winExp) {
  19. int[] scoreArr = Arrays.stream(scoreList.split(&quot;,&quot;)).map(String::trim).mapToInt(Integer::parseInt).toArray();
  20. int sumExp = Arrays.stream(scoreArr).sum();
  21. when(inHoop.shotAttempt(anyInt())).thenReturn(scoreArr);
  22. BasketResult result = basketBallGame.play(category);
  23. assertEquals(winExp, result.isWon());
  24. assertEquals(sumExp, result.getAmountInHoop());
  25. }

huangapple
  • 本文由 发表于 2020年8月11日 03:11:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63346570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定