1
1
package com .codedifferently .lesson16 .onepiece ;
2
2
3
3
import static org .assertj .core .api .Assertions .assertThat ;
4
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
5
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
6
+
7
+ import com .codedifferently .lesson16 .onepiece .Pirate .HakiType ;
8
+ import java .io .ByteArrayOutputStream ;
9
+ import java .io .PrintStream ;
4
10
import org .junit .jupiter .api .BeforeEach ;
5
11
import org .junit .jupiter .api .Test ;
6
12
@@ -10,73 +16,112 @@ public class PirateTest {
10
16
11
17
@ BeforeEach
12
18
void setUp () {
13
- pirate = new Pirate (2 , "Zoro " , "Swordsman " , 900000 );
19
+ pirate = new Pirate ("Luffy" , "StrawHatPirates " , 3000000000L , "Captain " , true );
14
20
}
15
21
16
22
@ Test
17
- void testGetId () {
18
- int actual = pirate .getId ();
23
+ void testGetName () {
24
+ String actual = pirate .getName ();
19
25
20
- assertThat (actual ).isEqualTo (2 );
26
+ assertThat (actual ).isEqualTo ("Luffy" );
21
27
}
22
28
23
29
@ Test
24
- void testSetId () {
25
- pirate .setId ( 3 );
26
- int actual = pirate .getId ();
30
+ void testSetName () {
31
+ pirate .setName ( "BlackBeard" );
32
+ String actual = pirate .getName ();
27
33
28
- assertThat (actual ).isEqualTo (3 );
34
+ assertThat (actual ).isEqualTo ("BlackBeard" );
29
35
}
30
36
31
37
@ Test
32
- void testGetName () {
33
- String actual = pirate .getName ();
38
+ void testGetCrew () {
39
+ String actual = pirate .getCrew ();
34
40
35
- assertThat (actual ).isEqualTo ("Zoro " );
41
+ assertThat (actual ).isEqualTo ("StrawHatPirates " );
36
42
}
37
43
38
44
@ Test
39
- void testSetName () {
40
- pirate .setName ( "Sanji " );
41
- String actual = pirate .getName ();
45
+ void testSetCrew () {
46
+ pirate .setCrew ( "BlackBeardPirates " );
47
+ String actual = pirate .getCrew ();
42
48
43
- assertThat (actual ).isEqualTo ("Sanji " );
49
+ assertThat (actual ).isEqualTo ("BlackBeardPirates " );
44
50
}
45
51
46
52
@ Test
47
- void testGetDepartment () {
48
- String actual = pirate .getDepartment ();
53
+ void testGetBounty () {
54
+ Long actual = pirate .getBounty ();
49
55
50
- assertThat (actual ).isEqualTo ("Swordsman" );
56
+ assertThat (actual ).isEqualTo (3000000000L );
51
57
}
52
58
53
59
@ Test
54
- void testSetDepartment () {
55
- pirate .setDepartment ( "Cook" );
56
- String actual = pirate .getDepartment ();
60
+ void testSetBounty () {
61
+ pirate .setBounty ( 3996000000L );
62
+ Long actual = pirate .getBounty ();
57
63
58
- assertThat (actual ).isEqualTo ("Cook" );
64
+ assertThat (actual ).isEqualTo (3996000000L );
59
65
}
60
66
61
67
@ Test
62
- void testGetSalary () {
63
- double actual = pirate .getSalary ();
68
+ void testGetRole () {
69
+ String actual = pirate .getRole ();
64
70
65
- assertThat (actual ).isEqualTo (900000 );
71
+ assertThat (actual ).isEqualTo ("Captain" );
66
72
}
67
73
68
74
@ Test
69
- void testSetSalary () {
70
- pirate .setSalary ( 850000 );
71
- double actual = pirate .getSalary ();
75
+ void testSetRole () {
76
+ pirate .setRole ( "Captain" );
77
+ String actual = pirate .getRole ();
72
78
73
- assertThat (actual ).isEqualTo (850000 );
79
+ assertThat (actual ).isEqualTo ("Captain" );
74
80
}
75
81
76
82
@ Test
77
- void testGetDetails () {
78
- String actual = pirate .getDetails ();
83
+ void testgetHasDream () {
84
+ try {
85
+ pirate .getHasDream ();
86
+ } catch (HasNoDreamException e ) {
87
+ System .out .println (e .getMessage ());
88
+ }
89
+ }
90
+
91
+ @ Test
92
+ void testRollPowers () {
93
+ pirate .rollPowers ();
94
+ assertNotNull (pirate .getPowers (), "Powers should not be null after rolling" );
95
+ boolean isValid = false ;
96
+ for (HakiType h : HakiType .values ()) {
97
+ if (pirate .getPowers () == h ) {
98
+ isValid = true ;
99
+ break ;
100
+ }
101
+ }
102
+ assertTrue (isValid , "Powers should be a valid HakiType" );
103
+ }
104
+
105
+ @ Test
106
+ void testRollPowersOutput () {
107
+
108
+ // Capture console output
109
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
110
+ System .setOut (new PrintStream (outputStream ));
111
+
112
+ pirate .rollPowers ();
113
+
114
+ String output = outputStream .toString ().trim ();
115
+ boolean matches = false ;
116
+ for (HakiType h : HakiType .values ()) {
117
+ if (output .equals ("Random Haki: " + h )) {
118
+ matches = true ;
119
+ break ;
120
+ }
121
+ }
122
+
123
+ assertTrue (matches , "Haki Options" );
79
124
80
- assertThat ( actual ). isEqualTo ( "2 Zoro Swordsman 900000.0" );
125
+ System . setOut ( System . out );
81
126
}
82
127
}
0 commit comments