This repository uses the example code from the excellent book "Mock Testing with PowerMock" where the JUnit test have been implemented with the Spock testing framework.
In some cases Spock syntax is used to replace PowerMock and Mockito methods. More importantly, in other cases, Spock test show how to integrate PowerMock with Spock.
The book, "Mock Testing with PowerBack", is available direcctly from PacktPub and Amazon as a both a paper book and Kindle edition.
https://www.packtpub.com/application-development/instant-mock-testing-powermock-instant
- Stubbing
- Mocking
- Verification
No PowerMock methods are used in these tests only Spock 'Stub' and 'Mock'.
- Using a Stub: Should return projected count of employees from the service
- Using a Mock: Should return projected count of employees from the service
- Should return count of employees from the service with default answer
- Use a mock to verify that a method was indeed invoked
How to use PowerMock with Spock using PowerMock's runner delegate.
See: https://medium.com/@WZNote/how-to-make-spock-and-powermock-work-together-a1889e9c5692 And: https://github.com/powermock/powermock/wiki/junit_delegating_runner
- Stub a static method using PowerMock's 'mockStatic' along with 'when' and 'thenReturn'
- Stub all static methods using PowerMock's 'mockStatic' so that all methods do nothing
- Stub a static method using PowerMock's 'mockStatic' to throw an exception
- Using a Stub: Should return projected count of employees from the service
- Using a Mock: Should return projected count of employees from the service
- Should return count of employees from the service with default answer
- Use a mock to verify that a method was indeed invoked
- Stubbing non static methods of a class.
- Concrete class throws an exception when saved
- Stubbed method does not throw an exception when saved
- Stubbed method throws an alternate exception when saved
No PowerMock methods are used in these tests only Spock 'Stub', 'Mock', 'thrown' and 'notThrown'.
- Use Spock Mocking for verification
- Testing for mocked method invocation order
- Should invoke save employee on the service while saving the employee
- Should create new employee if employee is new
- Should update employee if employee exists
- Should invoke 'isNew' before invoking 'create'; Moved the 'isNew' method to a 'then' block
Hint: Mocked stubs used in verification should go in the 'then' blocks
- Stub a final static method.
- Static method verification with PowerMock needs to be in the 'when' block
and not a 'then' block.
- Should get the next employee ID if employee is new
- Mock a constructor to take a specific set of arguments
- Should send welcome email to new employees
Notice the 'whenNew' syntax:
PowerMockito.whenNew().withArguments().thenReturn()
Informs PowerMock that, when a new instance of WelcomeEmail is created, with the stubbed employee instance and "Welcome to Mocking with PowerMock How-to!" text, then return a mock of 'EmployeeEmailService' class.
- Spock's answer to PowerMock's 'Answer' interface
- Should find an employee by startsWith email
- Should return null for any email
- Should return true if employee email is already taken
No PowerMock methods are used in these tests only Spock 'Stub' and 'Mock'.
- Additional use of Spock's answer to PowerMock's 'Answer' interface
- Should find employee by e-mail using an if/else answer cascade
No PowerMock methods are used in these tests only Spock 'Stub' and 'Mock'.
- Use a Spy to verify that SUT methods are invoked
- SUT should invoke the 'createEmployee' method when saving a new employee
No PowerMock methods are used in these tests only Spock 'Stub' and 'Mock'.
- This test differs from Chapter09 in that the access of the EmployeeService 'createEmployee' method has been change from package-private to private.
- Private methods are implicitly final. Spock 'Spy' has no way to implement final methods.
- You could change visibility to protected or package-private or as shown below use PowerMock.
- SUT should invoke the now PRIVATE 'createEmployee' method when saving a new employee
- These tests show how to use PowerMock's 'Whitebox' class to access a private field.
- Private field can be access for Spock due to Grooy's abilty to ignore field access.
Note: That if the field is 'final private' Spock would be able to read it but not set its own variable.
- PowerMock: Should verify that new employee is added to the department
- Spock: Should verify that new employee is added to the department
- PowerMock: Should add new employee to the department
- Spock: Should add new employee to the department
- Supress a constructor; In the this case the default constructor of the super class
- Supress a method in the super class
- Prevent a private static field from initializing
- Should verify that max salary offered for a department is calculated correctly
- Should suppress the base constructor of department
- Should suppress the 'performAudit' method of super class
- Should suppress the default initializer for BaseEntity
- Should suppress the field 'identifier' for BaseEntity
- Suppress a static initializer
- PowerMock: Should return net worth of the department
- Spock: Should return net worth of the department