Dependency injection does not work in Arquillian Test

杀马特。学长 韩版系。学妹 提交于 2019-12-24 15:22:59

问题


This is test class: I try to inject a Paper object then perform the action to test whether the injection is failed or not.

@RunWith(Arquillian.class)
public class ExcelProcessorTest {
    // Not work. Because Paper and ExcelProcessorTest are in different modules?
    @Inject
    private Paper paper;

    @Deployment
    public static JavaArchive createDeployment() {
        JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "test.jar")
                .addClasses(Paper.class)
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
        System.out.println(jar.toString(true));

        return jar;
    }

    @Test
    public void notNullTest() {
        Assert.assertNotNull(paper); // paper is null here.
    }

}

I have been working on a project named ftc and this is the structure of ftc:

Note that ftc is the parent module of ftc-*. This project is organized by Maven convention.

ExcelProcessorTest class is located in ftc-test module:

And Paper class is located in ftc-ejb module:

Paper class is a simple entity bean:

/**
 * Paper generated by hbm2java
 */
@Entity
@Table(name = "paper")
public class Paper implements java.io.Serializable {

    private Integer id;
    private String title;
    private String author;
    private String journal;
    private String volumn;
    private String pages;
    private String alternateJournal;
    private String issn;
    private String doi;
    private String acccessionNumber;
    private String keywords;
    private String abstract_;
    private Integer timesCited;
    private Integer citedReferenceCount;
    private String citedReferenceName;
    private String website;
    private String pdfPath;
    private String issue;
    private Integer paperIndexId;
    private Integer year; // 论文发表的年份
    private String type; // OVERVIEW, EXCEL, PAPER
    private SourceFile sourceFile;
    private Set<PaperIndex> paperIndexes = new HashSet<PaperIndex>(0);
    private Set<Sentence> sentences = new HashSet<Sentence>(0);

    public Paper() {
    }

Could anyone please tell me why the injection was failed?

Oh.. I'v got something to do now. I'll be back here in a few hours. If you need anything detail please comment bellow. Thanks.


回答1:


I have figured this out. In Paper class, I have used some other classes, such as Sentence.class, PaperIndex.class, but I did not add them in addClasses() method. This referred classse can not be found so the injection failed.

How stupid am I...




回答2:


I was stumbling upon the same thing, for me using @EJB instead of @Inject worked out perfectly... just to provide a hint but no explanation why.



来源:https://stackoverflow.com/questions/21456080/dependency-injection-does-not-work-in-arquillian-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!