org.powermock.core.classloader.annotations
Annotation Type Mock


Deprecated. Use Mock annotation in respective extension API instead. This annotation will be removed in an upcoming release.

@Target(value=FIELD)
@Retention(value=RUNTIME)
@Documented
public @interface Mock

This annotation can be placed on those fields in your test class that should be mocked. This eliminates the need to setup and tear-down mocks manually which minimizes repetitive test code and makes the test more readable. In order for PowerMock to control the life-cycle of the mocks you must supply the PowerMockListener annotation to the class-level of the test case. For example:

 ...
 @PowerMockListener(AnnotationEnabler.class)
 public class PersonServiceTest {
 
        @Mock
        private PersonDao personDaoMock;
 
        private PersonService classUnderTest;
 
        @Before
        public void setUp() {
                classUnderTest = new PersonService(personDaoMock);
        }
  ...
 }
 

Note that you can also create partial mocks by using the annotation. Let's say that the PersonService has a method called "getPerson" and another method called "savePerson" and these are the only two methods that you'd like to mock. Rewriting the previous example to accommodate this will give us the following test:

 ...
 @PowerMockListener(EasyMockAnnotationEnabler.class)
 public class PersonServiceTest {
 
        @Mock({"getPerson", "savePerson"})
        private PersonDao personDaoMock;
 
        private PersonService classUnderTest;
 
        @Before
        public void setUp() {
                classUnderTest = new PersonService(personDaoMock);
        }
  ...
 }
 


Optional Element Summary
 String[] value
          Deprecated.  
 

value

public abstract String[] value
Deprecated. 
Default:
""


Copyright © 2007-2012. All Rights Reserved.