Mockito测试单独运行正常,但一起运行时只有一个有效。

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

Mockito tests work separately but when run together only one works

问题

I have been encountering a strange issue. My test cases have two failing tests, first and third. However, if I run the same separately, it runs perfectly. I am new to JUnit and have no idea why this could happen. I tried to reset mocks and initialize them in setUp() method, but it didn't help.
Is there any problems with static cache?

  1. import org.junit.jupiter.api.AfterEach;
  2. import org.junit.jupiter.api.BeforeEach;
  3. import org.junit.jupiter.api.Test;
  4. import org.mockito.Mockito;
  5. import static org.junit.jupiter.api.Assertions.*;
  6. class CharCounterTest {
  7. private CharCounter charCounter;
  8. private CharCounter spyCharCounter;
  9. @BeforeEach
  10. public void setUp() {
  11. charCounter = new CharCounter();
  12. spyCharCounter = Mockito.spy(charCounter);
  13. }
  14. @AfterEach
  15. public void cleanUp(){
  16. Mockito.reset(spyCharCounter);
  17. }
  18. @Test
  19. void given3IdenticalInputs_whenGetCharacterFrequency_thenPutInputInCacheOnce() {
  20. //CharCounter charCounter = Mockito.spy(new CharCounter());
  21. spyCharCounter.getCharacterFrequency("hello world!");
  22. spyCharCounter.getCharacterFrequency("hello world!");
  23. spyCharCounter.getCharacterFrequency("hello world!");
  24. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("hello world!");
  25. }
  26. @Test
  27. void given3DifferentInputs_whenGetCharacterFrequency_thenPutInputInCacheThrice() {
  28. //CharCounter charCounter = Mockito.spy(new CharCounter());
  29. spyCharCounter.getCharacterFrequency("hello world!");
  30. spyCharCounter.getCharacterFrequency("hello!");
  31. spyCharCounter.getCharacterFrequency("world!");
  32. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("hello world!");
  33. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("hello!");
  34. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("world!");
  35. }
  36. @Test
  37. void given2IdenticalAnd1DifferentInputs_whenGetCharacterFrequency_thenPutInputInCacheTwice() {
  38. //CharCounter charCounter = Mockito.spy(new CharCounter());
  39. spyCharCounter.getCharacterFrequency("hello world!");
  40. spyCharCounter.getCharacterFrequency("hello world!");
  41. spyCharCounter.getCharacterFrequency("world!");
  42. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("hello world!");
  43. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("world!");
  44. }
  45. }```
  46. /*There's a CharCounter class, maybe there is some problems with static cache?
  47. */
  48. ```public class CharCounter {
  49. private static final CacheDecorator cache = new CacheDecorator(new LinkedHashMap<>());
  50. public Map<String, Long> getCharacterFrequency(String text) {
  51. Map<String, Long> characterFrequency;
  52. if (cache.containsKey(text)) {
  53. characterFrequency = cache.getData(text);
  54. } else {
  55. characterFrequency = countCharacters(text);
  56. cache.putData(text, characterFrequency);
  57. }
  58. return characterFrequency;
  59. }
  60. public Map<String, Long> countCharacters(String text) {
  61. Map<String, Long> frequencies = Arrays.stream(text.split(""))
  62. .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
  63. return frequencies;
  64. }
  65. }```
  66. <details>
  67. <summary>英文:</summary>
  68. I have been encountering a strange issue. My test cases have two failing tests, first and third. However, if I run the same separetely, it runs perfectly. I am new to JUnit and have no idea why this could happen. I tried to resed mocks and initialize them in setUp() method, but it didn&#39;t help.
  69. Is there any problems with static cache?
  70. ```import org.junit.jupiter.api.AfterAll;
  71. import org.junit.jupiter.api.AfterEach;
  72. import org.junit.jupiter.api.BeforeEach;
  73. import org.junit.jupiter.api.Test;
  74. import org.mockito.Mockito;
  75. import static org.junit.jupiter.api.Assertions.*;
  76. class CharCounterTest {
  77. private CharCounter charCounter;
  78. private CharCounter spyCharCounter;
  79. @BeforeEach
  80. public void setUp() {
  81. charCounter = new CharCounter();
  82. spyCharCounter = Mockito.spy(charCounter);
  83. }
  84. @AfterEach
  85. public void cleanUp(){
  86. Mockito.reset(spyCharCounter);
  87. }
  88. @Test
  89. void given3IdenticalInputs_whenGetCharacterFrequency_thenPutInputInCacheOnce() {
  90. //CharCounter charCounter = Mockito.spy(new CharCounter());
  91. spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
  92. spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
  93. spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
  94. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;hello world!&quot;);
  95. }
  96. @Test
  97. void given3DiffrentsInputs_whenGetCharacterFrequency_thenPutInputInCacheThrice() {
  98. //CharCounter charCounter = Mockito.spy(new CharCounter());
  99. spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
  100. spyCharCounter.getCharacterFrequency(&quot;hello!&quot;);
  101. spyCharCounter.getCharacterFrequency(&quot;world!&quot;);
  102. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;hello world!&quot;);
  103. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;hello!&quot;);
  104. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;world!&quot;);
  105. }
  106. @Test
  107. void given2IdenticalAnd1DiffrentInputs_whenGetCharacterFrequency_thenPutInputInCacheTwice() {
  108. //CharCounter charCounter = Mockito.spy(new CharCounter());
  109. spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
  110. spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
  111. spyCharCounter.getCharacterFrequency(&quot;world!&quot;);
  112. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;hello world!&quot;);
  113. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;world!&quot;);
  114. }
  115. }```
  116. /*There&#39;s a CharCounter class, maybe there is some problems with static cache?
  117. */
  118. ```public class CharCounter {
  119. private static final CacheDecorator cache = new CacheDecorator(new LinkedHashMap&lt;&gt;());
  120. public Map&lt;String, Long&gt; getCharacterFrequency(String text) {
  121. Map&lt;String, Long&gt; characterFrequency;
  122. if (cache.containsKey(text)) {
  123. characterFrequency = cache.getData(text);
  124. } else {
  125. characterFrequency = countCharacters(text);
  126. cache.putData(text, characterFrequency);
  127. }
  128. return characterFrequency;
  129. }
  130. public Map&lt;String, Long&gt; countCharacters(String text) {
  131. Map&lt;String, Long&gt; frequencies = Arrays.stream(text.split(&quot;&quot;))
  132. .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
  133. return frequencies;
  134. }
  135. }```
  136. </details>
  137. # 答案1
  138. **得分**: 0
  139. 以下是您要翻译的代码部分:
  140. ```java
  141. 在静态缓存中出现了问题,如果您以这种方式编写测试,它将起作用
  142. @Test
  143. void given3IdenticalInputs_whenGetCharacterFrequency_thenPutInputInCacheOnce() {
  144. spyCharCounter.getCharacterFrequency("hello world!");
  145. spyCharCounter.getCharacterFrequency("hello world!");
  146. spyCharCounter.getCharacterFrequency("hello world!");
  147. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("hello world!");
  148. }
  149. @Test
  150. void given3DiffrentsInputs_whenGetCharacterFrequency_thenPutInputInCacheThrice() {
  151. spyCharCounter.getCharacterFrequency("my name is!");
  152. spyCharCounter.getCharacterFrequency("my!");
  153. spyCharCounter.getCharacterFrequency("name!");
  154. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("my name is!");
  155. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("my!");
  156. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("name!");
  157. }
  158. @Test
  159. void given2IdenticalAnd1DiffrentInputs_whenGetCharacterFrequency_thenPutInputInCacheTwice() {
  160. spyCharCounter.getCharacterFrequency("something!");
  161. spyCharCounter.getCharacterFrequency("something!");
  162. spyCharCounter.getCharacterFrequency("problem!");
  163. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("something!");
  164. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("problem!");
  165. }
