1

I have a puzzling problem when mocking a class with a method that gets a collection as an argument.

public class Foo {
    public Foo () { }

    public boolean doSomething(List<Bar> bars) {
        return true;
    }
}

public class Bar {
    public Bar() {}
}

When trying to create a mock

Foo foo = mock(Foo.class);

I get:

Mockito can only mock visible & non-final classes.
If you are not sure...

Underlying exception : java.lang.IllegalArgumentException: java.lang.ClassCastExcception@1ba9917e
...

Sorry for the short error description, I had to copy by hand from offline terminal.

When I change the argument of the doSomething() method to just doSomething(Bar bar) it succeeds in creating the mock.

One final weird thing, it doesn't reproduce on other workstations.

4
  • 5
    Well, do is a keyword in Java (do while loop). I wouldn't be surprised if naming method with a keyword would cause some weird errors. Commented Feb 28, 2016 at 10:56
  • My mistake, @peter, I meant to write some generic function name. I typed directly without compiler because I can't paste the code..updated Commented Feb 28, 2016 at 12:51
  • @Sagi: You have notified the wrong user, it seems. Commented Feb 28, 2016 at 15:15
  • 1
    "Mockito can only mock visible & non-final classes" that's the problem (in your "real" code). Commented Feb 28, 2016 at 15:24

1 Answer 1

1

After much trial and error, the only difference between the machines was the JDK version. On the machine where it wasn't working we had 1.8.0_05. Updating to 1.8.0_45 fixed it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.