6

I am using arquillian for unit test. I am creating deployment jar using shrinkWrap. But for that I need to add all the packages which are used in my project, which are a lot in number.

following is my test file

@RunWith(Arquillian.class)
public class GreeterTest {



    @Deployment
    public static JavaArchive createDeployment() throws NamingException {

        return ShrinkWrap.create(JavaArchive.class, "test.jar")
                .addPackage(ABC.class.getPackage())
                .addPackage(EFG.class.getPackage())
                .addPackage(HIJ.class.getPackage())
                .addPackage(KLM.class.getPackage())
                .addPackage(NOP.class.getPackage())
                .addPackage(QRS.class.getPackage())
                .addPackage(TUV.class.getPackage())
                .addPackage(XYZ.class.getPackage())

                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");

      }

    @Inject
    ABC abc;

    @Inject
    EFG efg;

    @Inject
    HIJ hij;

    @Inject
    KLM klm;

    @Inject
    NOP nop;

    @Test
    public void shouldBeAbleToInjectEJBAndInvoke() throws Exception {

        abc.getDetail();

    }
}

You can see .addPackage(). there are hundreds of packages in my project. Obvious code size will be increasing enormously

Is there any other way for this? Or I must be making some big mistake

nilesh
  • 1,483
  • 2
  • 19
  • 37
  • BTW. Your resource definition is wrong. Should be: `.addAsManifestResource("META-INF/test-persistence.xml", "persistence.xml");` – alperc Aug 07 '18 at 13:35

2 Answers2

17

I'd recommend you to use string representation of package path: "com.root.core" etc. And there are methods:

addPackage(String pack)

addPackages(boolean recursive, String... packages)

The latest is more appropriate for you I guess as it provides you a possibility to add packages recursively and thus you avoid repeatedly including every package. For instance:

.addPackages(true, "com.root")
Vit Ias
  • 725
  • 4
  • 16
  • It worked. Thanx. But now its giving me same error for library m using, like org.codehaus, com.google – nilesh Aug 10 '15 at 13:57
  • Generaly it should resolve dependencies. You can try to add that problem dependencies manually. I already described the approach here: http://stackoverflow.com/questions/30932182/how-to-add-library-in-arquillian-service-deployment/31044392#31044392 – Vit Ias Aug 10 '15 at 14:25
0

You can use the existing EAR/WAR/JAR of your app because creating EAR with ShrinkWrap would be annoying in some complex cases (many dependencies etc). The @Deployment method should embed the test WAR into the EAR and add a module element into existing application.xml before returning the archive to Arquillian runtime.

A @Deployment method example:

...
@Deployment
public static Archive<?> createDeploymentPackage() throws IOException {
    final String testWarName = "test.war";

    final EnterpriseArchive ear = ShrinkWrap.createFromZipFile(
            EnterpriseArchive.class, new File("target/myApp.ear"));

    addTestWar(ear, myClassTest.class, testWarName);
...

Arquillian EJB-JAR/EAR testing examples

Arquillian EJB-JAR/EAR testing examples/Github


SO: How to add test classes to an imported ear file and run server side with arquillian?

oikonomopo
  • 4,025
  • 7
  • 44
  • 73