Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

spring - Transactional annotation avoids services being mocked

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...