英文:

There was a problem in the static cache, if you write tests this way, it will work

  1. @Test
  2. void given3IdenticalInputs_whenGetCharacterFrequency_thenPutInputInCacheOnce() {
  3. spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
  4. spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
  5. spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
  6. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;hello world!&quot;);
  7. }
  8. @Test
  9. void given3DiffrentsInputs_whenGetCharacterFrequency_thenPutInputInCacheThrice() {
  10. spyCharCounter.getCharacterFrequency(&quot;my name is!&quot;);
  11. spyCharCounter.getCharacterFrequency(&quot;my!&quot;);
  12. spyCharCounter.getCharacterFrequency(&quot;name!&quot;);
  13. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;my name is!&quot;);
  14. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;my!&quot;);
  15. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;name!&quot;);
  16. }
  17. @Test
  18. void
  19. given2IdenticalAnd1DiffrentInputs_whenGetCharacterFrequency_thenPutInputInCacheTwice() {
  20. spyCharCounter.getCharacterFrequency(&quot;something!&quot;);
  21. spyCharCounter.getCharacterFrequency(&quot;something!&quot;);
  22. spyCharCounter.getCharacterFrequency(&quot;problem!&quot;);
  23. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;something!&quot;);
  24. Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;problem!&quot;);
  25. }

huangapple
  • 本文由 发表于 2020年8月1日 23:00:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63206630.html
匿名

发表评论

匿名网友

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

确定