Test listener

The overview

  1. Use on a class@RunWith(PowerMockRunner.class)Annotation.
  2. Use on a class@PowerMockListener({Listener1.class, Listener2.class})Annotation.

The sample

PowerMock 1.1 and later have the concept of a test listener. Test listeners can be used to get events from the test framework, such as the start and end times of test methods and the results of test execution. The purpose of these tests the listener is to provide a method independent of the test framework, in order to pass the org. Powermock. Core. Spi. PowerMockTestListener and pass them to the PowerMockListener annotations for and respond to these notifications. PowerMock has some built-in test listeners for you to use.

AnnotationEnabler

Note: Starting with PowerMock 1.3, you no longer need to specify this listener manually. It is now integrated into the runner and mocks are automatically injected.

This test listener implementation lets you create mock objects using annotations. For example, consider the following code:

@RunWith(PowerMockRunner.class)
@PowerMockListener(AnnotationEnabler.class)
public class PersonServiceTest {

    @Mock
    private PersonDao personDaoMock;

    private PersonService classUnderTest;

    @Before
    public void setUp(a) {
        classUnderTest = newPersonService(personDaoMock); }... }Copy the code

Using the @Mock annotation eliminates the need to manually set up and dismantle the Mock, minimizing repetitive test code and making tests more readable. AnnotationEnabler works with the EasyMock and Mockito APIS. In the EasyMock version, if you want to create a partial mock, you can also provide the name of the method you want to mock, for example:

@RunWith(PowerMockRunner.class)
@PowerMockListener(AnnotationEnabler.class)
public class PersonServiceTest {

    @Mock("getPerson")
    private PersonDao personDaoMock;

    private PersonService classUnderTest;

    @Before
    public void setUp(a) {
        classUnderTest = newPersonService(personDaoMock); }... }Copy the code

This code instructs PowerMock to create the PersonDao and only partially emulate the “getPerson” method. Because EasyMock supports good and strict mocks, you can use the @Mocknice and @MockStrict annotations to get this benefit.

In Mockito, you just use Spy (..) Part of the simulation class or instance.

FieldDefaulter

This test listener implementation can be used to set default values for each member field in the junit test after each test. Creating a tearDown method and emptying all references is almost standard procedure for many developers when using JUnit (you can read more about this issue here). However, you can also do this automatically using FieldDefaulter. For example, suppose you have five collaborators that you want to emulate in a test, and you want to ensure that each collaborator is set to NULL after each test to allow garbage collection. So instead of doing:

@RunWith(PowerMockRunner.class)
public class MyTest {
	
  	private Collaborator1 collaborator1Mock;
  	private Collaborator2 collaborator2Mock;
  	private Collaborator3 collaborator3Mock;
  	private Collaborator4 collaborator4Mock;
  	privateCollaborator5 collaborator5Mock; .@After
  	public void tearDown(a) {
            collaborator1Mock = null;
            collaborator2Mock = null;
            collaborator3Mock = null;
            collaborator4Mock = null;
            collaborator5Mock = null; }... }Copy the code

You can use the FieldDefaulter test listener to get rid of the tear-down method entirely:

@RunWith(PowerMockRunner.class)
@PowerMockListener(FieldDefaulter.class)
public class MyTest {
	
  	private Collaborator1 collaborator1Mock;
  	private Collaborator2 collaborator2Mock;
  	private Collaborator3 collaborator3Mock;
  	private Collaborator4 collaborator4Mock;
  	privateCollaborator5 collaborator5Mock; . }Copy the code