Powermock supports Easymock and Mockito.Ii. introduction to PowermockThe Powermock has two important annotations:– @RunWith (Powermockrunner.class)– @PrepareForTest ({Yourclasswithegstaticmethod.class})If you do not use annotation @preparefortest in your test case, you can do so without annotating @runwith (Powermockrunner.class) and vice versa. When you need to use Powermock powerful features (mock stati
Related framework
JUNIT4, Mockit, Powermock
Related maven dependencies
Code baseThe subsequent test code is for the following class
Import org.springframework.beans.factory.annotation.Autowired;
public class Usercontroller {@Autowired private userservice userservice;
public boolean addUser (Userdto userdto) {int added = Userservice.adduser (userdto);
if (added
Public interface UserService {
int addUser (userdto userdto);
int deluser (int id) throws Exception;
: ' Org.powermock ', Name: ' Powermock-api-mockito2 ', version: ' 1.7.4 '
The Powermock has two important annotations:@RunWith (Powermockrunner.class)@PrepareForTest ({yourclass.class})When you need to use Powermock powerful features (mock static, final, private method, etc.), you need to annotate @preparefortest.
@InjectMocks: Creates an instance where a mock created with the @mock or @spy annotation is i
The last 1 months of the project at the end, in the company to do monomer testing, from the beginning of imitation, accumulated a number of things, recorded, prepared for a rainy day;Jar Package Required:Javassist-3.16.1-ga.jarMockito-all-1.9.5.jarPowermock-api-mockito-1.5.6.jarPowermock-api-support-1.5.6.jarPowermock-core-1.5.6.jarPowermock-module-junit4-1.5.6.jarPowermock-module-junit4-common-1.5.6.jarPowermock-reflect-1.5.6.jarJunit-4.10.jarWhen using a mock, the first thing to do is to add t
This article will briefly describe how to use powermock and Mockito to mock1. Constructors2. Static functions3. Enumeration implementation of the Singleton4. Select the parameter value as the return value of the function5. Change the value of the method parameter in the call to the mock-out methodA brief explanation: Mockito has actually been able to meet most of the requirements, but its implementation mechanism is to use Cglib to dynamically create an instance of the interface class. However,
. printstacktrace ();}Return result;}Public adder getadder (){Return adder;}Public void setadder (adder ){This. adder =;}}
For the previous sayhi class, the mock object cannot be passed in. 100% became an impossible task.
At this point, one of our options is to modify the source code. In object-oriented languages, we have always stressed flexible and independent code structures. If a class is hard to be tested, it may be a symbol of poor code structure.
Obviously, the sayhi method depen
The previous article describes how to let Surefire,failsafe and Jacoco maven plugin How to combine configuration, this article specifically explains how to use the Powermock agent to solve coverage incompatibility problems.First add Powermock agent maven dependency in POM
Note:it ' s recommended that's put powermock-module-junit4-rule-agent before JUnit in the classpath. (Remember to put this dependency before JUnit).
Next, modify the previous test code:Implementation prior to modification
@Ru
Note that the first two lines of the 2 annotations @RunWith and @PrepareForTest
The @RunWith is simple, the back is always Powermockrunner.class
@PrepareForText need to add the class name of the calling constructor, not the class itself that has the constructor.
In the following example, the class we want to test is: helper, which invokes the constructor of the Somthing class in the helper class to create the instance.
@RunWith (Powermockrunner.cl
it, please replace it with another try @PrepareForTest (User.class) Public class TestUser extends TestCase {user user; Test the static method @Test public void Testverify () {mockstatic (user.class); When (User.getinstance ()). Thenreturn (New Subuser ()); User user=user.getinstance ();Verifystatic ();}Testing a new Object @Test public void Testwhennew () { try { mockstatic (user.class); Whennew (User.class). Withnoarguments (). Thenreturn (New
1.
The following exception appears
Org.mockito.exceptions.misusing.InvalidUseOfMatchersException:Misplaced argument Matcher detected here:-> at Beyond.tools.util.BeyondExcelUtilsTest.testDoMain (beyondexcelutilstest.java:44)You are cannot use argument matchers outside of verification or stubbing.Examples of correct usage of argument matchers:When (Mock.get (Anyint ())). Thenreturn (NULL);Dothrow (New RuntimeException ()). When (mock). Somevoidmethod (Anyobject ());Verify (mock). SomeMethod (Cont
now familiar to everyone. Currently, Powermock only extends EasyMock and Mockito, and needs to be used with EasyMock or Mockito mates.Powermock can be downloaded in https://code.google.com/p/powermock/, this article takes Powermock+mockito as an example, using MAVEN, add the following dependencies, MAVEN will automatically introduce the Mockito package.Now for example the use of Powermock, suppose there is a class that needs to be modeled as follows:public class Class2mocked {public static i
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.