mlinhard / mockito

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

Create a TestNG Listener that does the same job as the MockitoJUnitRunner #304

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Since TestNG only instantiates your test class once and not for each test case 
like JUnit does, you can not use MockitoAnnotations.initMocks(this) just by 
itself with constructor injection.
http://martinfowler.com/bliki/JunitNewInstance.html
http://beust.com/weblog/2004/08/25/testsetup-and-evil-static-methods/

The issue appears to be that FieldInitializer.acquireFieldInstance() checks to 
see if the field is null or not. If it is not null then it will instantiate it 
otherwise you are left with the instance from the previous test case.
So if you have a test class that has multiple test cases, the first one will 
work as expected and the subsequent tests may get unexpected behavior.

Example with TestNG:    

package org.mockitousage.annotation;

import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockitousage.examples.use.ArticleCalculator;
import org.mockitousage.examples.use.ArticleDatabase;
import org.mockitousage.examples.use.ArticleManager;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;

@Test
public class MockInjectionUsingConstructorUsingTestNGTest {

    @Mock private ArticleCalculator calculator;
    @Mock private ArticleDatabase database;

    @InjectMocks private ArticleManager articleManager;

    @BeforeMethod
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    public void initMocks_should_create_all_new_objects_each_time_execution_1() {
        articleManager.updateArticleCounters("new");

        verify(calculator).countArticles(eq("new"));
    }

    // this test will fail because the Mocks injected into ArticleManager will be from the previous test case
    public void initMocks_should_create_all_new_objects_each_time_execution_2() {
        articleManager.updateArticleCounters("new");

        verify(calculator).countArticles(eq("new"));
    }
}

Ideally @InjectMocks would create a new instance each time 
MockitoAnnotations.initMocks is called. But this could cause problems for test 
cases that inline the field initialization.
Example:
    @InjectMocks private ArticleManager articleManager = new ArticleManager();

I can work around the issue by changing the setup to be something like the 
following but it would be nice to not have to.
    @BeforeMethod
    public void setup() {
        articleManager = null;
        MockitoAnnotations.initMocks(this);
    }

Original issue reported on code.google.com by kdomb...@gmail.com on 23 Dec 2011 at 10:11

Attachments:

GoogleCodeExporter commented 9 years ago
@Brice.  Sorry, I just noticed it under the subprojects.  I assume those are 
the "blessed" versions and will use those instead.  Thanks.

Original comment by adam.n.g...@gmail.com on 19 Jul 2012 at 4:23

GoogleCodeExporter commented 9 years ago
Yes, you can use them :)
They work well, though they might not be free of bugs.

Cheers

Original comment by brice.du...@gmail.com on 20 Jul 2012 at 9:49

GoogleCodeExporter commented 9 years ago

Original comment by brice.du...@gmail.com on 3 Sep 2012 at 10:00

GoogleCodeExporter commented 9 years ago
Sorry I don't get it. Is the testng support released in some binary? Or am I 
supposed just to copy the code on put it in my tests?

Original comment by dariusz....@googlemail.com on 17 Mar 2014 at 3:35

GoogleCodeExporter commented 9 years ago
Hi Darius

Yes at the moment the code is not released, just copy paste the 3 classes in 
your test core library.
At some point it will be released. But there is many things to do before that.

Cheers,
Brice

Original comment by brice.du...@gmail.com on 13 Apr 2014 at 1:16

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 16 Aug 2014 at 2:43

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 24 Aug 2014 at 3:14

GoogleCodeExporter commented 9 years ago

Original comment by szcze...@gmail.com on 24 Aug 2014 at 3:50