Search in sources :

Example 11 with ThrowsException

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

the class InvocationContainerImplStubbingTest method should_add_throwable_for_void_method.

@Test
public void should_add_throwable_for_void_method() throws Throwable {
    invocationContainerImpl.addAnswerForVoidMethod(new ThrowsException(new MyException()));
    invocationContainerImpl.setMethodForStubbing(new InvocationMatcher(simpleMethod));
    try {
        invocationContainerImpl.answerTo(simpleMethod);
        fail();
    } catch (MyException e) {
    }
}
Also used : ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) Test(org.junit.Test)

Example 12 with ThrowsException

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

the class InvocationContainerImplStubbingTest method should_get_results_for_methods.

@Test
public void should_get_results_for_methods() throws Throwable {
    invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod));
    invocationContainerImpl.addAnswer(new Returns("simpleMethod"));
    Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation();
    invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod));
    invocationContainerImpl.addAnswer(new ThrowsException(new MyException()));
    assertEquals("simpleMethod", invocationContainerImpl.answerTo(simpleMethod));
    try {
        invocationContainerImpl.answerTo(differentMethod);
        fail();
    } catch (MyException e) {
    }
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 13 with ThrowsException

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

the class MocksSerializationTest method should_allow_method_delegation.

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

Example 14 with ThrowsException

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

the class MocksSerializationForAnnotationTest method should_allow_throws_exception_to_be_serializable.

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

Example 15 with ThrowsException

use of org.mockito.internal.stubbing.answers.ThrowsException in project mobile-center-sdk-android by Microsoft.

the class StorageHelperTest method readErrorAndCloseError.

@Test
public void readErrorAndCloseError() throws Exception {
    mockStatic(MobileCenterLog.class);
    FileReader fileReader = mock(FileReader.class, new ThrowsException(new IOException()));
    whenNew(FileReader.class).withAnyArguments().thenReturn(fileReader);
    assertNull(StorageHelper.InternalStorage.read(new File("")));
    verify(fileReader).close();
    verifyStatic();
    MobileCenterLog.error(anyString(), anyString(), any(IOException.class));
}
Also used : ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)17 ThrowsException (org.mockito.internal.stubbing.answers.ThrowsException)17 IOException (java.io.IOException)8 File (java.io.File)4 FileReader (java.io.FileReader)4 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 FileNotFoundException (java.io.FileNotFoundException)3 OutputStream (java.io.OutputStream)3 MockitoException (org.mockito.exceptions.base.MockitoException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)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 Closeable (java.io.Closeable)1 SocketTimeoutException (java.net.SocketTimeoutException)1 CryptoProtocolVersion (org.apache.hadoop.crypto.CryptoProtocolVersion)1 ChecksumException (org.apache.hadoop.fs.ChecksumException)1