I have a drools rule file which uses service classes in the rules. So one rule does something like this:
eval(countryService.getCountryById(1) != null)
In a validationservice that is annotated with @service and @Transactional(propagation=Propagation.SUPPORTS) the drools file is used in a statelessKnowledgebase and facts are added that should be used in the drool. Once that is done the session.execute(facts) is invoked and the rule engine starts.
In order to test the rules I would like to stub the countryService.getCountryById(). No big problem using mockito. Done this for other service that use a drools setup as well and it worked fine. However in this particular case the countryService was not stubbed and I couldn't figure out why. After spending a lot of time and checking my code I found that having @Transactional above the service or lacking this annotation made the difference. Lacking the @Transaction made mockito mock the countryservice without any problem, having the @transactional in place caused mockito to fail (without any error or hint) injecting the mock so the original countryservice object was used.
My question is why this annotation causes this problem. Why can't mockito inject the mocks when @Transactional is set? I've noticed that mockito is failing as when I debug and inspect the countryService when it is being added as global to the drools session I see the following difference when I inspect the countryservice in my debugwindow:
with @transactional: countryService has the value CountryService$$EnhancerByCGLIB$$b80dbb7b
without @transactional:countryService has the value CountryService$$EnhancerByMockitoWithCGLIB$$27f34dc1
In addition with @transactional my breakpoint in the countryservice methode getCountryById is found and the debugger stops at that breakpoint, but without the @transactional my breakpoint is skipped as mockito bypasses it.
ValidationService:
@Service
@Transactional(propagation=Propagation.SUPPORTS)
public class ValidationService
{
@Autowired
private CountryService countryService;
public void validateFields(Collection<Object> facts)
{
KnowledgeBase knowledgeBase = (KnowledgeBase)AppContext.getApplicationContext().getBean(knowledgeBaseName);
StatelessKnowledgeSession session = knowledgeBase.newStatelessKnowledgeSession();
session.setGlobal("countryService", countryService);
session.execute(facts);
}
And the test class:
public class TestForeignAddressPostalCode extends BaseTestDomainIntegration
{
private final Collection<Object> postalCodeMinLength0 = new ArrayList<Object>();
@Mock
protected CountryService countryService;
@InjectMocks
private ValidationService level2ValidationService;
@BeforeMethod(alwaysRun=true)
protected void setup()
{
// Get the object under test (here the determination engine)
level2ValidationService = (ValidationService) getAppContext().getBean("validationService");
// and replace the services as documented above.
MockitoAnnotations.initMocks(this);
ForeignAddress foreignAddress = new ForeignAddress();
foreignAddress.setCountryCode("7029");
foreignAddress.setForeignPostalCode("foreign");
// mock country to be able to return a fixed id
Country country = mock(Country.class);
foreignAddress.setLand(country);
doReturn(Integer.valueOf(1)).when(country).getId();
doReturn(country).when(countryService).getCountryById(anyInt());
ContextualAddressBean context = new ContextualAddressBean(foreignAddress, "", AddressContext.CORRESPONDENCE_ADDRESS);
postalCodeMinLength0.add(context);
}
@Test
public void PostalCodeMinLength0_ExpectError()
{
// Execute
level2ValidationService.validateFields(postalCodeMinLength0, null);
}
Any idea what to do if I want to keep this @transactional annotation but also be able to stub the countryservice methodes?
regards,
Michael
Based on the answer of SuperSaiyen, I created an drop-in utility class to make it simpler & type safe:
import org.mockito.Mockito;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.test.util.ReflectionTestUtils;
@SuppressWarnings("unchecked")
public class SpringBeanMockUtil {
/**
* If the given object is a proxy, set the return value as the object being proxied, otherwise return the given
* object.
*/
private static <T> T unwrapProxy(T bean) {
try {
if (AopUtils.isAopProxy(bean) && bean instanceof Advised) {
Advised advised = (Advised) bean;
bean = (T) advised.getTargetSource().getTarget();
}
return bean;
}
catch (Exception e) {
throw new RuntimeException("Could not unwrap proxy!", e);
}
}
public static <T> T mockFieldOnBean(Object beanToInjectMock, Class<T> classToMock) {
T mocked = Mockito.mock(classToMock);
ReflectionTestUtils.setField(unwrapProxy(beanToInjectMock), null, mocked, classToMock);
return mocked;
}
}
Usage is simple, just on the beginning of your test method, call the method mockFieldOnBean(Object beanToInjectMock, Class<T> classToMock)
with the bean that you want to inject a mock on, and the class of the object that should be mocked. Example:
Let's say you have a bean with type SomeService
which holds an autowired bean of SomeOtherService
, something like;
@Component
public class SomeService {
@Autowired
private SomeOtherService someOtherService;
// some other stuff
}
To mock someOtherService
on the SomeService
bean, use the following:
@RunWith(SpringJUnit4ClassRunner.class)
public class TestClass {
@Autowired
private SomeService someService;
@Test
public void sampleTest() throws Exception {
SomeOtherService someOtherServiceMock = SpringBeanMockUtil.mockFieldOnBean(someService, SomeOtherService.class);
doNothing().when(someOtherServiceMock).someMethod();
// some test method(s)
verify(someOtherServiceMock).someMethod();
}
}
everything should work as they should.
Please note that since Spring 4.3.1, ReflectionTestUtils should automatically unwrap proxies. So
ReflectionTestUtils.setField(validationService, "countryService", countryService);
should now work even if your countryService
is annotated with @Transactional
, @Cacheable
... (that is, hidden behind a proxy at runtime)
Related issue: SPR-14050
Whats happening is your ValidationService is being wrapped in a JdkDynamicAopProxy, so when Mockito goes to inject the mocks into the service it does not see any fields to inject them into. You'll need to do one of two things:
Code Example:
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
ValidationService validationService = (ValidationService) unwrapProxy(level2ValidationService);
ReflectionTestUtils.setField(validationService, "countryService", countryService);
}
public static final Object unwrapProxy(Object bean) throws Exception {
/*
* If the given object is a proxy, set the return value as the object
* being proxied, otherwise return the given object.
*/
if (AopUtils.isAopProxy(bean) && bean instanceof Advised) {
Advised advised = (Advised) bean;
bean = advised.getTargetSource().getTarget();
}
return bean;
}
Blog entry on the issue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With