Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug with throwing exception from void method using WithBDDMockito #7

Closed
nickheniser opened this issue Mar 11, 2016 · 7 comments
Closed
Labels
Milestone

Comments

@nickheniser
Copy link

The following code will cause java.lang.NoSuchMethodError instead of actually throwing the exception.

@Mock BufferedWriter mockedBufferedWriter
willThrow(new IOException()).given(mockedBufferedWriter).write(anyString());
@szpak
Copy link
Owner

szpak commented Mar 14, 2016

What Mockito and Mockito-Java8 versions do you use?

@szpak
Copy link
Owner

szpak commented Mar 15, 2016

(as I'm not able to reproduce it with Mockito 2.0.44-beta and mockito-java8 2.0.0-beta)

@nickheniser
Copy link
Author

I am using Mockito 2.042-beta and mockito-java8 2.0.0-beta. But I also tried with 2.0.44 and it still is an issue.

package com.example;

import info.solidsoft.mockito.java8.api.WithBDDMockito;

import java.io.BufferedWriter;
import java.io.IOException;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class ReproducableTest implements WithBDDMockito {

    @Mock BufferedWriter writer;

    @Test(expected = IOException.class)
    public void testWrongExceptionThrown() throws IOException {

        willThrow(new IOException()).given(writer)
            .write(anyString());

        writer.write("throws NoSuchMethodError instead of IOException");

    }
}

@szpak
Copy link
Owner

szpak commented Mar 22, 2016

In the end I found a reason. BDDMockito.willThrow accepts Throwable... toBeThrown while my WithBDDMockito.willThrow was accepting just Throwable. I fixed it and will be available in the next version of mockito-java8.

Btw, your code works fine when put into the mockito-java8 project itself as a test. I was needed to create a separate project to reproduce that issue...

Thanks for your report.

@szpak szpak added the bug label Mar 22, 2016
@szpak szpak added this to the 2.0.0-beta.2 milestone Mar 22, 2016
@nickheniser
Copy link
Author

Very odd about it working in the project and not externally. Thanks for
making a fix, I will watch for the new release!

Nick

On Tue, Mar 22, 2016 at 6:38 PM Marcin Zajączkowski <
notifications@github.com> wrote:

In the end I found a reason. BDDMockito.willThrow accepts Throwable...
toBeThrown while my WithBDDMockito.willThrow was accepting just Throwable.
I fixed it and will be available in the next version of mockito-java8.

Btw, your code works fine when put into the mockito-java8 project itself
as a test. I was needed to create a separate project to reproduce that
issue...

Thanks for your report.


You are receiving this because you authored the thread.

Reply to this email directly or view it on GitHub
#7 (comment)

@szpak
Copy link
Owner

szpak commented Mar 26, 2016

I don't like unexplained situations, so I had looked into the case and I found that even with a little bit different method signatures with varargs the compiler is able to generate valid byte code by converting my:

return BDDMockito.willThrow(toBeThrown);

into

return BDDMockito.willThrow(new Throwable[]{toBeThrown});

I suspected enhancements in the compiler, but I wasn't able to reproduce it with very early Java 8 versions. In the end I have found that mockito-java8 2.0.0-beta.1 was built using mockito 2.0.22-beta as a dependency and in 2.0.32-beta the signatures was changed. Therefore your test case works fine from the project itself (with 2.0.44-beta compiler adjusted the code, with 2.0.22-beta API matched) and the problem only occurs with the original 2.0.0-beta.1 jar built with old method signatures in Mockito . The new version would be probably incompatible with Mockito <2.0.32 (if changed methods are called).

@szpak
Copy link
Owner

szpak commented Mar 26, 2016

The fix is available in 2.0.0-beta.2.

@szpak szpak closed this as completed Mar 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants