CDI unit test with ELContext and ExpressionFactory
-
09-12-2019 - |
Question
I'd like to test some CDI classes.
Today, I'm using Arquillian to do that like that :
@RunWith(Arquillian.class)
public class MyCDIBeanTest {
@Deployment
public static JavaArchive createTestArchive() {
return ShrinkWrap
.create(JavaArchive.class, "test.jar")
.addClasses(MyCDIBean.class)
.addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"));
}
...
Here is my current pom.xml
:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<version>1.0.0.CR6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-test-spi</artifactId>
<version>1.0.0.CR6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-spi</artifactId>
<version>1.0.0.CR6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
<version>1.0.0.CR3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
<version>1.0.0.CR3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-api</artifactId>
<version>1.1.2.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-spi</artifactId>
<version>1.1.2.Final</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
The versions are in the parent pom and weld
and slf4j
versions are imported with
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core-bom</artifactId>
<version>1.1.2.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Until now, all has been runnig well.
Today, I want to test some classes which use javax.el.ElContext
and javax.el.ExpressionFactory
.
They use Seam Solder to @Inject
them.
So I added .addPackage(org.jboss.solder.el.Expressions.class.getPackage())
to ShrinkWrap
.
But now here is what I got :
javax.el.ELException: Provider org.apache.el.ExpressionFactoryImpl not found
at javax.el.FactoryFinder.newInstance(FactoryFinder.java:97)
at javax.el.FactoryFinder.find(FactoryFinder.java:193)
at javax.el.ExpressionFactory.newInstance(ExpressionFactory.java:185)
at javax.el.ExpressionFactory.newInstance(ExpressionFactory.java:156)
at org.jboss.solder.el.ExpressionFactoryProducer.createExpressionFactory(ExpressionFactoryProducer.java:35)
Does anybody knows how could I do that better ? (or just working)
Solution
OK here is what I did (inspired by that question).
First I deleted the .addPackage(Expressions.class.getPackage())
.
Then I added a dependency to Juel :
<dependency>
<!-- Used to simulate El expressions without Faces environment -->
<groupId>de.odysseus.juel</groupId>
<artifactId>juel-impl</artifactId>
<version>2.2.4</version>
<scope>test</scope>
</dependency>
Then in my test I added that code :
/// Methods & variables for the elContext ///
static SimpleContext elContext = new SimpleContext();
static ExpressionFactory expressionFactory = new ExpressionFactoryImpl();
@Produces @RequestScoped
public static ELContext getElContext() {
return elContext;
}
@Produces @RequestScoped
public static ExpressionFactory getExpressionFactory() {
return expressionFactory;
}
@Before
public void reInitElContext() {
elContext = new SimpleContext();
}
private void setElVariable(final String variableName, final Object variableValue) {
setElVariable(variableName, variableValue, variableValue.getClass());
}
private void setElVariable(final String variableName, final Object variableValue, final Class<? extends Object> variableClass) {
expressionFactory.createValueExpression(elContext, "#{"+variableName+"}", variableClass).setValue(elContext, variableValue);
}
private void setElNullVariable(final String variableName, final Class<?> variableClass) {
setElVariable(variableName, null, variableClass);
}
/**
* By default, set a null String variable
* @param variableName
*/
private void setElNullVariable(final String variableName) {
setElNullVariable(variableName, String.class);
}
Now I can use setElVariable("variableName", "value")
in my tests !