首页 > 解决方案 > Spring 如何写Junit4 关于Websocket

问题描述

我知道如何测试 websocket,比如使用 WebSocketKing 或 Postman(2021/5/20),效果很好

我不知道在单元测试中编写 websocket

通常,我使用“SpringJUnit4ClassRunner”和“@WebAppConfiguration”来模拟我的服务并测试我的控制器

但是如何创建一个模拟 websocket 服务器来在单元测试中测试我的 websocket?

我可以使用像我的 SimpleControllerTest 这样的方式来创建一个模拟 webSocket 服务器吗?

如果可以,该怎么做?

对不起我的英语不好,谢谢大家

环境
Java:1.8
服务器:Tomcat 8.5
测试:Junit 4
套接字:javax.websocket.jar
框架:Spring MVC

我的简单 WebSocket 代码

package com.ws.socket;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@ServerEndpoint(value = "/MyEndpoint")
public class Socket {

    private SocketConnection socketConnection;

    @OnOpen
    public void onOpen(Session session) {
        socketConnection = new SocketConnectionImpl(session);
        socketConnection.onOpen(session.getId());
    }

    @OnClose
    public void onClose(Session session) {
        socketConnection.onClose(session.getId());
    }

    @OnMessage
    public void onMessage(Session session, String msg) throws IOException {
        socketConnection.onMessage(session, msg);
    }

    @OnError
    public void onError(Session session, Throwable error) {
        socketConnection.onError(session.getId(), error);
    }
}

public abstract class SocketConnection {

    protected SocketConnection(Session session) {
    }

    protected void onOpen(String sessionId) {
        System.out.println(sessionId + " build websocket connection !");
    }

    protected void onClose(String sessionId) {
        System.out.println(sessionId + " close connection !");
    }

    protected void onMessage(Session session, String msg) throws IOException {
        System.out.println(session.getId() + " say : " + msg);
        session.getBasicRemote().sendText(" already receive msg about your say " + msg);
    }

    protected void onError(String sessionId, Throwable error) {
        System.out.println(sessionId + " get error , message = " + error.getMessage());
    }
}

我的简单测试控制器

package com.ws.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SimpleController {

    @GetMapping("/test")
    public String testService() {
        return "start success!";
    }
}

我的简单控制器测试

package com.ws.controller;

import com.ws.config.ServletConfig;
import com.ws.config.SpringConfig;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

@WebAppConfiguration
@ContextConfiguration(classes = {ServletConfig.class, SpringConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class SimpleControllerTest {
    @Autowired
    WebApplicationContext webApplicationContext;

    MockMvc mvc;

    @Before
    public void init() {
        mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
                .build();
    }

    @Test
    public void testAPI() throws Exception {
        String result = mvc.perform(get("/test")).andReturn().getResponse().getContentAsString();
        Assert.assertEquals("start success!", result);
    }
}

标签: javaspringwebsocketjunit4springmockito

解决方案


推荐阅读