dhamini-poornachandra / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

not public abstract class - mocking problem #274

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

//NOT PUBLIC abstract class
abstract class ClassB {
    public boolean doSomething(){   
        return true;
    }
}

public class ClassA extends ClassB{}

//main class
public class SomeClass {
    ClassA classA;

    public Integer someMethod(){
        if (classA.doSomething()){
            return 1;
        }
        return 2;
    }
}

//test class
public class SomeClassTest {

    private SomeClass someClass;

    @Test
    public void shouldReturn1() {

        //given
        when(someClass.classA.doSomething()).thenReturn(true);

        //when
        Integer result = someClass.someMethod();

        //then
        assertThat(result).isEqualTo(1);
    }

    @Before
    public void setUp() {
        someClass = new SomeClass();
        someClass.classA = mock(ClassA.class);
    }
}

What is the expected output? What do you see instead?

This test should run without any errors, but I have an exception:
org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
...

What version of the product are you using? On what operating system?
mockito 1.8.5, ubuntu

Please provide any additional information below.
If I change the visibility of ClassB to public, everything works just fine. 

Why the visibility of ClassB have something to do with proper mocking?

Original issue reported on code.google.com by andrzejl...@gmail.com on 25 Jul 2011 at 9:33

GoogleCodeExporter commented 8 years ago
Yeah we know about it, there is a bug in the bytecode lib we are using.

We could eventually explain it in the exception.

Original comment by brice.du...@gmail.com on 7 Oct 2011 at 8:45