Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@SpyBean with few Integration tests doesn't work correctly

I have a strange behavior of @SpyBean field in my integration tests. For example, I have a few integration tests:

package a;

@SpringBootTest
public class A {
 @SpyBean
 public MySpyBeanCandidate spyBean;

 @Test
 public void test1 {
   // some work
   Mockito.verify(spyBean, Mockito.atLeastOnce()).methodName(eq("String value"), anyString());
 }

}
package a;

@SpringBootTest
public class B {
 @SpyBean
 public MySpyBeanCandidate spyBean;

 @Test
 public void test2 {
   // some work
   Mockito.verify(spyBean, Mockito.atLeastOnce()).methodName(eq("String value"), anyString());
 }

}

The problem is when I try to execute them separately they executed successfully, but if I'll run them together, in the second test Mockito.verify(..) will throw an exception: Wanted but not invoked. But I have debugged it and checked that method (methodName) called correctly. Who knows why this is happening?

like image 214
Danzis Avatar asked Oct 14 '25 14:10

Danzis


1 Answers

@DirtiesContext on each @Test method for such cases worked for me.

like image 58
Artyom Avatar answered Oct 18 '25 08:10

Artyom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!