I’m trying to work testcode about Java Spring Boot 6.x Websocket code with STOMP but in trouble

public class WebSocketIntegrationTest {

    @LocalServerPort
    private int port;

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper objectMapper;

    private String url;

    private WebSocketStompClient stompClient;
    private WebSocketHandler webSocketHandler;
    private CompletableFuture<NotificationDto> completableFuture = new CompletableFuture<>();

    @Autowired
    private NotificationService notificationService;



    @BeforeEach
    public void setUp() {
        this.stompClient = new WebSocketStompClient(
            new SockJsClient(
                List.of(new WebSocketTransport(new StandardWebSocketClient()))
            )
        );
        var messageConverter = new MappingJackson2MessageConverter();
        messageConverter.setObjectMapper(objectMapper); 
        this.stompClient.setMessageConverter(messageConverter);
        this.url = "ws://localhost:" + port + "/ws";
        this.webSocketHandler = new WebSocketHandler(new CompletableFuture<NotificationDto>());
    }
   
    @Test
    @Transactional
    public void likeArticle_shouldSendNotification() throws Exception {

        StompSession session = stompClient.connectAsync(url, new StompSessionHandlerAdapter() {
                @Override
                public void afterConnected(StompSession session, StompHeaders connectedHeaders) {

                    session.subscribe("/user/testId/topic/notification",
                        new StompFrameHandler() {
                            @Override
                            public Type getPayloadType(StompHeaders headers) {
                                logger.info("Received STOMP Headers: {}", headers);
                                return NotificationDto.class;
                            }

                            @Override
                            public void handleFrame(StompHeaders headers, Object o) {
                                logger.info("Received notification: {}", o);
                                if (o instanceof NotificationDto) {
                                    completableFuture.complete((NotificationDto) o);
                                } else {
                                    logger.error("Unexpected payload type: {}", o.getClass());
                                }
                            }
                        }
                    );
                }
            }
        ).get(5, TimeUnit.SECONDS);

        //when
        notificationService.sendNotification(
            Notification.builder().message("test").user(user).build(), user);

        TimeUnit.SECONDS.sleep(1);

        //then
        var notificationDto = completableFuture.get(20, TimeUnit.SECONDS);
        assertThat(notificationDto).isNotNull();
    }
}

this is my test code for testing websocket message send and transfer server to client but I got always TimeoutException on var notificationDto = completableFuture.get(20, TimeUnit.SECONDS);

According to chatGPT4o mini,

  1. check the path. so I checked the Config.class, and NotificationService.sendNotification()
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic", "/queue");
        config.setApplicationDestinationPrefixes("/app");
        config.setUserDestinationPrefix("/user");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws")
            .addInterceptors(new HttpSessionHandshakeInterceptor())
            .setAllowedOrigins("*")
            .withSockJS();
    }
@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationService {

    private final NotificationRepository notificationRepository;
    private final SimpMessagingTemplate messagingTemplate; 

    @Transactional
    public void createNotification(Notification notification) {
        notificationRepository.save(notification);
    }

    public void sendNotification(Notification notification, User receiver) {
        NotificationDto notificationDto = NotificationDto.from(notification);

        messagingTemplate.
            convertAndSendToUser(
                receiver.getLoginId(), 
                "/topic/notification",
                notificationDto 
            );
        log.info("Notification sent to /user/{}{}", receiver.getLoginId(), "/topic/notification");
    }

I subscribe the end-point thru session.subscribe("/user/testId/topic/notification", new StompFrameHandler()...

I checked so many times but I’m sure the path is great. log.info("Notification sent to /user/{}{}", receiver.getLoginId(), "/topic/notification"); prints out the same destination with session.subscribe("/user/testId/topic/notification",...

  1. check port number -> no problem

  2. make longer the timeout of var notificationDto = completableFuture.get(20, TimeUnit.SECONDS); -> no work (only longer time to wait to get failure)

I really want to know how to work it. and I guess the subscribe() method is something wrong bcs the new StompFrameHandler() has 2 override method and I log the two but not working. that means the getPayloadType() and handleFrame() is not computed

everything I can do. I’m sure for 2 days

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật