2011-03-24 80 views
4

運行下面的junit會引發異常。JUnit 4:如何創建一套套件?

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) 

謝謝您的回答!

回答

2

如果你使用eclipse;項目屬性(右鍵單擊項目)/ Java Build Path/Project/....添加您的測試項目..並再次運行:)

6

我終於找到了一種方法來實現我想通過運行junit 4套件套件,即在多模塊項目的所有模塊中運行所有測試。要做到這一點,請使用Johannes Link ClassPathSuite tool

下載jar,將它安裝到你的maven倉庫中,創建一個allTests項目,這個項目取決於你的junit駐留的其他項目並創建一個AllTestClass。下面是一些代碼和SCN捕獲來說明解決方案:

安裝JAR到你的maven回購

enter image description here

創建ALLTESTS項目

enter image description here

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

添加依賴在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運行」。

+0

+1很好的答案。順便說一句,我已經按照你在問題中描述的方式工作了。如果我沒有記錯的話,那個項目使用了ANT,想要的套件(大部分時間AllTestsSuite)都是作爲jUnit ANT任務的參數給出的。 – kaskelotti 2012-06-27 05:59:04

2

當測試使用不在類路徑中的類時,通常會引發此異常。只要確保你的類路徑設置正確。

+0

天哪,這個異常幾乎和Oracle錯誤信息一樣有用,那麼! :) – nsandersen 2016-11-08 19:13:11

相關問題