Sfoglia il codice sorgente

refactored some of my test classes. Im think im getting better at TDD

Nicholas Maidanos 8 anni fa
parent
commit
69f842aab9

+ 1
- 1
src/main/java/com/zipcoder/payment/Check.java Vedi File

@@ -25,7 +25,7 @@ public class Check implements Payment {
25 25
     }
26 26
 
27 27
     public String getShortDescription() {
28
-        return String.format("Check %s ****%d", this.getPayerName(), lastFourDigits());
28
+        return String.format("Check %s ****%d\n", this.getPayerName(), lastFourDigits());
29 29
     }
30 30
 
31 31
     public int lastFourDigits(){

+ 1
- 1
src/main/java/com/zipcoder/payment/CreditCard.java Vedi File

@@ -61,7 +61,7 @@ public class CreditCard implements Payment {
61 61
     }
62 62
 
63 63
     public String getShortDescription() {
64
-        return String.format("CC %s %d %d/%d", this.payerName, this.lastFourDigits(), this.expiredMonth, this.expiredYear);
64
+        return String.format("CC %s %d %d/%d\n", this.payerName, this.lastFourDigits(), this.expiredMonth, this.expiredYear);
65 65
     }
66 66
 
67 67
     public int lastFourDigits(){

+ 1
- 1
src/main/java/com/zipcoder/payment/Paypal.java Vedi File

@@ -40,7 +40,7 @@ public class Paypal implements Payment {
40 40
 
41 41
 
42 42
     public String getShortDescription() {
43
-        return String.format("Paypal %s %s", this.payerName, this.email);
43
+        return String.format("Paypal %s %s\n", this.payerName, this.email);
44 44
     }
45 45
 
46 46
     public int compareTo(Payment o) {

+ 39
- 58
src/test/java/com/zipcoder/payment/CheckTest.java Vedi File

@@ -1,52 +1,70 @@
1 1
 package com.zipcoder.payment;
2 2
 
3 3
 import static org.junit.Assert.*;
4
+import org.junit.Before;
4 5
 import org.junit.Test;
5 6
 
6 7
 public class CheckTest {
7 8
 
9
+    Check ck;
10
+
11
+    @Before
12
+    public void before(){
13
+        this.ck = new Check(45, "Nicholas M", "445784", "4451884");
14
+    }
15
+
8 16
     @Test
9 17
     public void getId() {
10
-        //When
11
-        Check ck = new Check();
12
-        ck.setId(4456);
13
-
14 18
         //Expect
15
-        long expected = 4456;
19
+        long expected = 45;
16 20
 
17 21
         //Actual
18 22
         long actual =  ck.getId();
19
-
20 23
         assertEquals(expected, actual);
21 24
     }
22 25
 
23 26
     @Test
24
-    public void getPayerName() {
27
+    public void setId(){
25 28
         //When
26
-        Check ck = new Check();
27
-        ck.setPayerName("Nicholas");
29
+        ck.setId(50);
28 30
 
29 31
         //Expect
30
-        String expected = "Nicholas";
32
+        long expect = 50;
31 33
 
32 34
         //Actual
33
-        String acual = ck.getPayerName();
34
-        assertEquals(expected, acual);
35
+        long actual = ck.getId();
36
+        assertEquals(expect, actual);
35 37
     }
36 38
 
37 39
     @Test
38
-    public void getShortDescription() {
39
-    }
40
+    public void getPayerName() {
41
+        //Expect
42
+        String expected = "Nicholas M";
43
+
44
+        //Actual
45
+        String actual = ck.getPayerName();
40 46
 
47
+        assertEquals(expected, actual);
48
+    }
41 49
 
42 50
     @Test
43
-    public void getRoutingNumber() {
51
+    public void setPayerName(){
44 52
         //When
45
-        Check ck = new Check();
46
-        ck.setRoutingNumber("4532353");
53
+        ck.setPayerName("Dan Smith");
47 54
 
48 55
         //Expect
49
-        String expected = "4532353";
56
+        String expect = "Dan Smith";
57
+
58
+        //Actual
59
+        String actual = ck.getPayerName();
60
+
61
+        assertEquals(expect, actual);
62
+    }
63
+
64
+    @Test
65
+    public void getRoutingNumber() {
66
+        //Expect
67
+        String expected = "445784";
50 68
 
51 69
         //Actual
52 70
         String actual = ck.getRoutingNumber();
@@ -57,7 +75,6 @@ public class CheckTest {
57 75
     @Test
58 76
     public void setRoutingNumber() {
59 77
         //When
60
-        Check ck = new Check();
61 78
         ck.setRoutingNumber("4532353");
62 79
 
63 80
         //Expect
@@ -71,7 +88,6 @@ public class CheckTest {
71 88
     @Test
72 89
     public void setAccountNumber() {
73 90
         //When
74
-        Check ck = new Check();
75 91
         ck.setAccountNumber("4532353");
76 92
 
77 93
         //Expect
@@ -83,41 +99,9 @@ public class CheckTest {
83 99
     }
84 100
 
85 101
     @Test
86
-    public void setPayerName() {
87
-        //When
88
-        Check ck = new Check();
89
-        ck.setPayerName("Nick M");
90
-
91
-        //Expect
92
-        String expected = "Nick M";
93
-
94
-        //Actual
95
-        String actual = ck.getPayerName();
96
-        assertEquals(expected,actual);
97
-    }
98
-
99
-    @Test
100
-    public void setId() {
101
-        //When
102
-        Check ck = new Check();
103
-        ck.setId(3423);
104
-
105
-        //Expect
106
-        long expected = 3423;
107
-
108
-        //Actual
109
-        long actual =  ck.getId();
110
-
111
-        assertEquals(expected, actual);
112
-    }
113
-
114
-    @Test
115 102
     public void getAccountNumber() {
116
-        //When
117
-        Check ck = new Check();
118
-        ck.setAccountNumber("893456");
119 103
         //Expect
120
-        String expected = "893456";
104
+        String expected = "4451884";
121 105
 
122 106
         //Actual
123 107
         String actual = ck.getAccountNumber();
@@ -127,12 +111,9 @@ public class CheckTest {
127 111
     }
128 112
 
129 113
     @Test
130
-    public void getShortDescription1() {
131
-        //When
132
-        Check ck = new Check(4563, "Nicholas Maidanos", "45335665", "444545");
133
-
114
+    public void getShortDescriptionTest() {
134 115
         //Expect
135
-        String expected = "Check Nicholas Maidanos ****4545";
116
+        String expected = "Check Nicholas M ****1884\n";
136 117
 
137 118
         //Actual
138 119
         String actual = ck.getShortDescription();

+ 39
- 43
src/test/java/com/zipcoder/payment/CreditCardTest.java Vedi File

@@ -7,14 +7,17 @@ import org.junit.Test;
7 7
 
8 8
 public class CreditCardTest {
9 9
 
10
+    CreditCard cc;
11
+
12
+    @Before
13
+    public void begin(){
14
+        this.cc = new CreditCard(12, 4111131,"Nicholas Maidanos", 4, 2011);
15
+    }
16
+
10 17
     @Test
11 18
     public void getIdTest(){
12
-        //When
13
-        CreditCard cc = new CreditCard();
14
-        cc.setId(100);
15
-
16 19
         //Expect
17
-        long expected = 100;
20
+        long expected = 12;
18 21
 
19 22
         //Actual
20 23
         long actual =  cc.getId();
@@ -25,7 +28,6 @@ public class CreditCardTest {
25 28
     @Test
26 29
     public void setIdTest(){
27 30
         //When
28
-        CreditCard cc = new CreditCard();
29 31
         cc.setId(100);
30 32
 
31 33
         //Expect
@@ -40,10 +42,6 @@ public class CreditCardTest {
40 42
 
41 43
     @Test
42 44
     public void getNameTest(){
43
-        //When
44
-        CreditCard cc = new CreditCard();
45
-        cc.setPayerName("Nicholas Maidanos");
46
-
47 45
         //Expect
48 46
         String expected = "Nicholas Maidanos";
49 47
 
@@ -54,9 +52,33 @@ public class CreditCardTest {
54 52
     }
55 53
 
56 54
     @Test
57
-    public void SetNumTest(){
55
+    public void setNameTest(){
56
+        //When
57
+        cc.setPayerName("Dan Smith");
58
+
59
+        //Expect
60
+        String expect = "Dan Smith";
61
+
62
+        //Actual
63
+        String actual = cc.getPayerName();
64
+
65
+        assertEquals(expect, actual);
66
+    }
67
+
68
+    @Test
69
+    public void getNumTest(){
70
+        //Expect
71
+        long expected = 4111131;
72
+
73
+        //Actual
74
+        long actual = cc.getNumber();
75
+
76
+        assertEquals(expected, actual);
77
+    }
78
+
79
+    @Test
80
+    public void setNumTest(){
58 81
         //When
59
-        CreditCard cc = new CreditCard();
60 82
         cc.setNumber(411111111);
61 83
 
62 84
         //Expect
@@ -70,12 +92,8 @@ public class CreditCardTest {
70 92
 
71 93
     @Test
72 94
     public void getMonthTest(){
73
-        //When
74
-        CreditCard cc = new CreditCard();
75
-        cc.setExpiredMonth(12);
76
-
77 95
         //Expect
78
-        int expected = 12;
96
+        int expected = 4;
79 97
 
80 98
         //Actual
81 99
         int actual =  cc.getExpiredMonth();
@@ -86,7 +104,6 @@ public class CreditCardTest {
86 104
     @Test
87 105
     public void setMonthTest(){
88 106
         //When
89
-        CreditCard cc = new CreditCard();
90 107
         cc.setExpiredMonth(12);
91 108
 
92 109
         //Expect
@@ -99,13 +116,9 @@ public class CreditCardTest {
99 116
     }
100 117
 
101 118
     @Test
102
-    public void setYearTest(){
103
-        //When
104
-        CreditCard cc = new CreditCard();
105
-        cc.setExpiredYear(1989);
106
-
119
+    public void getYearTest(){
107 120
         //Expect
108
-        int expected = 1989;
121
+        int expected = 2011;
109 122
 
110 123
         //Actual
111 124
         int actual =  cc.getExpiredYear();
@@ -114,9 +127,8 @@ public class CreditCardTest {
114 127
     }
115 128
 
116 129
     @Test
117
-    public void getYearTest(){
130
+    public void setYearTest(){
118 131
         //When
119
-        CreditCard cc = new CreditCard();
120 132
         cc.setExpiredYear(1989);
121 133
 
122 134
         //Expect
@@ -130,11 +142,8 @@ public class CreditCardTest {
130 142
 
131 143
     @Test
132 144
     public void getAndShortDescriptionTest(){
133
-        //When
134
-        CreditCard cc = new CreditCard(12, 4111131,"Nicholas Maidanos", 4, 11);
135
-
136 145
         //Expect
137
-        String expected = "CC Nicholas Maidanos 1131 4/11";
146
+        String expected = "CC Nicholas Maidanos 1131 4/2011\n";
138 147
 
139 148
         //Actual
140 149
         String actual = cc.getShortDescription();
@@ -142,19 +151,6 @@ public class CreditCardTest {
142 151
     }
143 152
 
144 153
     @Test
145
-    public void testGetLastFourDigits(){
146
-        //Given
147
-        CreditCard cc = new CreditCard();
148
-        cc.setNumber(888999);
149
-        Integer expected = 8999;
150
-
151
-        //When
152
-        Integer actual = cc.lastFourDigits();
153
-        assertEquals(expected, actual);
154
-
155
-    }
156
-
157
-    @Test
158 154
     public void compareToTest() {
159 155
         //Given
160 156
         Payment cc = new CreditCard(12, 4111131,"Nicholas Maidanos", 4, 11);

+ 27
- 36
src/test/java/com/zipcoder/payment/PaypalTest.java Vedi File

@@ -2,32 +2,35 @@ package com.zipcoder.payment;
2 2
 
3 3
 
4 4
 import static org.junit.Assert.*;
5
+import org.junit.Before;
5 6
 import org.junit.Test;
6 7
 
7 8
 public class PaypalTest {
8 9
 
10
+    Paypal pp;
11
+
12
+    @Before
13
+    public void setup(){
14
+        this.pp = new Paypal(50, "Nicholas Maidanos", "nmaidanos@gmail.com");
15
+    }
16
+
9 17
     @Test
10
-    void setId() {
18
+    public void setId() {
11 19
         //When
12
-        Paypal pp = new Paypal();
13
-        pp.setId(75);
20
+        pp.setId(48);
14 21
 
15 22
         //Expect
16
-        long expect = 75;
23
+        long expect = 48;
17 24
 
18 25
         //Actual
19
-        long actual = pp.getId();
26
+        long actual = this.pp.getId();
20 27
         assertEquals(expect, actual);
21 28
     }
22 29
 
23 30
     @Test
24
-    void getId() {
25
-        //When
26
-        Paypal pp = new Paypal();
27
-        pp.setId(75);
28
-
31
+    public void getId() {
29 32
         //Expect
30
-        long expect = 75;
33
+        long expect = 50;
31 34
 
32 35
         //Actual
33 36
         long actual = pp.getId();
@@ -35,13 +38,12 @@ public class PaypalTest {
35 38
     }
36 39
 
37 40
     @Test
38
-    void setPayerName() {
41
+    public void setPayerName() {
39 42
         //When
40
-        Paypal pp =  new Paypal();
41
-        pp.setPayerName("Nicholas Maidanos");
43
+        pp.setPayerName("Dan Smith");
42 44
 
43 45
         //Expect
44
-        String expect = "Nicholas Maidanos";
46
+        String expect = "Dan Smith";
45 47
 
46 48
         //Actual
47 49
         String actual = pp.getPayerName();
@@ -49,11 +51,7 @@ public class PaypalTest {
49 51
     }
50 52
 
51 53
     @Test
52
-    void getPayerName() {
53
-        //When
54
-        Paypal pp =  new Paypal();
55
-        pp.setPayerName("Nicholas Maidanos");
56
-
54
+    public void getPayerName() {
57 55
         //Expect
58 56
         String expect = "Nicholas Maidanos";
59 57
 
@@ -63,13 +61,12 @@ public class PaypalTest {
63 61
     }
64 62
 
65 63
     @Test
66
-    void setEmail() {
64
+    public void setEmail() {
67 65
         //When
68
-        Paypal pp = new Paypal();
69
-        pp.setEmail("example@email.com");
66
+        pp.setEmail("desk@email.com");
70 67
 
71 68
         //Expect
72
-        String expect = "example@email.com";
69
+        String expect = "desk@email.com";
73 70
 
74 71
         //Actual
75 72
         String actual = pp.getEmail();
@@ -77,13 +74,9 @@ public class PaypalTest {
77 74
     }
78 75
 
79 76
     @Test
80
-    void getEmail() {
81
-        //When
82
-        Paypal pp = new Paypal();
83
-        pp.setEmail("example@email.com");
84
-
77
+    public void getEmail() {
85 78
         //Expect
86
-        String expect = "example@email.com";
79
+        String expect = "nmaidanos@gmail.com";
87 80
 
88 81
         //Actual
89 82
         String actual = pp.getEmail();
@@ -91,12 +84,9 @@ public class PaypalTest {
91 84
     }
92 85
 
93 86
     @Test
94
-    void getShortDescription() {
95
-        //When
96
-        Paypal pp = new Paypal(78, "Nicholas Maidanos", "nmaidanos@gmail.com");
97
-
87
+    public void getShortDescription() {
98 88
         //Expect
99
-        String expect = "PayPal Nicholas Maidanos nmaidanos@gmail.com";
89
+        String expect = "Paypal Nicholas Maidanos nmaidanos@gmail.com\n";
100 90
 
101 91
         //Actual
102 92
         String actual = pp.getShortDescription();
@@ -105,6 +95,7 @@ public class PaypalTest {
105 95
     }
106 96
 
107 97
     @Test
108
-    void compareTo() {
98
+    public void compareTo() {
99
+
109 100
     }
110 101
 }