Note that there are some explanatory texts on larger screens.

plurals
  1. PODependency verify functions call mockito
    primarykey
    data
    text
    <p>I want create unit test to test a class which contains another object. I have created a mock to this second object.</p> <p>When I test a method of my first class, I want verify if the functions of the second object are called. For this, I use the function <code>verify(mock).myfunction();</code></p> <p>My problem is that the same function of my object 2 can be called by several function of my first class. </p> <p>When I write the test class, I write a test function by function but it seems that the "times" isn't reset at the beginning of a test method.</p> <p>I don't know if I am clear, therefore, an example :</p> <pre><code>public class Main { public Object o = createObject(); public void function1(){ o.function(); } public void function2(){ o.function(); } public Object createObject() { return new Object() ; } public class MainTest { private static Main main; @BeforeClass public static void setUp() throws Exception { final Object mockO = mock(Object.class); main = new Main() { @Override public Object createObject() { return mockO; } }; } @Test public void testfunction1(){ verify(main.world[0], never()).function(); main.function1(); verify(main.world[0]).function(); } @Test public void testfunction2(){ verify(main.world[0], never()).function(); main.function2(); verify(main.world[0]).function(); } </code></pre> <p>If I test <code>testfunction1()</code> and <code>testfunction2()</code> ignored, it's work.</p> <p>If I test <code>testfunction2()</code> and <code>testfunction1()</code> ignored, it's work.</p> <p>But if the two tests are executed, I have an error : </p> <pre><code>org.mockito.exceptions.verification.NeverWantedButInvoked: Object.function(); Never wanted here : -&gt; at test.MainTest.testfunction1 but invoked here : at source.Main.function2 </code></pre> <p>How I can test independently the two functions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload