由于公共私人领域的矛盾,使用Junit @Rule进行CdiUnit测试是不可能的 [英] CdiUnit test with Junit @Rule is impossible because of a public private field paradox
问题描述
以下代码段足以重现我的问题:
The following snippet is enough to reproduce my problem:
- 要么设置
抛出
属性public
并获取错误org.jboss.weld.exceptions.DefinitionException:WELD-000075:正常范围的托管bean实现类有一个公共字段
- 或者我删除
public
修饰符并获取错误org .junit.internal.runners.rules.ValidationError:@Rule'抛出'必须公开。
- 我还试图让
public
修饰符并在类上添加@Dependent
注释范围,但得到错误org.jboss。 weld.exceptions.DefinitionException:WELD-000046:[EnhancedAnnotatedTypeImpl]上最多可以指定一个范围public @Dependent @ApplicationScoped @RunWith
- Either I set the
thrown
attributepublic
and get the errororg.jboss.weld.exceptions.DefinitionException: WELD-000075: Normal scoped managed bean implementation class has a public field
- Or I remove the
public
modifier and get the errororg.junit.internal.runners.rules.ValidationError: The @Rule 'thrown' must be public.
- I also tried to let the
public
modifier in place and to add the@Dependent
annotation scope on the class, but got errororg.jboss.weld.exceptions.DefinitionException: WELD-000046: At most one scope may be specified on [EnhancedAnnotatedTypeImpl] public @Dependent @ApplicationScoped @RunWith
我删除了所有不必要的代码,但这是一个非常复杂的单元测试,通过CDI进行模拟,服务注入和一些测试方法thar应该抛出异常。
I stripped out all unnecessary code, but this is quite a complex unit test with mock, service injection through CDI and some test methods thar are expected to throw an exception.
import org.jglue.cdiunit.CdiRunner;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
@RunWith(CdiRunner.class)
public class FooBarTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void test() {
}
}
所以我的问题是,一方面Weld希望所有字段都不公开,否则它将无法代理该类,另一方面,JUnit需要规则要公开的字段,因为它使用反射来访问它们,并且不希望使用 setAccessible(true)
方法,因为安全管理器处于活动状态。如何处理这个悖论?
So my problem is that on one hand Weld wants all fields to not be public because it will not be able to proxify the class otherwise, and on the other hand, JUnit want Rule fields to be public because it is using reflection to access them and does not want to use the setAccessible(true)
method because of the cases where the security manager is active. How to deal with that paradox?
注意:我还发现了对这个答案,说明
NB: I also found a hint comments to this answer, stating that
您还可以使用@Rule注释方法,这样可以避免问题
You can also annotate a method with @Rule, so this would avoid the problem
但我找不到任何带有 @Rule
注释的junit测试的例子一个方法,我打算问一个单独的问题。
But I could not found any example of junit test with a @Rule
annotation on a method, I plan to ask a separate question about this.
推荐答案
我发现了如何解决这个问题。为了将来参考,这里有一个有用的片段,希望这会有助于其他人。
I found out how to solve the problem. For future reference, here is a snippet that works, hope this will help other people.
import org.jglue.cdiunit.CdiRunner;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
@RunWith(CdiRunner.class)
public class FooBarTest {
private ExpectedException thrown = ExpectedException.none();
@Rule
public ExpectedException getThrown() {
return thrown;
}
@Test
public void test() {
thrown.expect(ArithmeticException.class);
int i = 1 / 0;
}
}
这篇关于由于公共私人领域的矛盾,使用Junit @Rule进行CdiUnit测试是不可能的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!