http://abhinandanmk.blogspot.com/2012/06/jmockit-tutoriallearn-it-today-with.html
JMockit is one of the many mocking frameworks available for unit testing in Java. If you have to unit test you'll invariably end up mocking objects for third party classes and for stubbing.
I assume you already know JUnit. We'll see step by step in very short tutorials that'll help you start using jmockit. I'll be trying to keep each one as crisp as possible. If you want more details.You can ask them in comments section below...
I am yet to understand some of the JMockit features completely, so please bear with me if I have not covered it in entirety.
- What is Mocking in unit Testing?
- How does JMockit support mocking?
- How to add JMockit to an eclipse project?
- How to run a test case with JMockit?
- How to mock default constructors(or constructors with no parameters) in JMockit?
- How to mock constructors with parameters (or those that take arguments) ?
- How to mock static initialization blocks in JMockit?
- How to mock public methods in JMockit
- How to mock private methods in JMockit?
- How to mock static methods in JMockit?
- How to invoke or test private methods in JMockit?
- How to throw exceptions in an Expectations block?
- Some more JMockit utilities before signing off.
How to mock constructors in JMockit?
Consider the code below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| /************************* Person.java ****************/ public class Person { private String name; public Person(){ name = "Default Abhi" ; } public String getName() { return name; } public void setName(String name) { this .name = name; } } /******************* PersonTestConstructor.java ***********/ public class PersonTestConstructor { @Test public void testGetName() { new MockUp<Person>() { @Mock public void $init() { //Dont assign name variable at all //Leave it null } }; Person p = new Person(); String name = p.getName(); assertNull( "Name of person is null" ,name); } } |
Remember the to put the @Mock annotation above. I have spent hours trying to debug only to find out that I forgot.
How to mock constructors with parameters (or those that take arguments) ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| /************************* Person.java ****************/ public class Person { private String name; public Person(String name){ this .name = name; } public String getName() { return name; } public void setName(String name) { this .name = name; } } /******************* PersonTestConstructor.java ***********/ public class PersonTestConstructor { @Test public void testGetName() { new MockUp<Person>() { @Mock public void $init(String name) { //Dont assign name variable at all //Leave it null } }; Person p = new Person( "AbhiJMockit" ); String name = p.getName(); System.out.println(name); assertNull( "Name of person is null" ,name); } } |
How to mock static initialization blocks in JMockit?
In the following example the static initialization block calls a static method and sets the bankBalance to 100. But in our test case we want to start off with 500.
Note the $clinit method in the BankTest class that calls the same method with 500 as the argument.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
| /***************** Bank.java ******************/ public class Bank { static int balanceAmount; //Static block begins static { updateBalance( 100 ); } public static void updateBalance( float balance) { balanceAmount += balance; } } /*********************** BankTest.java **********/ public class BankTest { @Test public void testBankStaticBlock(){ new MockUp<Bank>(){ @SuppressWarnings ( "unused" ) @Mock public void $clinit(){ Bank.updateBalance( 500 ); } }; assertEquals( "The balance amount is 500" , 500 , Bank.balanceAmount); } } |
Комментариев нет:
Отправить комментарий