Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito fails in Maven but succeeds in Eclipse

We have a test wich fails when executed in Maven, but succeeds in Eclipse.

Basically the problem is, that when executed with Maven Mockito fails to mock a method which comes from a superclass from another maven module with package private modifier.

Questions

  1. Why is this so?
  2. Is this a known bug? If not where to file it? surefire, mockito ...?
  3. how to fix it?

I have found a description of a similar problem, with the recommended fix to use surefire-2.7.1 instead of 2.7.0 but we are already on 2.10 (and also see the problem in 2.16)

Obviously the simplest solution would be to make the BaseClass public, but we can't do it, since it is not under our control.

Another alternative would be to overwrite close in MockedClass, which would be ugly but possible.

The error message is

failsCallingOriginalMethod(ModifierTest)  Time elapsed: 0.156 sec  <<< ERROR!
java.lang.RuntimeException: must not have called me

Relevant Code

The real code is not in the default package, but all code is in the same package; import statements removed for brevity.

Maven Module 1

public class ModifierTest
{
    @Test
    public void failsCallingOriginalMethod()
    {
        MockedClass mock = Mockito.mock(MockedClass.class);
        doNothing().when(mock).close();
    }
}

Maven Module 2

public class MockedClass extends BaseClass
{
}

class BaseClass
{
    public void close()
    {
        throw new RuntimeException("must not have called me");
    }
}

Versions of stuff involved

Maven Version 3.0.5 (can't change that due to other bugs)

Oracle JDK 1.6.0_20 (reproducable with IBM JDK 1.5)

Mockito 1.95

surefire plugin 2.10 (reproducable with 2.16)

like image 329
Jens Schauder Avatar asked Dec 10 '25 17:12

Jens Schauder


1 Answers

Do you have control over MockedClass?

If so, you could consider adding a delegating method in MockedClass:

public void close() {
    super.close();
}

This doesn't solve the issue, but it's a quick workaround.

like image 75
vikingsteve Avatar answered Dec 13 '25 08:12

vikingsteve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!