2016-05-03 74 views
3

我必須測試REST Web服務的類是否生成異常。我使用Jersey,Mockito,而IDE是Eclipse,它在Maven命令行下工作正常,但與Eclipse無關。Jersey mockito異常

類:

import static org.junit.Assert.assertEquals; 
import static org.mockito.Matchers.anyString; 
import static org.powermock.api.mockito.PowerMockito.when; 
import java.security.cert.CertificateException; 
import java.util.Collections; 
import java.util.Enumeration; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.ResourceBundle; 
import java.util.UUID; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.Mock; 
import org.mockito.MockitoAnnotations; 
import org.mockito.runners.MockitoJUnitRunner; 

@RunWith(MockitoJUnitRunner.class) 
public class TestController { 

    @Mock 
    Service service; 

    @Before 
    public void setUp() throws CertificateException { 
     MockitoAnnotations.initMocks(this); 
    } 
} 

在POM我:

<dependency> 
    <groupId>org.mockito</groupId> 
    <artifactId>mockito-all</artifactId> 
    <version>1.9.0</version> 
    <scope>test</scope> 
</dependency> 
<dependency> 
    <groupId>org.mockito</groupId> 
    <artifactId>mockito-core</artifactId> 
    <version>1.9.0</version> 
    <scope>test</scope> 
</dependency> 
<dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-servlet</artifactId> 
    <version>1.19</version> 
</dependency> 
<dependency> 
    <groupId>com.sun.jersey</groupId> 
    <artifactId>jersey-bundle</artifactId> 
    <version>1.19</version> 
</dependency> 
<dependency> 
    <groupId>com.sun.jersey.contribs</groupId> 
    <artifactId>jersey-multipart</artifactId> 
    <version>1.19</version> 
</dependency> 

當我添加:

PowerMockito.mock(Service.class); 

@Mock 
Service service; 

拋出以下異常:

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ws/rs/core/Response 
    at java.lang.ClassLoader.defineClass1(Native Method) 
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800) 
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
    ..... 
    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) 
+2

請提供_complete_。[MCVE(http://stackoverflow.com/help/mcve) –

+0

@dur其他依賴關係是不是球衣或JUnit測試 –

回答

0

嘗試添加以下依賴項。將其添加到Jersey依賴關係之後。

<dependency> 
    <groupId>javax</groupId> 
    <artifactId>javaee-api</artifactId> 
    <version>6.0</version> 
    <scope>provided</scope> 
</dependency> 
+0

我有添加這個,但也有相同錯誤 –

+0

我在這裏發現了類似的問題:http://stackoverflow.com/questions/3424207/testing-against-java-ee-6-api 請檢查它是否有幫助 – Sravya

+0

謝謝@Sravya同樣的錯誤 –