2015-12-02 119 views
0

我試圖執行一個簡單的JUnit測試,以便編組/解組一些數據。jaxb2-maven插件與JUnit4的集成

這裏是「數據」的定義:

@XmlRootElement(name = "data") 
@XmlAccessorType(XmlAccessType.FIELD) 
public class Data { 
public Data(){} 
@XmlAttribute(name = "name") 
private String name = "name"; 
public String getName(){return this.name;} 
public void setName(String name){this.name = name;} 
} 

應用程序工作正常(我可以馬歇爾/解組此對象作爲XML)

但是,當我執行測試,我有以下錯誤(在測試的代碼是完全一樣的,因爲它是在應用程序):

java.lang.NoClassDefFoundError: com/sun/istack/Pool 
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1138) 
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:162) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at javax.xml.bind.ContextFinder.newInstance(Unknown Source) 
at javax.xml.bind.ContextFinder.newInstance(Unknown Source) 
at javax.xml.bind.ContextFinder.find(Unknown Source) 
at javax.xml.bind.JAXBContext.newInstance(Unknown Source) 
at javax.xml.bind.JAXBContext.newInstance(Unknown Source) 
at com.c42.asset.doubleface.data.Data.xml(Data.java:48) 
at com.c42.asset.doubleface.data.MainTestXML.main(MainTestXML.java:68) 
at com.c42.asset.doubleface.data.DataTest.setUpBeforeClass(DataTest.java:31) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) 
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:309) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 
Caused by: java.lang.ClassNotFoundException: com.sun.istack.Pool 
at java.net.URLClassLoader.findClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
... 30 more 

我不明白爲什麼應用程序的工作,但不是測試。我想有一些關於在maven中定義的插件範圍。這是我的pom.xml:

<dependencies>  
    <dependency> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>jaxb2-maven-plugin</artifactId> 
     <version>2.2</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>mockit</groupId> 
     <artifactId>jmockit</artifactId> 
     <version>0.999</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>4.11</version> 
     <scope>test</scope> 
    </dependency> 
</dependencies> 
<build> 
    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>jaxb2-maven-plugin</artifactId> 
      <version>2.2</version> 
      <configuration>     
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.3</version> 
      <configuration> 
       <source>1.8</source> 
       <target>1.8</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

回答

1

你不應該使用jaxb2-maven-plugin的依賴。

而且你可能需要添加

 <dependency> 
      <groupId>com.sun.xml.bind</groupId> 
      <artifactId>jaxb-core</artifactId> 
      <version>2.2.11</version> 
     </dependency> 
+0

好吧。我刪除「JAXB2 - Maven的插件」依賴(只在「依賴」標籤),我添加了「JAXB核心」的依賴。它運作良好!即使如果我不明白爲什麼它的作品如此好:) –

0

嘗試添加到您的依賴關係:

<dependency> 
    <groupId>com.sun.istack</groupId> 
    <artifactId>istack-commons-runtime</artifactId> 
    <version>2.22</version> 
</dependency>