ChaizOFF3123123

Untitled

Jul 2nd, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. package com.epam.rd.autotasks.godutch;
  2.  
  3. import org.junit.jupiter.params.ParameterizedTest;
  4. import static org.junit.jupiter.params.provider.Arguments.arguments;
  5.  
  6. import org.junit.jupiter.params.provider.Arguments;
  7. import org.junit.jupiter.params.provider.MethodSource;
  8. import static org.junit.jupiter.api.Assertions.assertEquals;
  9.  
  10. import java.io.*;
  11. import java.util.stream.Stream;
  12.  
  13.  
  14. public class GoDutchTest {
  15.  
  16. @ParameterizedTest
  17. @MethodSource("correctDataProvider")
  18. void correctInputTest(String totalBill, String numberOfFriends, String partToPay) {
  19. String input = totalBill + System.lineSeparator() + numberOfFriends;
  20.  
  21. ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes());
  22. BufferedInputStream controlledIn = new BufferedInputStream(inputStream);
  23. InputStream defaultIn = System.in;
  24. System.setIn(controlledIn);
  25.  
  26. final ByteArrayOutputStream sink = new ByteArrayOutputStream();
  27. PrintStream controlledOut = new PrintStream(sink);
  28. PrintStream defaultOut = System.out;
  29. System.setOut(controlledOut);
  30.  
  31. try {
  32. GoDutch.main(new String[]{});
  33. controlledOut.flush();
  34. final String actual = sink.toString().trim();
  35. assertEquals(partToPay, actual);
  36. } finally {
  37. System.setIn(defaultIn);
  38. System.setOut(defaultOut);
  39. }
  40.  
  41. }
  42.  
  43. @ParameterizedTest
  44. @MethodSource("incorrectTotalBillDataProvider")
  45. void incorrectTotalBillTest(String totalBill, String numberOfFriends) {
  46. String input = totalBill + System.lineSeparator() + numberOfFriends;
  47.  
  48. ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes());
  49. BufferedInputStream controlledIn = new BufferedInputStream(inputStream);
  50. InputStream defaultIn = System.in;
  51. System.setIn(controlledIn);
  52.  
  53. final ByteArrayOutputStream sink = new ByteArrayOutputStream();
  54. PrintStream controlledOut = new PrintStream(sink);
  55. PrintStream defaultOut = System.out;
  56. System.setOut(controlledOut);
  57.  
  58. try {
  59. GoDutch.main(new String[]{});
  60. controlledOut.flush();
  61. final String actual = sink.toString().trim();
  62. assertEquals("Bill total amount cannot be negative", actual);
  63. } finally {
  64. System.setIn(defaultIn);
  65. System.setOut(defaultOut);
  66. }
  67. }
  68.  
  69. @ParameterizedTest
  70. @MethodSource("incorrectNumberOfFriendsDataProvider")
  71. void incorrectNumberOfFriendsTest(String totalBill, String numberOfFriends) {
  72. String input = totalBill + System.lineSeparator() + numberOfFriends;
  73.  
  74. ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes());
  75. BufferedInputStream controlledIn = new BufferedInputStream(inputStream);
  76. InputStream defaultIn = System.in;
  77. System.setIn(controlledIn);
  78.  
  79. final ByteArrayOutputStream sink = new ByteArrayOutputStream();
  80. PrintStream controlledOut = new PrintStream(sink);
  81. PrintStream defaultOut = System.out;
  82. System.setOut(controlledOut);
  83.  
  84. try {
  85. GoDutch.main(new String[]{});
  86. controlledOut.flush();
  87. final String actual = sink.toString().trim();
  88. assertEquals("Number of friends cannot be negative or zero", actual);
  89. } finally {
  90. System.setIn(defaultIn);
  91. System.setOut(defaultOut);
  92. }
  93. }
  94.  
  95.  
  96.  
  97.  
  98. static Stream<Arguments> correctDataProvider() {
  99. return Stream.of(
  100. arguments("10000", "5", "2200"),
  101. arguments("5000", "1", "5500"),
  102. arguments("200", "220", "1"),
  103. arguments("0", "100", "0"),
  104. arguments("1", "1", "1")
  105. );
  106. }
  107.  
  108. static Stream<Arguments> incorrectTotalBillDataProvider() {
  109. return Stream.of(
  110. arguments("-100", "2"),
  111. arguments("-2", "2"),
  112. arguments("-1", "1")
  113. );
  114. }
  115.  
  116. static Stream<Arguments> incorrectNumberOfFriendsDataProvider() {
  117. return Stream.of(
  118. arguments("100", "-2"),
  119. arguments("340", "-2300"),
  120. arguments("1236", "0")
  121. );
  122. }
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment