public void testAddingServiceWorks() {
    ServiceContext context = ServiceContext.of(ConfigSource.class, MOCK_CONFIG_SOURCE);
    ServiceContext context2 = context.with(SecuritySource.class, MOCK_SECURITY_SOURCE);

    assertThat(context2.get(ConfigSource.class), is(MOCK_CONFIG_SOURCE));
    assertThat(context2.get(SecuritySource.class), is(MOCK_SECURITY_SOURCE));
  }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testAddServiceWithMapHandlesNullValues() {
   Map<Class<?>, Object> services = new HashMap<>();
   services.put(null, new Object());
   ServiceContext context = ServiceContext.of(ConfigSource.class, MOCK_CONFIG_SOURCE);
   context.with(services);
 }
  public void testCreateServiceWithMapWorks() {
    final Map<Class<?>, Object> services =
        ImmutableMap.<Class<?>, Object>of(
            ConfigSource.class, MOCK_CONFIG_SOURCE,
            SecuritySource.class, MOCK_SECURITY_SOURCE);
    ServiceContext context = ServiceContext.of(services);

    assertThat(context.get(ConfigSource.class), is(MOCK_CONFIG_SOURCE));
    assertThat(context.get(SecuritySource.class), is(MOCK_SECURITY_SOURCE));
  }
  @Test(expectedExceptions = ClassCastException.class)
  public void testCreateServiceWithIncorrectTypeIsDetected() {

    // Generics prevent ServiceContext.of(ConfigSource.class, MOCK_SECURITY_SOURCE)
    // from compiling. This test ensures we get equivalent safety when we configure
    // via a Map (where the generics can't help).
    Map<Class<?>, Object> services = new HashMap<>();
    services.put(ConfigSource.class, MOCK_SECURITY_SOURCE);
    ServiceContext.of(services);
  }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testAddingServiceWithNullClassIsHandled() {
   ServiceContext context = ServiceContext.of(ConfigSource.class, MOCK_CONFIG_SOURCE);
   context.with(null, new Object());
 }
  public void testCreateServiceWorks() {
    ServiceContext context = ServiceContext.of(ConfigSource.class, MOCK_CONFIG_SOURCE);

    assertThat(context.get(ConfigSource.class), is(MOCK_CONFIG_SOURCE));
  }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testCreateServiceWithMapHandlesNullValues() {
   Map<Class<?>, Object> services = new HashMap<>();
   services.put(null, new Object());
   ServiceContext.of(services);
 }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testCreateServiceWithMapHandlesNullKeys() {
   Map<Class<?>, Object> services = new HashMap<>();
   services.put(ConfigSource.class, null);
   ServiceContext.of(services);
 }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testCreateServiceWithMapHandlesNullClass() {
   ServiceContext.of(null);
 }
 @Test(expectedExceptions = IllegalArgumentException.class)
 public void testCreateServiceHandlesNullObject() {
   ServiceContext.of(ConfigSource.class, null);
 }
  public void testUpdatingServiceWorks() {
    ServiceContext context = ServiceContext.of(ConfigSource.class, MOCK_CONFIG_SOURCE);
    ServiceContext context2 = context.with(ConfigSource.class, MOCK_CONFIG_SOURCE2);

    assertThat(context2.get(ConfigSource.class), is(MOCK_CONFIG_SOURCE2));
  }