如何使用Arquillian测试ManagedBean?

时间:2016-09-13 09:42:58

标签: java junit jboss-arquillian

我所有的managedBean类都使用CDI。我有以下要测试的managedBean。如您所见,属性中有几个EJB和managedBean。我认为这是问题的根本原因。

package pa.cka.ge

@ViewScoped
@ManagedBean
public class UserBean{
   ...
   some properties
   ...

   @EJB
   private someDao dao1

   @EJB
   private anotherDao dao2

   ...

   @EJB
   private DaoNumerN daoN

   @ManagedProperty=(value="#{managedBeanName}")
   private ManagedBeanName mbn1;

   ...

   @ManagedProperty=(value="#{managedBeanNameN}")
   private ManagedBeanName mbnN;

   ...
   some methods
   ...

   ...
   getters and setters
   ...
}

我的想法是使用Arquillian来测试上面的类,因为它有几个方法,其变量在init()方法中初始化,如果我使用PowerMock,我应该在before()中复制init()文本,使测试过高加上实施。但是,有一些方法在没有托管容器的情况下很难调用(我应该嘲笑宇宙)。我的Arquillian类测试看起来像这样:

@RunWith(Arquillian.class)
public class DichiarazioneUserBeanTest {

    @ManagedProperty(value="#{dichiarazioneUserBean}") //also tried with @Inject
    private DeclarationUserBean dub;


    @Deployment
    public static JavaArchive createDeployment() {
        JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
                .addPackage(MyJPAEntity.class.getPackage())
                .addClasses(ManagadeBeanName1.class, dao1.class, dao2.class)
                .addClasses(dao3.class, dao4.class)
                .addClasses(dao5.class, ManagedBeanName2.class, ManagedBeanName3.class)
                .addClass(ManagedBeanName4.class)
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
        System.out.println(jar.toString(true));
        return jar;
    }

    public byte[] extractBytes (String ImageName) throws IOException {
        // open image
        File imgPath = new File(ImageName);
        BufferedImage bufferedImage = ImageIO.read(imgPath);

        // get DataBufferBytes from Raster
        WritableRaster raster = bufferedImage .getRaster();
        DataBufferByte data   = (DataBufferByte) raster.getDataBuffer();

        return ( data.getData() );
    }

    @Before
    public void setUp() throws IOException{
        dub.setProperty(null); //here I get java.lang.NullPointerException
    }

    @Test
    public void test(){

    }

}

我也尝试将@Inject表示法用于ManagedBean,并使用@EJB表示法用于EJB,但它不起作用

@RunWith(Arquillian.class)
public class DichiarazioneUserBeanTest {

    @Inject
    private DeclarationUserBean dub;

    ...

    @EJB
    private Dao dao1

    ....


    @Deployment
    public static JavaArchive createDeployment() {
        JavaArchive jar = ShrinkWrap.create(JavaArchive.class)
                .addPackage(MyJPAEntity.class.getPackage())
                .addClasses(ManagadeBeanName1.class, dao1.class, dao2.class)
                .addClasses(dao3.class, dao4.class)
                .addClasses(dao5.class, ManagedBeanName2.class, ManagedBeanName3.class)
                .addClass(ManagedBeanName4.class)
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
        System.out.println(jar.toString(true));
        return jar;
    }

    public byte[] extractBytes (String ImageName) throws IOException {
        // open image
        File imgPath = new File(ImageName);
        BufferedImage bufferedImage = ImageIO.read(imgPath);

        // get DataBufferBytes from Raster
        WritableRaster raster = bufferedImage .getRaster();
        DataBufferByte data   = (DataBufferByte) raster.getDataBuffer();

        return ( data.getData() );
    }

    @Before
    public void setUp() throws IOException{
        dub.setProperty(null); //here I get java.lang.NullPointerException
    }

    @Test
    public void test(){

    }

}

0 个答案:

没有答案