JUnit 4: how do I create a suite of suites?
Asked Answered
C

4

5

Running the junit below raises an exception.

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import com.prosveta.backend.daoimpl.AllDaoImplTests;

/**
 * Short desc.
 *
 * Longer desc.
 *
 * @author Jean-Pierre Schnyder
 *
 */
@RunWith(Suite.class)
@SuiteClasses({AllDaoImplTests.class,AllServiceImplTests.class})
public class AllBackendTests {
}

Stack trace

java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy
    at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:653)
    at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:460)
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:286)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:222)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52)
    at java.lang.Class.initAnnotationsIfNecessary(Class.java:3070)
    at java.lang.Class.getAnnotations(Class.java:3050)
    at org.junit.runner.Description.createSuiteDescription(Description.java:72)
    at org.junit.internal.runners.ErrorReportingRunner.getDescription(ErrorReportingRunner.java:25)
    at org.junit.runner.Runner.testCount(Runner.java:38)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.countTestCases(JUnit4TestClassReference.java:30)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.countTests(RemoteTestRunner.java:487)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:455)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Thanks for your answer !

Chalmer answered 24/3, 2011 at 17:59 Comment(0)
C
7

I finally found a way of doing what I wanted to achieve with running a junit 4 suite of suites, i.e. running all the tests in all the modules of a multimodule project. To do that, use the Johannes Link ClassPathSuite tool.

Download the jar, install it in your maven repo, create a allTests project which depends on your other projects where your junits reside and create an AllTestClass. Here are some code and scn capture to illustrate the solution:

Install the jar into your maven repo

enter image description here

Create an allTests project

enter image description here

the pom ...

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.prosveta.backend</groupId>
<artifactId>alltests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>com.prosveta.backend</groupId>
        <artifactId>serviceimpl</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.prosveta.backend</groupId>
        <artifactId>daoimpl</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.prosveta.backend</groupId>
        <artifactId>model</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.extensions</groupId>
        <artifactId>cpsuite</artifactId>
        <version>1.2.5</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
</dependencies>

Add dependencies in Eclipse ...

enter image description here

and here is the all tests class

package com.prosveta.backend.serviceimpl;

import org.junit.extensions.cpsuite.ClasspathSuite;
import org.junit.runner.RunWith;

@RunWith(ClasspathSuite.class)
public class AllBackendTests {
}

which you just "run as JUnit".

Chalmer answered 28/4, 2011 at 20:19 Comment(1)
+1 great answer. By the way, I've got it working just the way you describe it in your question. That project was using ANT and the wanted suite (most of the time AllTestsSuite) was given as parameter to the jUnit ANT task, if I remember correctly.Caresa
R
2

If you use eclipse; Properties of Project (Right click on project) / Java Build Path / Project / ....Add your testing projects..and run again :)

Ricky answered 14/4, 2011 at 14:18 Comment(0)
D
2

This exception is usually raised when the test is using a class that is not in the classpath. Just make sure your classpath is correctly set.

Dives answered 30/11, 2012 at 9:20 Comment(1)
Gosh, that Exception is almost as helpful as an Oracle error message, then! :)Crockery
C
0

I had the same problem when I was trying to execute some tests using spring framework.

If you are working on a Maven project, try to add this dependency:

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-library</artifactId>
    <version>1.3</version>
    <scope>test</scope>
</dependency>

This is my entire pom.xml

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.0.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.0.RELEASE</version>
        <scope>test</scope>
    </dependency>
</dependencies>

This configuration is working properly for me.

Cyndi answered 8/4, 2018 at 8:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.