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
6 changes: 0 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,6 @@
<version>5.23.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
Expand Down
41 changes: 16 additions & 25 deletions src/test/java/com/google/acai/AcaiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

Expand All @@ -31,9 +32,7 @@
import javax.inject.Inject;
import javax.inject.Singleton;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
Expand All @@ -42,7 +41,6 @@

@RunWith(MockitoJUnitRunner.class)
public class AcaiTest {
@Rule public ExpectedException thrown = ExpectedException.none();
@Mock private Statement statement;
@Mock private FrameworkMethod frameworkMethod;

Expand Down Expand Up @@ -95,12 +93,8 @@ public void testCaseIsInjected() throws Throwable {
@Test
public void failingTestInjectionDoesNotAffectSubsequentTests() throws Throwable {
Acai acai = new Acai(TestModule.class);
try {
acai.apply(statement, frameworkMethod, new TestWithUnsatisfiedBinding()).evaluate();
assertWithMessage("Expected ConfigurationException to be thrown.").fail();
} catch (ConfigurationException e) {
// Expected: TestWithUnsatisfiedBinding requires binding not satisfied by TestModule.
}
Statement failing = acai.apply(statement, frameworkMethod, new TestWithUnsatisfiedBinding());
assertThrows(ConfigurationException.class, failing::evaluate);

acai.apply(statement, frameworkMethod, new ExampleTest()).evaluate();

Expand All @@ -110,12 +104,8 @@ public void failingTestInjectionDoesNotAffectSubsequentTests() throws Throwable
@Test
public void failingBeforeTestMethodDoesNotAffectSubsequentTests() throws Throwable {
Acai acai = new Acai(FailingBeforeTestModule.class);
try {
acai.apply(statement, frameworkMethod, new ExampleTest()).evaluate();
assertWithMessage("Expected TestException to be thrown.").fail();
} catch (TestException e) {
// Expected: ServiceWithFailingBeforeTest throws TestException in @BeforeTest.
}
Statement failing = acai.apply(statement, frameworkMethod, new ExampleTest());
assertThrows(TestException.class, failing::evaluate);

ServiceWithFailingBeforeTest.shouldFail = false;
acai.apply(statement, frameworkMethod, new ExampleTest()).evaluate();
Expand Down Expand Up @@ -146,19 +136,20 @@ public void servicesRunInDependencyOrder() throws Throwable {
}

@Test
public void usefulErrorMessageWhenModuleMissingZeroArgConstructor() throws Throwable {
thrown.expectMessage("does not have zero argument constructor");
new Acai(ModuleWithoutZeroArgumentConstructor.class)
.apply(statement, frameworkMethod, new Object())
.evaluate();
public void usefulErrorMessageWhenModuleMissingZeroArgConstructor() {
Statement runner =
new Acai(ModuleWithoutZeroArgumentConstructor.class)
.apply(statement, frameworkMethod, new Object());
RuntimeException e = assertThrows(RuntimeException.class, runner::evaluate);
assertThat(e).hasMessageThat().contains("does not have zero argument constructor");
}

@Test
public void rethrowsExceptionThrownByModuleConstructor() throws Throwable {
thrown.expect(TestException.class);
new Acai(ModuleWithThrowingConstructor.class)
.apply(statement, frameworkMethod, new Object())
.evaluate();
public void rethrowsExceptionThrownByModuleConstructor() {
Statement runner =
new Acai(ModuleWithThrowingConstructor.class)
.apply(statement, frameworkMethod, new Object());
assertThrows(TestException.class, runner::evaluate);
}

@Test
Expand Down
12 changes: 5 additions & 7 deletions src/test/java/com/google/acai/DependenciesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@
package com.google.acai;

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

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import java.util.Set;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class DependenciesTest {
@Rule public ExpectedException thrown = ExpectedException.none();

private static class ServiceA implements TestingService {}

private static class ServiceB implements TestingService {}
Expand Down Expand Up @@ -98,8 +95,9 @@ private static class C implements TestingService {}

@Test
public void throwsExceptionIfCyclePresent() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Cycle");
Dependencies.inOrder(ImmutableSet.of(new A(), new B(), new C()));
ImmutableSet<TestingService> services = ImmutableSet.of(new A(), new B(), new C());
IllegalArgumentException e =
assertThrows(IllegalArgumentException.class, () -> Dependencies.inOrder(services));
assertThat(e).hasMessageThat().contains("Cycle");
}
}
12 changes: 5 additions & 7 deletions src/test/java/com/google/acai/TestScopeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@
package com.google.acai;

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

import com.google.inject.AbstractModule;
import com.google.inject.Provider;
import com.google.inject.ProvisionException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
import javax.inject.Inject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;
Expand All @@ -35,7 +34,6 @@

@RunWith(MockitoJUnitRunner.class)
public class TestScopeTest {
@Rule public ExpectedException thrown = ExpectedException.none();
@Mock private Statement statement;
@Mock private FrameworkMethod frameworkMethod;

Expand All @@ -61,12 +59,12 @@ public void differentInstanceInjectedAcrossTests() throws Throwable {
}

@Test
public void servicesInstantiatedOutsideTestScope() throws Throwable {
public void servicesInstantiatedOutsideTestScope() {
FakeTestClass test = new FakeTestClass();
Statement runner = new Acai(InvalidTestModule.class).apply(statement, frameworkMethod, test);

thrown.expect(ProvisionException.class);
thrown.expectMessage("@TestScoped binding outside test");
new Acai(InvalidTestModule.class).apply(statement, frameworkMethod, test).evaluate();
ProvisionException e = assertThrows(ProvisionException.class, runner::evaluate);
assertThat(e).hasMessageThat().contains("@TestScoped binding outside test");
}

@Test
Expand Down
22 changes: 10 additions & 12 deletions src/test/java/com/google/acai/TestingServiceManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@
package com.google.acai;

import static com.google.common.truth.Truth.assertThat;
import static org.hamcrest.Matchers.isA;
import static org.junit.Assert.assertThrows;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class TestingServiceManagerTest {
@Rule public ExpectedException thrown = ExpectedException.none();

@Test
public void callBeforeSuiteMethod() {
Expand Down Expand Up @@ -93,28 +90,29 @@ public void methodsFoundThroughSubclass() {

@Test
public void runtimeExceptionsPropagated() {
thrown.expect(TestRuntimeException.class);
new TestingServiceManager(
TestingServiceManager manager =
new TestingServiceManager(
new TestingService() {
@BeforeTest
void beforeTest() {
throw new TestRuntimeException();
}
})
.beforeTest();
});
assertThrows(TestRuntimeException.class, manager::beforeTest);
}

@Test
public void checkedExceptionsPropagatedInsideRuntimeException() {
thrown.expectCause(isA(TestException.class));
new TestingServiceManager(
TestingServiceManager manager =
new TestingServiceManager(
new TestingService() {
@BeforeTest
void beforeTest() throws TestException {
throw new TestException();
}
})
.beforeTest();
});
RuntimeException e = assertThrows(RuntimeException.class, manager::beforeTest);
assertThat(e).hasCauseThat().isInstanceOf(TestException.class);
}

@Test
Expand Down
Loading