|
1 | 1 | package io.github.dunwu.javalib.bean; |
2 | 2 |
|
| 3 | +import lombok.Cleanup; |
| 4 | +import org.junit.Assert; |
| 5 | +import org.junit.Test; |
| 6 | + |
3 | 7 | import java.io.ByteArrayOutputStream; |
4 | 8 | import java.io.IOException; |
5 | 9 | import java.util.ArrayList; |
6 | 10 | import java.util.List; |
7 | 11 |
|
8 | | -import org.junit.Assert; |
9 | | -import org.junit.Test; |
10 | | - |
11 | | -import lombok.Cleanup; |
12 | | - |
13 | 12 | /** |
14 | 13 | * Lombok 单元测试 |
| 14 | + * |
15 | 15 | * @see @see http://jnb.ociweb.com/jnb/jnbJan2010.html |
16 | 16 | * @author Zhang Peng |
17 | 17 | */ |
18 | 18 | public class LombokTest { |
19 | | - @Test |
20 | | - public void testData() { |
21 | | - Person huangshiren = new Person(); |
22 | | - huangshiren.setName("黄世仁"); |
23 | | - huangshiren.setAge(30); |
24 | | - huangshiren.setSex("男"); |
25 | | - Person yangbailao = new Person(); |
26 | | - yangbailao.setName("杨白劳"); |
27 | | - yangbailao.setAge(50); |
28 | | - yangbailao.setSex("男"); |
29 | | - Person xiaobaicai = new Person(); |
30 | | - xiaobaicai.setName("小白菜"); |
31 | | - xiaobaicai.setAge(20); |
32 | | - xiaobaicai.setSex("女"); |
33 | 19 |
|
34 | | - List<Person> personList = new ArrayList<>(); |
35 | | - personList.add(yangbailao); |
36 | | - personList.add(xiaobaicai); |
| 20 | + @Test |
| 21 | + public void testData() { |
| 22 | + Person huangshiren = new Person(); |
| 23 | + huangshiren.setName("黄世仁"); |
| 24 | + huangshiren.setAge(30); |
| 25 | + huangshiren.setSex("男"); |
| 26 | + Person yangbailao = new Person(); |
| 27 | + yangbailao.setName("杨白劳"); |
| 28 | + yangbailao.setAge(50); |
| 29 | + yangbailao.setSex("男"); |
| 30 | + Person xiaobaicai = new Person(); |
| 31 | + xiaobaicai.setName("小白菜"); |
| 32 | + xiaobaicai.setAge(20); |
| 33 | + xiaobaicai.setSex("女"); |
| 34 | + |
| 35 | + List<Person> personList = new ArrayList<>(); |
| 36 | + personList.add(yangbailao); |
| 37 | + personList.add(xiaobaicai); |
| 38 | + |
| 39 | + Company company = Company.of(huangshiren); |
| 40 | + company.setName("黑心农产品公司"); |
| 41 | + company.setEmployees(personList); |
37 | 42 |
|
38 | | - Company company = Company.of(huangshiren); |
39 | | - company.setName("黑心农产品公司"); |
40 | | - company.setEmployees(personList); |
| 43 | + System.out.println("公司名:" + company.getName()); |
| 44 | + System.out.println("创始人:" + company.getFounder()); |
| 45 | + System.out.println("员工信息"); |
| 46 | + company.getEmployees().forEach(person -> { |
| 47 | + System.out.println(person.toString()); |
| 48 | + }); |
| 49 | + } |
41 | 50 |
|
42 | | - System.out.println("公司名:" + company.getName()); |
43 | | - System.out.println("创始人:" + company.getFounder()); |
44 | | - System.out.println("员工信息"); |
45 | | - company.getEmployees().forEach(person -> { |
46 | | - System.out.println(person.toString()); |
47 | | - }); |
48 | | - } |
| 51 | + @Test |
| 52 | + public void testCleanUp() { |
| 53 | + try { |
| 54 | + @Cleanup |
| 55 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 56 | + baos.write(new byte[] { 'Y', 'e', 's' }); |
| 57 | + System.out.println(baos.toString()); |
| 58 | + } |
| 59 | + catch (IOException e) { |
| 60 | + e.printStackTrace(); |
| 61 | + } |
| 62 | + } |
49 | 63 |
|
50 | | - @Test |
51 | | - public void testCleanUp() { |
52 | | - try { |
53 | | - @Cleanup |
54 | | - ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
55 | | - baos.write(new byte[] { 'Y', 'e', 's' }); |
56 | | - System.out.println(baos.toString()); |
57 | | - } catch (IOException e) { |
58 | | - e.printStackTrace(); |
59 | | - } |
60 | | - } |
| 64 | + @Test |
| 65 | + public void testToString() { |
| 66 | + Person person = new Person(); |
| 67 | + person.setName("张三"); |
| 68 | + person.setAge(20); |
| 69 | + person.setSex("男"); |
| 70 | + System.out.println(person.toString()); |
| 71 | + // output: Person(name=张三, sex=男) |
| 72 | + } |
61 | 73 |
|
62 | | - @Test |
63 | | - public void testToString() { |
64 | | - Person person = new Person(); |
65 | | - person.setName("张三"); |
66 | | - person.setAge(20); |
67 | | - person.setSex("男"); |
68 | | - System.out.println(person.toString()); |
69 | | - // output: Person(name=张三, sex=男) |
70 | | - } |
| 74 | + @Test |
| 75 | + public void testEqualsAndHashCode() { |
| 76 | + Person person = new Person(); |
| 77 | + person.setName("张三"); |
| 78 | + person.setAge(20); |
| 79 | + person.setSex("男"); |
71 | 80 |
|
72 | | - @Test |
73 | | - public void testEqualsAndHashCode() { |
74 | | - Person person = new Person(); |
75 | | - person.setName("张三"); |
76 | | - person.setAge(20); |
77 | | - person.setSex("男"); |
| 81 | + Person person2 = new Person(); |
| 82 | + person2.setName("张三"); |
| 83 | + person2.setAge(18); |
| 84 | + person2.setSex("男"); |
78 | 85 |
|
79 | | - Person person2 = new Person(); |
80 | | - person2.setName("张三"); |
81 | | - person2.setAge(18); |
82 | | - person2.setSex("男"); |
| 86 | + Person person3 = new Person(); |
| 87 | + person3.setName("李四"); |
| 88 | + person3.setAge(20); |
| 89 | + person3.setSex("男"); |
83 | 90 |
|
84 | | - Person person3 = new Person(); |
85 | | - person3.setName("李四"); |
86 | | - person3.setAge(20); |
87 | | - person3.setSex("男"); |
| 91 | + Assert.assertEquals(person, person2); |
| 92 | + Assert.assertNotEquals(person, person3); |
| 93 | + } |
88 | 94 |
|
89 | | - Assert.assertEquals(person, person2); |
90 | | - Assert.assertNotEquals(person, person3); |
91 | | - } |
92 | 95 | } |
0 commit comments