以下代码段足以重现我的问题:
thrown
属性public
并获取错误org.jboss.weld.exceptions.DefinitionException: WELD-000075: Normal scoped managed bean implementation class has a public field
public
修饰符并获取错误org.junit.internal.runners.rules.ValidationError: The @Rule 'thrown' must be public.
public
修饰符到位并@Dependent
在类上添加注释范围,但是出现了错误org.jboss.weld.exceptions.DefinitionException: WELD-000046: At most one scope may be specified on [EnhancedAnnotatedTypeImpl] public @Dependent @ApplicationScoped @RunWith
我删除了所有不必要的代码,但这是一个非常复杂的单元测试,通过CDI进行模拟,服务注入,并且一些测试方法预计会引发异常.
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() {
}
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,一方面Weld希望所有字段都不公开,因为否则它将无法代理该类,另一方面,JUnit希望规则字段是公共的,因为它使用反射来访问它们setAccessible(true)
由于安全管理器处于活动状态,因此不希望使用该方法.如何处理这个悖论? …
这是单元测试代码.当我们运行单元测试代码(SampleServiceTest2)时; 在AbstractDao中注入的EntityManager始终为null!我们如何在单元测试期间注入em.
***SampleServiceTest2.java
import javax.inject.Inject;
import org.jglue.cdiunit.CdiRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(CdiRunner.class)
public class SampleServiceTest2 {
@Inject SampleService greeter;
@Test
public void testGreeter() throws Exception {
System.out.println("before2");
greeter.addSampleData(new SampleDataDto(), new KullaniciDto());
System.out.println("after2");
}
}
Run Code Online (Sandbox Code Playgroud)
***SampleService.java
import javax.ejb.Stateless;
import javax.inject.Inject;
....
@Stateless
@SecuredBean
public class SampleService {
@Inject
SampleLogic sampleLogic;
@Yetki(tag="perm_add_sample_data")
public void addSampleData(SampleDataDto data, KullaniciDto aktifKullaniciDto){
SampleDataHelper sampleDataHelper = new SampleDataHelper();
SampleData sampleData = sampleDataHelper.getEntity(data);
KullaniciHelper kullaniciHelper = new KullaniciHelper();
Kullanici kullanici = kullaniciHelper.getEntity(aktifKullaniciDto);
sampleLogic.addData(sampleData, kullanici);
}
}
Run Code Online (Sandbox Code Playgroud)
****SampleLogic.java
import …
Run Code Online (Sandbox Code Playgroud)