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

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

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?

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.*;
class CharCounterTest {
private CharCounter charCounter;
private CharCounter spyCharCounter;
@BeforeEach
public void setUp() {
charCounter = new CharCounter();
spyCharCounter = Mockito.spy(charCounter);
}
@AfterEach
public void cleanUp(){
Mockito.reset(spyCharCounter);
}
@Test
void given3IdenticalInputs_whenGetCharacterFrequency_thenPutInputInCacheOnce() {
//CharCounter charCounter = Mockito.spy(new CharCounter());
spyCharCounter.getCharacterFrequency("hello world!");
spyCharCounter.getCharacterFrequency("hello world!");
spyCharCounter.getCharacterFrequency("hello world!");
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("hello world!");
}
@Test
void given3DifferentInputs_whenGetCharacterFrequency_thenPutInputInCacheThrice() {
//CharCounter charCounter = Mockito.spy(new CharCounter());
spyCharCounter.getCharacterFrequency("hello world!");
spyCharCounter.getCharacterFrequency("hello!");
spyCharCounter.getCharacterFrequency("world!");
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("hello world!");
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("hello!");
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("world!");
}
@Test
void given2IdenticalAnd1DifferentInputs_whenGetCharacterFrequency_thenPutInputInCacheTwice() {
//CharCounter charCounter = Mockito.spy(new CharCounter());
spyCharCounter.getCharacterFrequency("hello world!");
spyCharCounter.getCharacterFrequency("hello world!");
spyCharCounter.getCharacterFrequency("world!");
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("hello world!");
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("world!");
}
}```
/*There's a CharCounter class, maybe there is some problems with static cache?
*/
```public class CharCounter {
private static final CacheDecorator cache = new CacheDecorator(new LinkedHashMap<>());
public Map<String, Long> getCharacterFrequency(String text) {
Map<String, Long> characterFrequency;
if (cache.containsKey(text)) {
characterFrequency = cache.getData(text);
} else {
characterFrequency = countCharacters(text);
cache.putData(text, characterFrequency);
}
return characterFrequency;
}
public Map<String, Long> countCharacters(String text) {
Map<String, Long> frequencies = Arrays.stream(text.split(""))
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
return frequencies;
}
}```
<details>
<summary>英文:</summary>
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.
Is there any problems with static cache?
```import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import static org.junit.jupiter.api.Assertions.*;
class CharCounterTest {
private CharCounter charCounter;
private CharCounter spyCharCounter;
@BeforeEach
public void setUp() {
charCounter = new CharCounter();
spyCharCounter = Mockito.spy(charCounter);
}
@AfterEach
public void cleanUp(){
Mockito.reset(spyCharCounter);
}
@Test
void given3IdenticalInputs_whenGetCharacterFrequency_thenPutInputInCacheOnce() {
//CharCounter charCounter = Mockito.spy(new CharCounter());
spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;hello world!&quot;);
}
@Test
void given3DiffrentsInputs_whenGetCharacterFrequency_thenPutInputInCacheThrice() {
//CharCounter charCounter = Mockito.spy(new CharCounter());
spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
spyCharCounter.getCharacterFrequency(&quot;hello!&quot;);
spyCharCounter.getCharacterFrequency(&quot;world!&quot;);
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;hello world!&quot;);
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;hello!&quot;);
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;world!&quot;);
}
@Test
void given2IdenticalAnd1DiffrentInputs_whenGetCharacterFrequency_thenPutInputInCacheTwice() {
//CharCounter charCounter = Mockito.spy(new CharCounter());
spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
spyCharCounter.getCharacterFrequency(&quot;world!&quot;);
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;hello world!&quot;);
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;world!&quot;);
}
}```
/*There&#39;s a CharCounter class, maybe there is some problems with static cache?
*/
```public class CharCounter {
private static final CacheDecorator cache = new CacheDecorator(new LinkedHashMap&lt;&gt;());
public Map&lt;String, Long&gt; getCharacterFrequency(String text) {
Map&lt;String, Long&gt; characterFrequency;
if (cache.containsKey(text)) {
characterFrequency = cache.getData(text);
} else {
characterFrequency = countCharacters(text);
cache.putData(text, characterFrequency);
}
return characterFrequency;
}
public Map&lt;String, Long&gt; countCharacters(String text) {
Map&lt;String, Long&gt; frequencies = Arrays.stream(text.split(&quot;&quot;))
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()));
return frequencies;
}
}```
</details>
# 答案1
**得分**: 0
以下是您要翻译的代码部分:
```java
在静态缓存中出现了问题,如果您以这种方式编写测试,它将起作用
@Test
void given3IdenticalInputs_whenGetCharacterFrequency_thenPutInputInCacheOnce() {
spyCharCounter.getCharacterFrequency("hello world!");
spyCharCounter.getCharacterFrequency("hello world!");
spyCharCounter.getCharacterFrequency("hello world!");
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("hello world!");
}
@Test
void given3DiffrentsInputs_whenGetCharacterFrequency_thenPutInputInCacheThrice() {
spyCharCounter.getCharacterFrequency("my name is!");
spyCharCounter.getCharacterFrequency("my!");
spyCharCounter.getCharacterFrequency("name!");
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("my name is!");
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("my!");
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("name!");
}
@Test
void given2IdenticalAnd1DiffrentInputs_whenGetCharacterFrequency_thenPutInputInCacheTwice() {
spyCharCounter.getCharacterFrequency("something!");
spyCharCounter.getCharacterFrequency("something!");
spyCharCounter.getCharacterFrequency("problem!");
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("something!");
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters("problem!");
}
英文:

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

@Test
void given3IdenticalInputs_whenGetCharacterFrequency_thenPutInputInCacheOnce() {
spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
spyCharCounter.getCharacterFrequency(&quot;hello world!&quot;);
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;hello world!&quot;);
}
@Test
void given3DiffrentsInputs_whenGetCharacterFrequency_thenPutInputInCacheThrice() {
spyCharCounter.getCharacterFrequency(&quot;my name is!&quot;);
spyCharCounter.getCharacterFrequency(&quot;my!&quot;);
spyCharCounter.getCharacterFrequency(&quot;name!&quot;);
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;my name is!&quot;);
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;my!&quot;);
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;name!&quot;);
}
@Test
void 
given2IdenticalAnd1DiffrentInputs_whenGetCharacterFrequency_thenPutInputInCacheTwice() {
spyCharCounter.getCharacterFrequency(&quot;something!&quot;);
spyCharCounter.getCharacterFrequency(&quot;something!&quot;);
spyCharCounter.getCharacterFrequency(&quot;problem!&quot;);
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;something!&quot;);
Mockito.verify(spyCharCounter, Mockito.times(1)).countCharacters(&quot;problem!&quot;);
}

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:

确定