wujun / powermock

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

Support mock injection via annotations #435

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
There is a feature I love with unitils : the possibility to inject the tested 
object and the mocks.

For example :

@RunWith(UnitilsJUnit4TestClassRunner.class)
public class MyServiceInjectTest { 

    @TestedObject
    private MyService myService; 

    @InjectIntoByType
    private Mock<MyDao> myDaoMock; 

    @Test
    public void testMethod() { 
        // define behavior 
        expect(myDaoMock.doSomething()).andReturn("something"),

        replayAll();
        myService.aService();
        verifyAll();
    } 
}

@TestedObject creates the object to test.
@InjectIntoByType creates a mock, and inject it in the tested object, using its 
type.

We no longer need to use createMock() to create a mock and 
ReflectionTestUtils.setField() to set the mock in a private field, all the work 
is done automatically with only two annotations.

Unfortunately, I can use only one Runner in my JUnit classes, I can't use 
powermock and unitils together.

It would be nice if powermock had such a feature given that it considerably 
simplifies test classes.

Original issue reported on code.google.com by anthony....@gmail.com on 14 Mar 2013 at 4:24

GoogleCodeExporter commented 9 years ago
This actually works already. PowerMock supports Mocktio @Mock and annotations 
etc and it has some extensions to EasyMock that makes annotation works as well. 
 Since PowerMock works as a wrapper around other frameworks I don't have any 
plans to make diverge too much from the underlying frameworks.

Original comment by johan.ha...@gmail.com on 26 Mar 2013 at 7:48