22
33import static org .junit .Assert .assertEquals ;
44
5- import java .io .UnsupportedEncodingException ;
65import java .text .ParseException ;
76import java .text .SimpleDateFormat ;
87import java .time .LocalDateTime ;
@@ -20,78 +19,111 @@ public class StringConversionTest {
2019
2120 @ Test
2221 public void whenConvertedToInt_thenCorrect () {
23- assertEquals (Integer .parseInt ("1" ), 1 );
22+ String beforeConvStr = "1" ;
23+ int afterConvInt = 1 ;
24+
25+ assertEquals (Integer .parseInt (beforeConvStr ), afterConvInt );
2426 }
2527
2628 @ Test
2729 public void whenConvertedToInteger_thenCorrect () {
28- assertEquals (Integer .valueOf ("12" ).equals (12 ), true );
30+ String beforeConvStr = "12" ;
31+ Integer afterConvInteger = 12 ;
32+
33+ assertEquals (Integer .valueOf (beforeConvStr ).equals (afterConvInteger ), true );
2934 }
3035
3136 @ Test
3237 public void whenConvertedTolong_thenCorrect () {
33- assertEquals (Long .parseLong ("12345" ), 12345 );
38+ String beforeConvStr = "12345" ;
39+ long afterConvLongPrimitive = 12345 ;
40+
41+ assertEquals (Long .parseLong (beforeConvStr ), afterConvLongPrimitive );
3442 }
3543
3644 @ Test
3745 public void whenConvertedToLong_thenCorrect () {
38- assertEquals (Long .valueOf ("14567" ).equals (14567L ), true );
46+ String beforeConvStr = "14567" ;
47+ Long afterConvLong = 14567l ;
48+
49+ assertEquals (Long .valueOf (beforeConvStr ).equals (afterConvLong ), true );
3950 }
4051
4152 @ Test
4253 public void whenConvertedTodouble_thenCorrect () {
43- assertEquals (Double .parseDouble ("1.4" ), 1.4 , 0.0 );
54+ String beforeConvStr = "1.4" ;
55+ double afterConvDoublePrimitive = 1.4 ;
56+
57+ assertEquals (Double .parseDouble (beforeConvStr ), afterConvDoublePrimitive , 0.0 );
4458 }
4559
4660 @ Test
4761 public void whenConvertedToDouble_thenCorrect () {
48- assertEquals (Double .valueOf ("145.67" ).equals (145.67d ), true );
62+ String beforeConvStr = "145.67" ;
63+ double afterConvDouble = 145.67d ;
64+
65+ assertEquals (Double .valueOf (beforeConvStr ).equals (afterConvDouble ), true );
4966 }
5067
5168 @ Test
52- public void whenConvertedToByteArray_thenCorrect () throws UnsupportedEncodingException {
53- byte [] byteArray1 = new byte [] { 'a' , 'b' , 'c' } ;
54- String string = new String ( byteArray1 , "UTF-8" ) ;
69+ public void whenConvertedToByteArr_thenCorrect () {
70+ String beforeConvStr = "abc" ;
71+ byte [] afterConvByteArr = new byte [] { 'a' , 'b' , 'c' } ;
5572
56- assertEquals (Arrays .equals (string .getBytes (), byteArray1 ), true );
73+ assertEquals (Arrays .equals (beforeConvStr .getBytes (), afterConvByteArr ), true );
5774 }
5875
5976 @ Test
6077 public void whenConvertedToboolean_thenCorrect () {
61- assertEquals (Boolean .parseBoolean ("true" ), true );
78+ String beforeConvStr = "true" ;
79+ boolean afterConvBooleanPrimitive = true ;
80+
81+ assertEquals (Boolean .parseBoolean (beforeConvStr ), afterConvBooleanPrimitive );
6282 }
6383
6484 @ Test
6585 public void whenConvertedToBoolean_thenCorrect () {
66- assertEquals (Boolean .valueOf ("true" ), true );
86+ String beforeConvStr = "true" ;
87+ Boolean afterConvBoolean = true ;
88+
89+ assertEquals (Boolean .valueOf (beforeConvStr ), afterConvBoolean );
6790 }
6891
6992 @ Test
70- public void whenConvertedToCharArray_thenCorrect () {
71- String str = "hello" ;
72- char [] charArray = { 'h' , 'e' , 'l' , 'l' , 'o' };
93+ public void whenConvertedToCharArr_thenCorrect () {
94+ String beforeConvStr = "hello" ;
95+ char [] afterConvCharArr = { 'h' , 'e' , 'l' , 'l' , 'o' };
7396
74- assertEquals (Arrays .equals (charArray , str .toCharArray ()), true );
97+ assertEquals (Arrays .equals (beforeConvStr .toCharArray (), afterConvCharArr ), true );
7598 }
7699
77100 @ Test
78101 public void whenConvertedToDate_thenCorrect () throws ParseException {
79- String str = "15/10/2013" ;
102+ String beforeConvStr = "15/10/2013" ;
103+ int afterConvCalendarDay = 15 ;
104+ int afterConvCalendarMonth = 9 ;
105+ int afterConvCalendarYear = 2013 ;
80106 SimpleDateFormat formatter = new SimpleDateFormat ("dd/M/yyyy" );
81- Date date1 = formatter .parse (str );
82- Calendar calendar = new GregorianCalendar (2013 , 9 , 15 );
83- Date date2 = calendar .getTime ( );
107+ Date afterConvDate = formatter .parse (beforeConvStr );
108+ Calendar calendar = new GregorianCalendar ();
109+ calendar .setTime ( afterConvDate );
84110
85- assertEquals (date1 .compareTo (date2 ), 0 );
111+ assertEquals (calendar .get (Calendar .DAY_OF_MONTH ), afterConvCalendarDay );
112+ assertEquals (calendar .get (Calendar .MONTH ), afterConvCalendarMonth );
113+ assertEquals (calendar .get (Calendar .YEAR ), afterConvCalendarYear );
86114 }
87115
88116 @ Test
89- public void whenConvertedToLocalDateTime_thenCorrect () throws ParseException {
117+ public void whenConvertedToLocalDateTime_thenCorrect () {
90118 String str = "2007-12-03T10:15:30" ;
91- LocalDateTime localDateTime = new UseLocalDateTime ().getLocalDateTimeUsingParseMethod (str );
92-
93- assertEquals (localDateTime .getDayOfMonth (), 3 );
94- assertEquals (localDateTime .getMonth (), Month .DECEMBER );
95- assertEquals (localDateTime .getYear (), 2007 );
119+ int afterConvCalendarDay = 03 ;
120+ Month afterConvCalendarMonth = Month .DECEMBER ;
121+ int afterConvCalendarYear = 2007 ;
122+ LocalDateTime afterConvDate
123+ = new UseLocalDateTime ().getLocalDateTimeUsingParseMethod (str );
124+
125+ assertEquals (afterConvDate .getDayOfMonth (), afterConvCalendarDay );
126+ assertEquals (afterConvDate .getMonth (), afterConvCalendarMonth );
127+ assertEquals (afterConvDate .getYear (), afterConvCalendarYear );
96128 }
97129}
0 commit comments