Arquillian JUnit测试不起作用

时间:2016-08-29 20:40:13

标签: java junit wildfly-8 jboss-arquillian

我正在尝试使用Arquillian运行JUnit测试,因为我的服务类注释为@Stateless但它不起作用...

@Deployment传递测试但@Test断言失败,注入服务的NullPointer异常:

@RunWith(Arquillian.class)
public class GenericDaoTest {
@Inject
private EmployeeService employeeService;

@Deployment
public static JavaArchive createTestableDeployment() {
    final JavaArchive jar = ShrinkWrap
            .create(JavaArchive.class)
            .addPackage("it.smartit.application.timesheet.service")
            .addPackage("it.smartit.application.timesheet.service.impl")
            .addAsManifestResource("META-INF/persistence.xml",
                    "persistence.xml")
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
            .addPackage("it.smartit.application.timesheet.entity");
    return jar;
}

@Test
public void should_crud() {
    assertNotNull(employeeService);
    Employee initialSize = employeeService.findById(new Integer(1));
}
}

注入的服务是:“视图类的代理:EJB的EmployeeService:mployeeServiceImpl”,当我尝试调用它返回的方法时:

我第一次使用dao但是我用jpa没用,所以现在服务我正在使用实体管理器但仍然无法工作:(

@Local
public interface GenericService<T, PK extends Serializable>{
    T findById(PK id);
}


@Stateless(name = "GenericServiceImpl", mappedName = "GenericServiceImpl")
public class GenericServiceImpl<T, PK extends Serializable> implements
    GenericService<T, PK> {

@PersistenceContext(unitName = "timesheet")
protected EntityManager entityManager;

private Class<T> entityClass;

public GenericServiceImpl() {
}

public GenericServiceImpl(Class<T> entityClass) {
    this.entityClass = entityClass;
}

@Override
public T findById(PK id) {
    return entityManager.find(entityClass, id);

}

}


public interface EmployeeService extends
    GenericService<Employee,Integer> {
}


@Stateless(name = "EmployeeServiceImpl", mappedName = "EmployeeServiceImpl") 
public class EmployeeServiceImpl extends GenericServiceImpl<Employee,Integer> implements EmployeeService{

public EmployeeServiceImpl() {
    super(Employee.class);
}

}

使用此资产会返回此错误:

javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:190)

....
Caused by: org.jboss.arquillian.test.spi.ArquillianProxyException: org.h2.jdbc.JdbcSQLException : Table "EMPLOYEES" not found; SQL statement:

选择employee0_.employeed_id为employee1_3_0_,employee0_.address as ....

我在Wildfly8和Mysql上使用Java EE 7

1 个答案:

答案 0 :(得分:0)

我认为这个问题的原因是数据库配置定义。

我的第一个猜测是你没有在你的persistence.xml中设置你的hibernate.hbm2ddl.auto值来创建或创建drop:如下所示:

<property name="hibernate.hbm2ddl.auto" value="create-drop" />

这一行告诉hibernate(在这种情况下)在启动时创建数据库表并在关闭时删除它们。如果您没有添加此行,则表格可能不会存在于数据库中。

如果这不是问题,那么在添加Employee类时会有所帮助。