2

Is there something as simple as Spring MockMvc for websocket testing? I saw some questions/answers but all they are at least one year old and it seemed to me not so simple in terms of writing tests. Let's say I've got this controller:

public class ClieantQuery {
    private String name;
    private int count;

    public String getName() { return name; }

    public int getCount() { return count; }
}

@Controller
public class WSController {

    @MessageMapping("/ws-message")
    @SendTo("/message/data")
    public List<String> processClientQuery(ClientQuery clientQuery) {
        return IntStream.range(0, clientQuery.getCount())
                .boxed().map(i -> "Hello " + clientQuery.getName())
                .collect(Collectors.toList());
    }
}

How might look the class of tests?

1
  • This is not unit testing but integration testing. Commented Mar 13, 2019 at 11:20

1 Answer 1

1

You can use one of the two methods mentioned here (https://github.com/rstoyanchev/spring-websocket-portfolio/tree/master/src/test/java/org/springframework/samples/portfolio/web) to test your controllers, using a SubscribableChannel as an alternative to MockMvc to send messages:

  1. Tests loading Spring configuration: https://github.com/rstoyanchev/spring-websocket-portfolio/blob/master/src/test/java/org/springframework/samples/portfolio/web/context/ContextPortfolioControllerTests.java
  2. Tests without loading Spring configuration: https://github.com/rstoyanchev/spring-websocket-portfolio/blob/master/src/test/java/org/springframework/samples/portfolio/web/standalone/StandalonePortfolioControllerTests.java
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.