Search in sources :

Example 1 with ThrowsException

use of org.mockito.internal.stubbing.answers.ThrowsException in project hadoop by apache.

the class TestFailoverController method testFailoverFromNonExistantServiceWithFencer.

@Test
public void testFailoverFromNonExistantServiceWithFencer() throws Exception {
    DummyHAService svc1 = spy(new DummyHAService(null, svc1Addr));
    // Getting a proxy to a dead server will throw IOException on call,
    // not on creation of the proxy.
    HAServiceProtocol errorThrowingProxy = Mockito.mock(HAServiceProtocol.class, Mockito.withSettings().defaultAnswer(new ThrowsException(new IOException("Could not connect to host"))).extraInterfaces(Closeable.class));
    Mockito.doNothing().when((Closeable) errorThrowingProxy).close();
    Mockito.doReturn(errorThrowingProxy).when(svc1).getProxy(Mockito.<Configuration>any(), Mockito.anyInt());
    DummyHAService svc2 = new DummyHAService(HAServiceState.STANDBY, svc2Addr);
    svc1.fencer = svc2.fencer = setupFencer(AlwaysSucceedFencer.class.getName());
    try {
        doFailover(svc1, svc2, false, false);
    } catch (FailoverFailedException ffe) {
        fail("Non-existant active prevented failover");
    }
    // Verify that the proxy created to try to make it go to standby
    // gracefully used the right rpc timeout
    Mockito.verify(svc1).getProxy(Mockito.<Configuration>any(), Mockito.eq(CommonConfigurationKeys.HA_FC_GRACEFUL_FENCE_TIMEOUT_DEFAULT));
    // Don't check svc1 because we can't reach it, but that's OK, it's been fenced.
    assertEquals(HAServiceState.ACTIVE, svc2.state);
}
Also used : ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) Closeable(java.io.Closeable) IOException(java.io.IOException) AlwaysSucceedFencer(org.apache.hadoop.ha.TestNodeFencer.AlwaysSucceedFencer) Test(org.junit.Test)

Example 2 with ThrowsException

use of org.mockito.internal.stubbing.answers.ThrowsException in project mockito by mockito.

the class InvocationContainerImplStubbingTest method should_finish_stubbing_when_wrong_throwable_is_set.

@Test
public void should_finish_stubbing_when_wrong_throwable_is_set() throws Exception {
    state.stubbingStarted();
    try {
        invocationContainerImpl.addAnswer(new ThrowsException(new Exception()));
        fail();
    } catch (MockitoException e) {
        state.validateState();
    }
}
Also used : ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) MockitoException(org.mockito.exceptions.base.MockitoException) ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) MockitoException(org.mockito.exceptions.base.MockitoException) Test(org.junit.Test)

Example 3 with ThrowsException

use of org.mockito.internal.stubbing.answers.ThrowsException in project mockito by mockito.

the class MocksSerializationTest method should_allow_throws_exception_to_be_serializable.

@Test
public void should_allow_throws_exception_to_be_serializable() throws Exception {
    // given
    Bar mock = mock(Bar.class, new ThrowsException(new RuntimeException()));
    // when-serialize then-deserialize
    serializeAndBack(mock);
}
Also used : ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) Test(org.junit.Test)

Example 4 with ThrowsException

use of org.mockito.internal.stubbing.answers.ThrowsException in project neo4j by neo4j.

the class ExecutionResultSerializerTest method shouldAbbreviateWellKnownIOErrors.

@Test
public void shouldAbbreviateWellKnownIOErrors() throws Exception {
    // given
    OutputStream output = mock(OutputStream.class, new ThrowsException(new IOException("Broken pipe")));
    AssertableLogProvider logProvider = new AssertableLogProvider();
    ExecutionResultSerializer serializer = getSerializerWith(output, null, logProvider);
    // when
    serializer.finish();
    // then
    logProvider.assertExactly(AssertableLogProvider.inLog(ExecutionResultSerializer.class).error("Unable to reply to request, because the client has closed the connection (Broken pipe)."));
}
Also used : ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) Test(org.junit.Test) Neo4jJsonCodecTest(org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)

Example 5 with ThrowsException

use of org.mockito.internal.stubbing.answers.ThrowsException in project neo4j by neo4j.

the class ExecutionResultSerializerTest method shouldLogIOErrors.

@Test
public void shouldLogIOErrors() throws Exception {
    // given
    IOException failure = new IOException();
    OutputStream output = mock(OutputStream.class, new ThrowsException(failure));
    AssertableLogProvider logProvider = new AssertableLogProvider();
    ExecutionResultSerializer serializer = getSerializerWith(output, null, logProvider);
    // when
    serializer.finish();
    // then
    logProvider.assertExactly(AssertableLogProvider.inLog(ExecutionResultSerializer.class).error(is("Failed to generate JSON output."), sameInstance(failure)));
}
Also used : ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) AssertableLogProvider(org.neo4j.logging.AssertableLogProvider) Test(org.junit.Test) Neo4jJsonCodecTest(org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)

Aggregations

Test (org.junit.Test)15 ThrowsException (org.mockito.internal.stubbing.answers.ThrowsException)15 IOException (java.io.IOException)6 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)4 OutputStream (java.io.OutputStream)3 MockitoException (org.mockito.exceptions.base.MockitoException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)2 Returns (org.mockito.internal.stubbing.answers.Returns)2 Invocation (org.mockito.invocation.Invocation)2 AssertableLogProvider (org.neo4j.logging.AssertableLogProvider)2 Neo4jJsonCodecTest (org.neo4j.server.rest.transactional.Neo4jJsonCodecTest)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 Closeable (java.io.Closeable)1 SocketTimeoutException (java.net.SocketTimeoutException)1 CryptoProtocolVersion (org.apache.hadoop.crypto.CryptoProtocolVersion)1 ChecksumException (org.apache.hadoop.fs.ChecksumException)1