Note that there are some explanatory texts on larger screens.

plurals
  1. POTransactional annotation avoids services being mocked
    primarykey
    data
    text
    <p>I have a drools rule file which uses service classes in the rules. So one rule does something like this:</p> <p>eval(countryService.getCountryById(1) != null)</p> <p>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.</p> <p>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.</p> <p>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:</p> <ul> <li><p>with @transactional: countryService has the value CountryService$$EnhancerByCGLIB$$b80dbb7b</p></li> <li><p>without @transactional:countryService has the value CountryService$$EnhancerByMockitoWithCGLIB$$27f34dc1</p></li> </ul> <p>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. </p> <p>ValidationService:</p> <pre><code>@Service @Transactional(propagation=Propagation.SUPPORTS) public class ValidationService { @Autowired private CountryService countryService; public void validateFields(Collection&lt;Object&gt; facts) { KnowledgeBase knowledgeBase = (KnowledgeBase)AppContext.getApplicationContext().getBean(knowledgeBaseName); StatelessKnowledgeSession session = knowledgeBase.newStatelessKnowledgeSession(); session.setGlobal("countryService", countryService); session.execute(facts); } </code></pre> <p>And the test class:</p> <pre><code>public class TestForeignAddressPostalCode extends BaseTestDomainIntegration { private final Collection&lt;Object&gt; postalCodeMinLength0 = new ArrayList&lt;Object&gt;(); @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); } </code></pre> <p>Any idea what to do if I want to keep this @transactional annotation but also be able to stub the countryservice methodes?</p> <p>regards,</p> <p>Michael</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload