简体   繁体   中英

Arquillian JUnit test doesn't work

I'm trying to run JUnit test with Arquillian for my service classes annotated as @Stateless but it doesn't work...

The @Deployment pass test but @Test assertions fails with a NullPointer Exception for injected services:

@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));
}
}

Injected service is a: "Proxy for view class: EmployeeService of EJB:mployeeServiceImpl" and when I try to invoke a method it returns:

at first time I used dao but I with jpa is not useful so now in service I'm using entity manager but still not work :(

@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);
}

}

Using this asset it return this error:

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:

select employee0_.employeed_id as employee1_3_0_, employee0_.address as....

I'm using Java EE 7 on Wildfly8 and Mysql

I believe that the cause of this problem is the database configuration definition.

My first guess would be that you don't have set your hibernate.hbm2ddl.auto value to create or create-drop in your persistence.xml like this:

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

This line tells hibernate (in this case) to create the database tables at start up and drops them at shutdown. If you don't add this line it's possible that the tables don't exist in the database.

If this isn't the problem it would be helpful if you added your Employee class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM