I have a Producers class annotated with @Singleton
, containing a method annotated with @Produces
.
I want to write unit test using an alternative of this method, but can't manage to do it. here's a summary of my code :
package fr.easycompany.easywrite.tools.injection;
@Singleton
public class Producers {
@Produces @Named(PREFERENCES_FILE_NAMED)
public String producePreferenceFileName(){
return "preferences.xml";
}
}
And my Alternative :
package fr.easycompany.easywrite.tools.injection;
@Singleton
@Alternative
public class ProducersAlternative {
@Produces @Named(PREFERENCES_FILE_NAMED)
public String producePreferenceFileName(){
return "preferences_test.xml";
}
}
I also created a beans.xml in src/test/resources/META-INF with the following content
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:weld="http://jboss.org/schema/weld/beans"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd
http://jboss.org/schema/weld/beans http://jboss.org/schema/weld/beans_1_1.xsd">
<alternatives>
<class>fr.easycompany.easywrite.tools.injection.ProducersAlternative</class>
</alternatives>
</beans>
At the execution, it's always Producers#producePreferenceFileName()
which is called. Why is it not ProducersAlternative
's method ? Is it impossible to have an alternative of a singleton injected class ?