Mantis - Resin
Viewing Issue Advanced Details
4501 minor always 04-13-11 12:03 04-26-11 17:48
reza  
ferg  
normal  
closed 4.0.17  
fixed  
none    
none 4.0.18  
0004501: Specifying @Alternatives in beans.xml overrides do not work
Specifying alternatives in beans.xml overrides do not work. For example, the following code will cause "hit()" instead of "mock-hit()" to be printed out:

================================================================================
package qa;

public interface Test {
  public void hit();
}
================================================================================

================================================================================
package qa;

public class TestBean implements Test {
  public void hit() {
    System.out.println("hit()");
  }
}
================================================================================

================================================================================
<beans xmlns="http://java.sun.com/xml/ns/javaee"> [^]
  <alternatives>
    <class>qa.MockTestBean</class>
  </alternatives>
</beans>
================================================================================

================================================================================
package qa;

import javax.enterprise.inject.Alternative;

@Alternative
public class MockTestBean implements Test {
  public void hit() {
    System.out.println("mock-hit()");
  }
}
================================================================================

================================================================================
package qa;

import javax.inject.Inject;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.caucho.junit.ResinBeanConfiguration;
import com.caucho.junit.ResinBeanContainerRunner;

@RunWith(ResinBeanContainerRunner.class)
@ResinBeanConfiguration(beansXml = { "classpath:META-INF/beans.xml" })
public class JUnitTest {
  @Inject
  private qa.Test bean;

  @Test
  public void test() {
    bean.hit();
  }
}
================================================================================

This definitely appears to be a timing issue in that the underlying CDI context gets processed/bootstrapped before the beans.xml overrides are processed. For example, the following will work because no XML override is specified although the exact same META-INF/beans.xml will be loaded (via the default bootstrap as opposed to an override):

================================================================================
package qa;

import javax.inject.Inject;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.caucho.junit.ResinBeanContainerRunner;

@RunWith(ResinBeanContainerRunner.class)
public class JUnitTest {
  @Inject
  private qa.Test bean;

  @Test
  public void test() {
    bean.hit();
  }
}
================================================================================

This is a pretty big problem since most developers are likely to use alternatives for mock objects in unit tests used through XML overrides rather than in production using the default beans.xml.

Notes
(0005208)
ferg   
04-26-11 17:48   
ioc/5012, ioc/5013