java.lang.VerifyError:无法使用Spring WebSocket从最终类继承

时间:2018-07-11 08:37:43

标签: java spring spring-boot websocket

我是Spring的websocket新手,我的单元测试有错误

  

[java.lang.VerifyError:无法从最终类继承]

SockJsClient sockJsClient = new SockJsClient(transports);行中。

import java.lang.reflect.Type;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.annotation.Nullable;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.messaging.simp.stomp.StompFrameHandler;
import org.springframework.messaging.simp.stomp.StompHeaders;
import org.springframework.messaging.simp.stomp.StompSession;
import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.messaging.WebSocketStompClient;
import org.springframework.web.socket.sockjs.client.SockJsClient;
import org.springframework.web.socket.sockjs.client.Transport;
import org.springframework.web.socket.sockjs.client.WebSocketTransport;
import org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec;

import com.pharmagest.entite.websocket.ServerMessage;
import com.pharmagest.guiriden.offidose.websocket.DefEndPoint;


@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class ProductionWebSocketServiceTest2 {

    /**
     * WebSocket parameters
     */
    private static final String PORT = "9181";
    private static final String URL_WEB_SOCKET = "ws://localhost:" + PORT + "/offidose";

    /**
     * Timeout to receive a response on a broker
     */
    private static final int TIMEOUT = 200;

    /**
     * WebSocket session & Queue to stock data
     */
    private StompSession stompSession;
    private BlockingQueue<ServerMessage> blockingQueue;

    @Before
    public void setup() {
        try {
            blockingQueue = new LinkedBlockingQueue<>();

            Transport webSocketTransport = new WebSocketTransport(new StandardWebSocketClient());
            List<Transport> transports = Collections.singletonList(webSocketTransport);

            SockJsClient sockJsClient = new SockJsClient(transports);
            sockJsClient.setMessageCodec(new Jackson2SockJsMessageCodec());

            WebSocketStompClient stompClient = new WebSocketStompClient(sockJsClient);

            stompSession = stompClient.connect(URL_WEB_SOCKET, new StompSessionHandlerAdapter() {
            }).get(TIMEOUT, TimeUnit.MILLISECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        stompSession.subscribe(DefEndPoint.SUBSCRIBE_EXPORT_PROD, new DefaultStompFrameHandler(ServerMessage.class));
    }

    @Test
    public void testWebSocketEndPointExportProd()
            throws InterruptedException {
        stompSession.send(DefEndPoint.SUBSCRIBE_EXPORT_PROD, new ServerMessage());
        ServerMessage sm = blockingQueue.poll(TIMEOUT, TimeUnit.MILLISECONDS);
        Assert.assertEquals("Hello World From Server", sm.getMessage());
    }

    private class DefaultStompFrameHandler
            implements StompFrameHandler {

        private Class respClass;

        DefaultStompFrameHandler(final Class respClass) {
            this.respClass = respClass;
        }

        @Override
        public Type getPayloadType(final StompHeaders stompHeaders) {
            return respClass;
        }

        @Override
        public void handleFrame(final StompHeaders stompHeaders, @Nullable final Object o) {
            if (o != null && o instanceof ServerMessage) {
                blockingQueue.offer((ServerMessage) o);
            }
        }
    }

}

错误:

    java.lang.VerifyError: Cannot inherit from final class
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    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)
    at com.fasterxml.jackson.datatype.jdk8.Jdk8Module.setupModule(Jdk8Module.java:30)
    at com.fasterxml.jackson.databind.ObjectMapper.registerModule(ObjectMapper.java:524)
    at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.registerWellKnownModulesIfAvailable(Jackson2ObjectMapperBuilder.java:734)
    at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.configure(Jackson2ObjectMapperBuilder.java:624)
    at org.springframework.http.converter.json.Jackson2ObjectMapperBuilder.build(Jackson2ObjectMapperBuilder.java:608)
    at org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec.<init>(Jackson2SockJsMessageCodec.java:51)
    at org.springframework.web.socket.sockjs.client.SockJsClient.<init>(SockJsClient.java:107)
    at com.pharmagest.services.websocket.production.ProductionWebSocketServiceTest2.setup(ProductionWebSocketServiceTest2.java:67)
    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:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    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:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

0 个答案:

没有答案