title: How to test a component with websocket scope?
body: <p>I'm trying to build a simple test of a simple component marked with websocket scope. The problem is that I get this error:</p>
  <blockquote>
  <p>Scope 'websocket' is not active for the current thread;</p>
  </blockquote>
  <p>Which makes sense, because I'm using a simulated WebSocket client to perform a login, and on the client side context of the Spring app the websocket session created by the client has no impact.</p>
  <p>What is the proper way to test websocket scoped components?</p>
  <p>Component class:</p>
  <pre><code>@Service
  @Scope(scopeName = &quot;websocket&quot;, proxyMode = ScopedProxyMode.TARGET_CLASS)
  public class TrackerService {
      
      private final ConcurrentMap&lt;String, Collection&lt;Geolocation&gt;&gt; vehicleBeingTracked = new ConcurrentHashMap&lt;&gt;();
  
      public void startTracking(String plateSequence) {
          vehiclesBeingTracked.put(plateSequence, new ArrayList&lt;&gt;());
      }
  
      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);
      }
  
  }
  </code></pre>
  <p>Controller class:</p>
  <pre><code>@Controller
  @MessageMapping(&quot;/operational/tracker&quot;)
  public class TrackerController {
  
      @Autowired
      private TrackerService trackerService;
  
      @MessageMapping(&quot;/plate/{plateSequence}/track.start&quot;)
      public void startTracking(@DestinationVariable(&quot;plateSequence&quot;) String plateSequence) {
          trackerService.startTracking(plateSequence);
      }
  
  }
  </code></pre>
  <p>Test class:</p>
  <pre><code>@RunWith(SpringRunner.class)
  @ContextConfiguration
  @SpringBootTest(classes = UnifyApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
  @ActiveProfiles(&quot;test&quot;)
  public class TrackerTest {
  
      @Autowired
      private TrackerService trackerService;
  
      @Test
      public void startTracking() {
          TrackerClient client = new TrackerClient(&quot;ws://localhost:9050/operational/websocket&quot;, &quot;admin&quot;, &quot;admin&quot;);
          client.connectAndWait();
          client.send(&quot;/operational/tracker/plate/ABC1234/track.start&quot;);
          TimeUnit.SECONDS.sleep(1);
  
          boolean result = trackerService.isBeingTracked(&quot;ABC1234&quot;); // Here is the error
          Assert.assertTrue(result);
      }
  }
  </code></pre>