Note that there are some explanatory texts on larger screens.

plurals
  1. PO@Transactional causing @Inject fields to change
    primarykey
    data
    text
    <p>I'm running into a very strange problem with a jUnit test and bean proxies. As part of my test, I am manually adding beans to the application context, and forcing a bean re-wire using <code>AutowireCapableBeanFactory.autowire()</code> to force an autowire update.</p> <p>If I place a breakpoint in the jUnit test and inspect the bean I am autowire updating, I can see my my field has been properly updated and includes the new beans I programatically added to the context.</p> <p>However, when the execution moves to the actual bean and I place a breakpoint in the method I am testing, the beans I added to the context do not appear.</p> <p>My Bean-under-test method is annotated with <code>@Transactional</code>. If I remove the <code>@Transactional</code> annotation, the autowire field is properly updated.</p> <p>I have tried adding @Transaction to my jUnit test method, but it has had no impact.</p> <p>Junit (implements BeanDefinitionRegistryPostProcessor):</p> <pre><code>@Test @Rollback @Transactional public void testExecuteSinglePatch() throws Exception { GenericBeanDefinition definition = new GenericBeanDefinition(); definition.setBeanClass(Patch1.class); definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE); definition.setLazyInit(false); registry.registerBeanDefinition("PATCH_1", definition); applicationContext.getBeansOfType(Patch.class); // reinit the patchEngine autowireBeanFactory.autowireBean(patchEngine); patchEngine.execute(); assertEquals(1, patchRepository.findByName( new Patch1().getName()).size() ); } </code></pre> <p>Bean Under Test (PatchEngine):</p> <pre><code>@Inject private Map&lt;String, Patch&gt; availablePatches; @Transactional public void execute() throws Exception{ // loop over all the pending patches, and apply them. Keep looping over while patches exist and at least one patch is applied per iteration boolean appliedPatch = true; while( !availablePatches.isEmpty() &amp;&amp; appliedPatch ){ // no applied patches this iteration yet appliedPatch = false; // do some logic here } } </code></pre> <p>When I set my debug context to the jUnit test and examine <code>patchEngine</code>, the bean id for <code>patchEngine.availablePatches</code> is different than the bean id found when I set my debug context to the patchEngine.execute method. </p> <p>However, if I remove the <code>@transactional</code> annotation from the <code>execute()</code> method, then the ids are the same.</p> <p>Can anyone help me understand what is happening? Why is <code>@Transactional</code> impacting the dependency injection? How does the @Transactional impact the proxy and how to ensure that proxy is properly populated? Does the annotation cause a different context to be used? Is there a way to solve this problem?</p> <p>I'm using Spring 3.2.4.RELEASE and jUnit 4.11. Could this be a Spring bug?</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.
    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