tiebin-zhang / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

TestNG: Mocks annotated with @Mock are not re-initializing on test start #379

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a simple test with 2 test methods
2. Add mocked object annotated with @Mock
3. Add simple expectations to both test methods.
4. Run it

What is the expected output? What do you see instead?
Expected - All tests are passed, but instead it fails.

What version of the product are you using? On what operating system?
1.4.11, under Win7

Simple example:
@Test(groups = { "unit" })
@PrepareForTest
public class SimpleTest {

    @Mock
    List mockList;

    @ObjectFactory
    public IObjectFactory getObjectFactory() {
        return new PowerMockObjectFactory();
    }

    @Test
    public void testAdd() {
        expect(mockList.add("hello")).andReturn(true);
        replay(mockList);

        mockList.add("hello");
    }

    @Test
    public void testDelete() {
        expect(mockList.remove("hello")).andReturn(true);
        replay(mockList);

        mockList.remove("hello");
    }
}

Test fails with:
testDelete
" Unexpected method call List.remove("hello"):"

If you comment one test method - second will pass.

Original issue reported on code.google.com by afilipc...@gmail.com on 13 Mar 2012 at 6:14

GoogleCodeExporter commented 9 years ago
Forgot to mention - test uses easymocks

Original comment by afilipc...@gmail.com on 13 Mar 2012 at 6:26

GoogleCodeExporter commented 9 years ago
It's a bug, a work-around is to extend from PowerMockTestCase.

Original comment by johan.ha...@gmail.com on 26 Mar 2012 at 8:56