如何使用websocket范围测试组件?

时间:2019-04-25 18:37:03

标签: java spring spring-boot spring-test spring-websocket

我正在尝试对带有websocket范围标记的简单组件进行简单测试。问题是我收到此错误:

Scope 'websocket' is not active for the current thread;

这很有道理,因为我正在使用模拟的WebSocket客户端执行登录,并且在Spring应用程序的客户端上下文中,客户端创建的websocket会话没有影响。

测试websocket作用域组件的正确方法是什么?

组件类:

@Service
@Scope(scopeName = "websocket", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class TrackerService {

    private final ConcurrentMap<String, Collection<Geolocation>> vehicleBeingTracked = new ConcurrentHashMap<>();

    public void startTracking(String plateSequence) {
        vehiclesBeingTracked.put(plateSequence, new ArrayList<>());
    }

    public void stopTracking(String plateSequence) {
        vehiclesBeingTracked.remove(plateSequence);
    }

    public void spotVehicle(String plateSequence, Geolocation geo) {
        vehiclesBeingTracked.get(plateSequence).add(geo);
    }

    public boolean isBeingTracked(String plateSequence) {
        return vehiclesBeingTracked.containsKey(plateSequence);
    }

}

控制器类:

@Controller
@MessageMapping("/operational/tracker")
public class TrackerController {

    @Autowired
    private TrackerService trackerService;

    @MessageMapping("/plate/{plateSequence}/track.start")
    public void startTracking(@DestinationVariable("plateSequence") String plateSequence) {
        trackerService.startTracking(plateSequence);
    }

}

测试类:

@RunWith(SpringRunner.class)
@ContextConfiguration
@SpringBootTest(classes = UnifyApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")
public class TrackerTest {

    @Autowired
    private TrackerService trackerService;

    @Test
    public void startTracking() {
        TrackerClient client = new TrackerClient("ws://localhost:9050/operational/websocket", "admin", "admin");
        client.connectAndWait();
        client.send("/operational/tracker/plate/ABC1234/track.start");
        TimeUnit.SECONDS.sleep(1);

        boolean result = trackerService.isBeingTracked("ABC1234"); // Here is the error
        Assert.assertTrue(result);
    }


}

1 个答案:

答案 0 :(得分:0)

尝试在测试类中使用以下代码来监视trackerService服务:

@SpyBean
private TrackerService trackerService;

@Test
public void startTracking() {
    TrackerClient client = new 

    TrackerClient("ws://localhost:9050/operational/websocket", "admin", "admin");
    client.connectAndWait();
    client.send("/operational/tracker/plate/ABC1234/track.start");
    TimeUnit.SECONDS.sleep(1);

    boolean result = Mockito.verify(trackerService).isBeingTracked("ABC1234");
    Assert.assertTrue(result);
}

更新的答案:

@MockBean
private TrackerService trackerService;

@Test
public void startTracking() {

    Mockito.doAnswer((Answer<Object>) invocationOnMock -> {
        Object[] args = invocationOnMock.getArguments();
        assertEquals("ABC1234", invocationOnMock.getArgument(0));
        return null;

    }).when(sessionTracker).sessionDestroyed(Mockito.any());

    TrackerClient client = new 

    TrackerClient("ws://localhost:9050/operational/websocket", "admin", "admin");
    client.connectAndWait();
    client.send("/operational/tracker/plate/ABC1234/track.start");
    TimeUnit.SECONDS.sleep(1);
}