private FilterTestEnv(String method, String requestURI, String signupUrl) {
      context = new MockServletContext();
      req = new MockHttpServletRequest(context, method, requestURI);
      res = new MockHttpServletResponse();
      chain = new MockFilterChain();
      authManager = mock(AuthenticationManager.class);
      userIdSource = mock(UserIdSource.class);
      usersConnectionRepository = mock(UsersConnectionRepository.class);
      connectionRepository = mock(ConnectionRepository.class);

      filter =
          new SocialAuthenticationFilter(
              authManager,
              userIdSource,
              usersConnectionRepository,
              new SocialAuthenticationServiceRegistry());
      filter.setServletContext(context);
      filter.setRememberMeServices(new NullRememberMeServices());
      filter.setSignupUrl(signupUrl);

      when(filter.getUsersConnectionRepository().createConnectionRepository(Mockito.anyString()))
          .thenReturn(connectionRepository);

      auth = new SocialAuthenticationToken(DummyConnection.dummy("provider", "user"), null);

      Collection<? extends GrantedAuthority> authorities = Collections.emptyList();
      User user = new SocialUser("foo", "bar", authorities);
      authSuccess =
          new SocialAuthenticationToken(
              DummyConnection.dummy("provider", "user"), user, null, authorities);
    }
  @SuppressWarnings("unchecked")
  @Test
  public void addConnection() {
    UsersConnectionRepository usersConnectionRepository = mock(UsersConnectionRepository.class);
    SocialAuthenticationFilter filter =
        new SocialAuthenticationFilter(null, null, usersConnectionRepository, null);

    SocialAuthenticationService<Object> authService = mock(SocialAuthenticationService.class);
    ConnectionRepository connectionRepository = mock(ConnectionRepository.class);
    ConnectionFactory<Object> connectionFactory = mock(MockConnectionFactory.class);

    MockHttpServletRequest request = new MockHttpServletRequest();
    ConnectionData data =
        new ConnectionData("dummyprovider", "1234", null, null, null, null, null, null, null);
    String userId = "joe";

    DummyConnection<Object> connection = DummyConnection.dummy(data.getProviderId(), userId);

    when(usersConnectionRepository.findUserIdsConnectedTo(
            data.getProviderId(), set(data.getProviderUserId())))
        .thenReturn(empty(String.class));
    when(usersConnectionRepository.createConnectionRepository(userId))
        .thenReturn(connectionRepository);

    when(authService.getConnectionCardinality()).thenReturn(ConnectionCardinality.ONE_TO_ONE);
    when(authService.getConnectionFactory()).thenReturn(connectionFactory);
    when(authService.getConnectionAddedRedirectUrl(request, connection)).thenReturn("/redirect");

    when(connectionFactory.createConnection(data)).thenReturn(connection);

    Connection<?> addedConnection = filter.addConnection(authService, userId, data);
    assertNotNull(addedConnection);
    assertSame(connection, addedConnection);

    verify(connectionRepository).addConnection(connection);
  }