jeudi 22 décembre 2016

Integration test for a SpringBootApplication sitting in between websocket and REST server

I want to test SpringBootApplication that connects to a websocket server and sends received data as a POST requests to another server.

For test purposes I wrote a simple websocket server that sends a single message after connection is established.

@SpringBootApplication
public class WebsocketServerRunner {

   @EnableWebSocket
   @Controller
   public class SimpleWebsocketServer implements WebSocketConfigurer {

       private final Logger logger = LoggerFactory.getLogger(getClass());
       private final String payload = “Test”;

       @Override
       public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
           registry.addHandler(socketHandler(), "/events").withSockJS();
       }

       @Bean
       public WebSocketHandler socketHandler() {
           return new WebSocketHandler();
       }

       class WebSocketHandler extends TextWebSocketHandler {

           @Override
           public void afterConnectionEstablished(WebSocketSession session) throws Exception {
             logger.info("Websocket Server established a connection to {}", session.getUri());
             session.sendMessage(new TextMessage(payload));
          }
      }
    }
  }

Now my application can successfully connect to the websocket server, but in current configuration I cannot create a mock reset server and check if a POST request was sent.

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {Runner.class, WebsocketServerRunner.class})
public class MsgForwarderIntegrationTest {

   @Autowired
   private RestTemplate restTemplate;

   private MockRestServiceServer server;

   @Before
   public void setUp() {
       server = MockRestServiceServer.createServer(restTemplate);
       new SpringApplicationBuilder(WebsocketServerRunner.class).profiles("server").run();
   }

   @Test
   public void temp1() throws Exception {
       server.expect(requestTo("http://localhost:8080/api/xxx"))
               .andExpect(method(HttpMethod.POST))


     .andRespond(withSuccess("resultSuccess", MediaType.TEXT_PLAIN));

   new SpringApplicationBuilder(Runner.class).profiles("test").run();

   server.verify();

} }

Exception:

msgforwarder.EventsSender  : Failed to send events: I/O error on POST request for "http://localhost:8080/api/xxx": Connection refused;

What else can I try?

Aucun commentaire:

Enregistrer un commentaire