Mockito 测试单独工作,但当 运行 在一起时,只有一个工作
Mockito tests work separately but when run together only one works
我遇到了一个奇怪的问题。我的测试用例有两个失败的测试,第一个和第三个。但是,如果我单独 运行 相同,它 运行 就完美了。我是 JUnit 的新手,不知道为什么会这样。我试图重新模拟并在 setUp() 方法中初始化它们,但它没有帮助。
静态缓存有问题吗?
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 given3DiffrentsInputs_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 given2IdenticalAnd1DiffrentInputs_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;
}
}```
静态缓存有问题,这样写测试就可以了
@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!");
}
我遇到了一个奇怪的问题。我的测试用例有两个失败的测试,第一个和第三个。但是,如果我单独 运行 相同,它 运行 就完美了。我是 JUnit 的新手,不知道为什么会这样。我试图重新模拟并在 setUp() 方法中初始化它们,但它没有帮助。 静态缓存有问题吗?
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 given3DiffrentsInputs_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 given2IdenticalAnd1DiffrentInputs_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;
}
}```
静态缓存有问题,这样写测试就可以了
@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!");
}