2016-08-29 48 views
0

我試圖運行具有的Arquillian JUnit測試的註解@Stateless我的服務類,但它不工作...的Arquillian JUnit測試不起作用

的@Deployment通過測試,但@Test斷言失敗,出現空指針異常的注射服務:

@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」當我嘗試調用它返回的方法:首先

我使用道,但我與jpa沒有用,所以現在在servi CE我使用實體管理器,但仍然沒有工作:(

@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作爲....

我在Wildfly8和MySQL

+0

你能分享你的依賴嗎? –

+0

發佈更新謝謝@ bartosz.majsak – antonio

回答

0

使用Java EE 7我認爲,這個問題的原因是數據庫配置定義。

我的第一個猜測是,你沒有設置你的hibernate.hbm2ddl.auto創造價值或創造滴在你的persistence.xml這樣的:

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

這行告訴冬眠(以這種情況下)在啓動時創建數據庫表並在關閉時刪除它們。如果您不添加此行,則可能表中的數據庫不存在。

如果這不是問題,那麼如果您添加了您的Employee類將會很有幫助。