I have a couple of tests failing in my test class when running them all together. When I run them individually, they all work well. So there must be some conflict in the mocked value which I reuse in multiple tests.
Here is example:
@ExtendWith(SpringExtension::class)
@AutoConfigureMockMvc(addFilters = false)
class BerryStorageTest {
private lateinit var berryStorage: BerryStorage
private lateinit var dataB: DataB
@BeforeEach
fun setup() {
dataK = mock()
whenever(dataB.berries).thenReturn(ConcurrentHashMap())
this.berryStorage = BerryStorage(dataB)
}
@Test
fun `when adding additional berry to berries storage, number of berries increased`() {
// reset(dataB)
// dataB = mock()
whenever(dataB.berries).thenReturn(berriesMapMock)
berryStorage.addBerry(additionalBerryMock)
val resultBerries = dataB.berries
assertThat(resultBerries.size).isEqualTo(2)
}
// Some other tests...
}
What am I missing? I can clearly see through debugging that when the test starts there are 2 berries inside the mocked value instead of 1 value as it is initialized in berriesMapMock. So assert fails because as the result there are 3 berries overall, instead of 2. And one of them is coming from another unit test...
I have the @BeforeEach method to reinitialize the mock, but the problem persists for a couple of my tests.