Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package dagger.android.support;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import android.app.Application;
import android.os.Build;
Expand All @@ -40,12 +40,8 @@ public void injectFragment_simpleApplication() {
Fragment fragment = new Fragment();
startFragment(fragment);

try {
AndroidSupportInjection.inject(fragment);
fail();
} catch (Exception e) {
assertThat(e).hasMessageThat().contains("No injector was found");
}
Exception e = assertThrows(Exception.class, () -> AndroidSupportInjection.inject(fragment));
assertThat(e).hasMessageThat().contains("No injector was found");
}

private static class ApplicationReturnsNull extends Application
Expand All @@ -62,22 +58,15 @@ public void fragmentInjector_returnsNull() {
Fragment fragment = new Fragment();
startFragment(fragment);

try {
AndroidSupportInjection.inject(fragment);
fail();
} catch (Exception e) {
assertThat(e).hasMessageThat().contains("androidInjector() returned null");
}
Exception e = assertThrows(Exception.class, () -> AndroidSupportInjection.inject(fragment));
assertThat(e).hasMessageThat().contains("androidInjector() returned null");
}

@Test
public void injectFragment_nullInput() {
try {
AndroidSupportInjection.inject(null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().contains("fragment");
}
NullPointerException e =
assertThrows(NullPointerException.class, () -> AndroidSupportInjection.inject(null));
assertThat(e).hasMessageThat().contains("fragment");
}

void startFragment(Fragment fragment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package dagger.android;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;
import static org.robolectric.annotation.LooperMode.Mode.LEGACY;

import android.app.Activity;
Expand Down Expand Up @@ -119,14 +119,10 @@ public void fragmentInjectedByParentFragment() {
public void injectActivity_applicationDoesntImplementHasAndroidInjector() {
Activity activity = Robolectric.setupActivity(Activity.class);

try {
AndroidInjection.inject(activity);
fail();
} catch (Exception e) {
assertThat(e)
.hasMessageThat()
.contains("Application does not implement dagger.android.HasAndroidInjector");
}
Exception e = assertThrows(Exception.class, () -> AndroidInjection.inject(activity));
assertThat(e)
.hasMessageThat()
.contains("Application does not implement dagger.android.HasAndroidInjector");
}

@Test
Expand All @@ -135,12 +131,8 @@ public void injectFragment_hasFragmentInjectorNotFound() {
Activity activity = Robolectric.setupActivity(Activity.class);
activity.getFragmentManager().beginTransaction().add(fragment, null).commit();

try {
AndroidInjection.inject(fragment);
fail();
} catch (Exception e) {
assertThat(e).hasMessageThat().contains("No injector was found");
}
Exception e = assertThrows(Exception.class, () -> AndroidInjection.inject(fragment));
assertThat(e).hasMessageThat().contains("No injector was found");
}

private static class ApplicationReturnsNull extends Application implements HasAndroidInjector {
Expand All @@ -155,12 +147,8 @@ public AndroidInjector<Object> androidInjector() {
public void activityInjector_returnsNull() {
Activity activity = Robolectric.setupActivity(Activity.class);

try {
AndroidInjection.inject(activity);
fail();
} catch (Exception e) {
assertThat(e).hasMessageThat().contains("androidInjector() returned null");
}
Exception e = assertThrows(Exception.class, () -> AndroidInjection.inject(activity));
assertThat(e).hasMessageThat().contains("androidInjector() returned null");
}

@Test
Expand All @@ -170,31 +158,21 @@ public void fragmentInjector_returnsNull() {
Activity activity = Robolectric.setupActivity(Activity.class);
activity.getFragmentManager().beginTransaction().add(fragment, null).commit();

try {
AndroidInjection.inject(fragment);
fail();
} catch (Exception e) {
assertThat(e).hasMessageThat().contains("androidInjector() returned null");
}
Exception e = assertThrows(Exception.class, () -> AndroidInjection.inject(fragment));
assertThat(e).hasMessageThat().contains("androidInjector() returned null");
}

@Test
public void injectActivity_nullInput() {
try {
AndroidInjection.inject((Activity) null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().contains("activity");
}
NullPointerException e =
assertThrows(NullPointerException.class, () -> AndroidInjection.inject((Activity) null));
assertThat(e).hasMessageThat().contains("activity");
}

@Test
public void injectFragment_nullInput() {
try {
AndroidInjection.inject((Fragment) null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessageThat().contains("fragment");
}
NullPointerException e =
assertThrows(NullPointerException.class, () -> AndroidInjection.inject((Fragment) null));
assertThat(e).hasMessageThat().contains("fragment");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package dagger.android;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import android.app.Activity;
import android.os.Build;
Expand Down Expand Up @@ -86,11 +86,8 @@ public void throwsIfFactoryCreateReturnsNull() {
ImmutableMap.of(FooActivity.class, () -> null), ImmutableMap.of());
FooActivity activity = Robolectric.setupActivity(FooActivity.class);

try {
dispatchingAndroidInjector.maybeInject(activity);
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
}
assertThrows(
NullPointerException.class, () -> dispatchingAndroidInjector.maybeInject(activity));
}

@Test
Expand All @@ -100,11 +97,9 @@ public void throwsIfClassMismatched() {
ImmutableMap.of(FooActivity.class, BarInjector.Factory::new), ImmutableMap.of());
FooActivity activity = Robolectric.setupActivity(FooActivity.class);

try {
dispatchingAndroidInjector.maybeInject(activity);
fail("Expected InvalidInjectorBindingException");
} catch (InvalidInjectorBindingException expected) {
}
assertThrows(
InvalidInjectorBindingException.class,
() -> dispatchingAndroidInjector.maybeInject(activity));
}

private static <T> DispatchingAndroidInjector<T> newDispatchingAndroidInjector(
Expand Down
24 changes: 5 additions & 19 deletions dagger-runtime/test/javatests/dagger/internal/DoubleCheckTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package dagger.internal;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
Expand All @@ -40,20 +40,12 @@
public class DoubleCheckTest {
@Test
public void provider_nullPointerException() {
try {
DoubleCheck.provider(null);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> DoubleCheck.provider(null));
}

@Test
public void lazy_nullPointerException() {
try {
DoubleCheck.lazy(null);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> DoubleCheck.lazy(null));
}

private static final Provider<Object> DOUBLE_CHECK_OBJECT_PROVIDER =
Expand Down Expand Up @@ -123,10 +115,7 @@ public Object get() {
new AtomicReference<>();
Provider<Object> doubleCheck = DoubleCheck.provider(() -> doubleCheckReference.get().get());
doubleCheckReference.set(doubleCheck);
try {
doubleCheck.get();
fail();
} catch (StackOverflowError expected) {}
assertThrows(StackOverflowError.class, () -> doubleCheck.get());
}

@Test public void reentranceReturningSameInstance() {
Expand Down Expand Up @@ -155,10 +144,7 @@ public Object get() {
return new Object();
});
doubleCheckReference.set(doubleCheck);
try {
doubleCheck.get();
fail();
} catch (IllegalStateException expected) {}
assertThrows(IllegalStateException.class, () -> doubleCheck.get());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package dagger.internal;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -34,10 +34,6 @@ public final class InstanceFactoryTest {
}

@Test public void create_throwsNullPointerException() {
try {
InstanceFactory.create(null);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> InstanceFactory.create(null));
}
}
21 changes: 5 additions & 16 deletions dagger-runtime/test/javatests/dagger/internal/SetBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package dagger.internal;

import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import java.util.Arrays;
import org.junit.Before;
Expand All @@ -35,28 +35,17 @@ public void setUp() {

@Test
public void addNull() {
try {
setBuilder.add(null);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> setBuilder.add(null));
}

@Test
public void addNullCollection() {
try {
setBuilder.addAll(null);
fail();
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> setBuilder.addAll(null));
}

@Test
public void addNullElement() {
try {
setBuilder.addAll(Arrays.asList("hello", null, "world"));
fail();
} catch (NullPointerException expected) {
}
assertThrows(
NullPointerException.class, () -> setBuilder.addAll(Arrays.asList("hello", null, "world")));
}
}
38 changes: 7 additions & 31 deletions javatests/dagger/functional/builder/BuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import static com.google.common.truth.Truth.assertThat;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;

import dagger.Component;
import dagger.Module;
Expand Down Expand Up @@ -491,11 +491,7 @@ public void interfaceBuilder() {
DaggerBuilderTest_TestComponentWithBuilderInterface.builder();

// Make sure things fail if we don't set our required modules.
try {
builder.build();
fail();
} catch (IllegalStateException expected) {
}
assertThrows(IllegalStateException.class, () -> builder.build());

builder
.intModule(new IntModuleIncludingDoubleAndFloat(1))
Expand All @@ -518,11 +514,7 @@ public void abstractClassBuilder() {
TestComponentWithBuilderAbstractClass.builder();

// Make sure things fail if we don't set our required modules.
try {
builder.build();
fail();
} catch (IllegalStateException expected) {
}
assertThrows(IllegalStateException.class, () -> builder.build());

builder
.intModule(new IntModuleIncludingDoubleAndFloat(1))
Expand All @@ -545,11 +537,7 @@ public void interfaceGenericBuilder() {
DaggerBuilderTest_TestComponentWithGenericBuilderInterface.builder();

// Make sure things fail if we don't set our required modules.
try {
builder.build();
fail();
} catch (IllegalStateException expected) {
}
assertThrows(IllegalStateException.class, () -> builder.build());

builder
.setM2(new IntModuleIncludingDoubleAndFloat(1))
Expand All @@ -572,11 +560,7 @@ public void abstractClassGenericBuilder() {
DaggerBuilderTest_TestComponentWithGenericBuilderAbstractClass.builder();

// Make sure things fail if we don't set our required modules.
try {
builder.build();
fail();
} catch (IllegalStateException expected) {
}
assertThrows(IllegalStateException.class, () -> builder.build());

builder
.setM2(new IntModuleIncludingDoubleAndFloat(1))
Expand All @@ -597,11 +581,7 @@ public void abstractClassGenericBuilder() {
public void subcomponents_interface() {
ParentComponent parent = DaggerBuilderTest_ParentComponent.create();
TestChildComponentWithBuilderInterface.Builder builder1 = parent.childInterfaceBuilder();
try {
builder1.build();
fail();
} catch (IllegalStateException expected) {
}
assertThrows(IllegalStateException.class, () -> builder1.build());

builder1
.setM2(new IntModuleIncludingDoubleAndFloat(1))
Expand All @@ -622,11 +602,7 @@ public void subcomponents_abstractclass() {
ParentComponent parent = DaggerBuilderTest_ParentComponent.create();
TestChildComponentWithBuilderAbstractClass.Builder builder2 =
parent.childAbstractClassBuilder();
try {
builder2.build();
fail();
} catch (IllegalStateException expected) {
}
assertThrows(IllegalStateException.class, () -> builder2.build());

builder2
.setM2(new IntModuleIncludingDoubleAndFloat(10))
Expand Down
Loading
Loading