In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Just left recently, at the end of the project one month before leaving, do a single test in the company, start with imitation, accumulate something, record it, and reserve it for future needs from time to time When I was doing it, I also wanted to study deeply in searches such as Google. I have an impression that the relevant books in China are relatively old, generally around 2004, and they are not relevant to what they are doing now. It may be useful to have a look at them. The principle should be the same, but there are no examples and api that is consistent with the technology used. It feels like a waste of time. The questions on api and stackoverflow on Google are more reliable and close to work, but they are all in English. Some of them can be understood roughly, but it is not enough to pick up the details. Here are some problems encountered in my work. I made a summary by myself. I may do a single test one day, or I can pick it up quickly.
The jar package is required:
Javassist-3.16.1-GA.jar
Mockito-all-1.9.5.jar
Powermock-api-mockito-1.5.6.jar
Powermock-api-support-1.5.6.jar
Powermock-core-1.5.6.jar
Powermock-module-junit4-1.5.6.jar
Powermock-module-junit4-common-1.5.6.jar
Powermock-reflect-1.5.6.jar
Junit-4.10.jar
When using mock, first add the annotation @ PrepareForTest ({class name .class}) to the test class, where the "class name .class" is the class called in the class and needs to be mock.
When you need a private method called by a mock private method, you need to add @ RunWith (PowerMockRunner.class). You don't need to add the rest, and you can use new when you create it.
Public calls the private method with a return value
PowerMockito.when (object, "private method name", parameter) .thenReturn (expected value); this method needs to add @ RunWith (PowerMockRunner.class). Instead of using new directly when creating an object of a class, you need to use PowerMockito.Mock (class name .class); and you need to put the class name .class in the note @ class ({class name}) on the class.
Replace objects in the original code with mock objects
E.g:
Public class AA {
Private User u
Public void A () {
U.getName ()
}
}
@ PrepareForTest ({User.class})
Public class test {
@ Test
Public void test_001 () {
AA a = new AA ()
User U1 = PowerMockito.Mock (User.class)
Whitebox.setInternalState (a, "u", U1)
PowerMockito.when (u1.getName ()) .thenReturn (value)
}
}
4. To mock a method called by a method within a class
E.g:public class AA {
Private User u
Private Ver v
Public void getData () {
String a = u.getName ()
String b = v.getAge ()
}
}
Testing process: ①. First mock the entire class
two。 Then call the method
3. Then the class called by mock
4. The name of the new object replaces the name in the code
5. Mock the method
6. Adjust the method.
E.g: ① .AA a = PowerMockito.mock (AA.class)
②. PowerMockito.doCallRealMethod.when (a) .getData ()
③ .User U1 = PowerMockito.mock (User.class)
④ .Whitebox.setInternalState (a, "u", U1)
⑤ .PowerMockito.when (u1.getName ()) .thenReturn (value)
⑥ .a.getData ()
5. Abnormal test in test
PowerMockito.doThrow (new Exception ()). When (object). GetName ()
New Exception () is the exception you expect, and getName is the way to get an exception.
6. Test exception instance
E.g:public class A {
Private U u = null
Public void putData (Data d) {
Try {
U.putData (parameters, parameters)
} catch (AccessException e) {
Throw new xxxxException (eMagna....)
}
}
}
Test class
@ prepareForTest {(A.classma U.class)}
Public class ATest {
@ Test
Public void test_001 () {
Data d = new Data ()
D.setName ("a")
An a = PowerMockito.mock (A.class)
PowerMockito.docallRealMethod.when (a) .putData (d)
U u = PowerMockito.mock (U.class)
Whitebox.setInteralState (a, u, u)
Try {
PowerMockito.doThrow (new AccessException ()). When (u) .putData (Mockito.any (... class), Mockito.any (.. class))
A.putData (d)
} catch (AccessException e) {
AssertTrue (e instaceof xxxException)
AssertEquals (e.getException.getMessage (), "xxxxxxx")
}
}
}
Private methods called in the mock public method
An a = PowerMockito.mock (A.class)
PowerMockito.doCallRealMethod (). When (a). PutData (); / / putData () is the method of A.
PowerMockito.when (a, "getName ()", value.getId (), value.getName ()) .thenReturn ("abc"); / / getName is a private method, and value.getId (), value.getName () are method parameters
Parameters depend on what is used in the code, and if it is in the form of value.getId () in the code and in the test code, otherwise it will not be able to return the expected value.
7. When mock the method, you should pay attention to the consistency of the writing method.
Erroneous writing:
PowerMockito.doReturn (xx). When (instance,method (args))
①, PowerMockito.doReturn (xx). When (instance). Method name (args)
②, PowerMockito.when (instance. Method name (args); .thenReturn (xx)
In mock, the parameter advantage of the method will encounter problems (cannot be created), which can be solved by using Mockito.any () and mockito.eq ().
The e.g:Mockito.any (User.class) / / parameter is an object. You can use any, which is the class. Class
Mockito.eq (field name); / / the parameter is a string and other types; use eq
8. When mock a method, the global member variable used in the method is always null (in fact, the initial value is set when the member variable is initialized, but it is null), and the member variable is assigned a value in case.
Use Whitebox.setInternalState (instance, "Field name in Code", new name)
You can assign a value to the new name and pass it in.
9. In the B method of Class A, there is a call to the private method C of the same class, and the value needs to be returned. (this method can be called a big move, and it has a wonderful effect on testing private methods and mock private methods! )
Public void test_001 () {
An a = PowerMockito.mock (A.class)
PowerMockito.when (a, "c", arg1,arg2) .thenReturn ("01")
Whitebox.invokeMethod (a, "c", arg1,arg2); / / c is the private method name
PowerMockito.when (a, "B", args) .thencallRealMethod ()
Whitebox.invokeMethod (a, "B", args)
}
10. The constructor of mock class A (sometimes the constructor may have very complex processing), but you need to create a fake object to use
@ RunWith (PowerMockRunner.class)
@ PrepareForTest ({A.class.B.class})
Public class BTest {
@ Test
Public void test_001 () {
B b = PowerMockito.Mock (B.class)
PowerMockito.doCallRealMethod () .when (b) .getName (id)
An a = PowerMockito.mock (A.class)
PowerMockito.whenNew (A.class) .witharguments (Mockito.any (C.class), Mockito.any (D.class)) .thenReturn (a)
}
}
11. In the B method of class A, class C (local) is new, and the method of class C is called with C object.
Methods for mock local objects:
An a = new A ()
C c = new C ()
PowerMockito.whenNew (C.class). WhthArguments (). ThenReturn (c)
C.getName ()
12.mock static method
@ RunWith (PowerMockRunner.class)
@ PrepareForTest ({A.class})
Public class ATest {
@ Test
Public void test_static_001 () {
PowerMockito.mockStatic (A.class)
PowerMockito.when (A.getName ()) .thenReturn ("01")
}
}
Method 2 of 13.mock global variables
Public class A {
Private User u
}
Use Powermockito.field (xx.class, "field") .set (object, the value you want to assign)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.