JUnit 4: как мне создать наборы сьютов? - PullRequest
5 голосов
/ 24 марта 2011

Запуск джунта ниже вызывает исключение.

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 {
}

Трассировка стека

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)

Спасибо за ваш ответ!

Ответы [ 4 ]

6 голосов
/ 29 апреля 2011

Я наконец-то нашел способ сделать то, что хотел достичь, запустив пакет Junit 4, то есть запустив все тесты во всех модулях многомодульного проекта. Для этого используйте инструмент Johannes Link ClassPathSuite .

Загрузите флягу, установите ее в своем репозитории Maven, создайте проект allTests, который зависит от других ваших проектов, где находятся ваши юниты, и создайте AllTestClass. Вот некоторые примеры кода и scn для иллюстрации решения:

Установите банку в репозиторий Maven

enter image description here

Создание проекта allTests

enter image description here

пом ...

<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>

Добавить зависимости в Eclipse ...

enter image description here

и вот все классы испытаний

package com.prosveta.backend.serviceimpl;

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

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

который вы просто «запускаете как JUnit».

2 голосов
/ 30 ноября 2012

Это исключение обычно возникает, когда в тесте используется класс, которого нет в пути к классам. Просто убедитесь, что ваш classpath установлен правильно.

2 голосов
/ 14 апреля 2011

Если вы используете затмение;Свойства проекта (Щелкните правой кнопкой мыши по проекту) / Путь сборки Java / Проект / .... Добавьте свои проекты тестирования .. и запустите снова:)

0 голосов
/ 08 апреля 2018

У меня была такая же проблема, когда я пытался выполнить некоторые тесты с использованием Spring Framework.

Если вы работаете над проектом Maven, попробуйте добавить эту зависимость:

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

Это весь мой 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>

Эта конфигурация работает правильно для меня.

...