how Arquillian Supports multiple JavaArchive Deployments?
huyang Oct 19, 2014 1:43 AMI'm using Arquillian and TestNG to test java CDI components,such as DB service layer based on Mybatis.
Hope that one TestNG unit correspond to one DB service.
Use maven command: mvn test -Dtest=XXXService ,it worked fine.
But use command: mvn test, run all tests, not worked.
I tried some methods,for example,using singleton JavaArchive Deployment and different named @Deployment(name="xxxTest"),still failed.
the dos error output as follow:
(..)stCreate(testng.dbtest.testng.dbtest.security.UrlRuleServiceTest):
(..)stCreate(testng.dbtest.testng.dbtest.security.UserServiceTest):
arquillianAfterClass(testng.dbtest.testng.dbtest.security.UserRoleRelationTest
): Singleton is not set. Is your Thread.currentThread().getContextClassLoader()
set correctly?
Any one can help me?
example code here.
public abstract class TestNGBase<T> extends Arquillian {
protected List<T> objs = new ArrayList<T>();
protected T obj = null;
/* @Deployment
public static JavaArchive createDeployment() {
return DeploymentSingleton.getInstance();
}
*/
public abstract void checked();
public abstract void testCreate();
public abstract void testUpdate();
public abstract void testFind();
public abstract void testDelete();
}
public class UserServiceTest extends TestNGBase<User> {
@Inject
private UserService userService = null;
@Deployment(name = "userServiceTest",order = 15)
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class, "test1.jar")
.addClass(UserServiceImpl.class)
.addClass(LogFactory.class)
.addClass(SqlSessionFactoryProvider.class)
.addClass("org.mybatis.cdi.SqlSessionManagerRegistry")
.addAsResource("test_jdbc.properties")
.addAsResource("mybatis-config.xml")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Override
@Test
@BeforeMethod
public void checked() {
Assert.assertNotNull(userService);
}
@Override
@Test(dependsOnMethods = "checked")
public void testCreate() {
createOneUser("zhangsan", "zhangsan", "zhangsan@qq.com", "salt1");
createOneUser("lisi", "lisi", "lisi@qq.com", "salt2");
createOneUser("wangwu", "wangwu", "wangwu@qq.com", "salt3");
System.out.println(objs);
}
@Override
@Test(dependsOnMethods = "testCreate")
public void testUpdate() {
}
@Override
@Test(dependsOnMethods = "testUpdate")
public void testFind() {
for (User u : objs) {
Assert.assertNotNull(userService.find(u.getId()));
}
for (User u : objs) {
Assert.assertNotNull(userService.findByUsername(u.getUsername()));
}
}
@Override
@Test(dependsOnMethods = "testFind")
public void testDelete() {
//将用户分成几部分,每个部分采用不同的方法删除
int mid = objs.size() / 2;
for (int i = objs.size() - 1; i > mid; i--) {
boolean flag = userService.remove(objs.get(i).getId());
Assert.assertEquals(flag, true);
}
for (int i = mid; i >= 0; i--) {
boolean flag = userService.remove(objs.get(i).getUsername());
Assert.assertEquals(flag, true);
}
//用列表(内存)中的用户去查询数据库
for (User user : objs) {
Assert.assertNull(userService.find(user.getId()));
}
}
private void createOneUser(String username, String password, String email, String salt) {
User.UserBuilder builder = new User.UserBuilder();
builder = builder.username(username).password(password).email(email).salt(salt);
User user = builder.build();
userService.create(user);
Assert.assertNotNull(user);
objs.add(user);
}
}