问题
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