Skip to content

Commit 2ffc247

Browse files
BAEL-29 Review Comment - Remove Inlining Variables
1 parent 2d34971 commit 2ffc247

1 file changed

Lines changed: 68 additions & 37 deletions

File tree

‎core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java‎

Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static org.junit.Assert.assertEquals;
44

5-
import java.io.UnsupportedEncodingException;
65
import java.text.ParseException;
76
import java.text.SimpleDateFormat;
87
import java.time.LocalDateTime;
@@ -19,79 +18,111 @@
1918
public class StringConversionTest {
2019

2120
@Test
22-
public void whenConvertedToInt_thenCorrect() {
23-
assertEquals(Integer.parseInt("1"), 1);
21+
public void whenConvedToInt_thenCorrect() {
22+
String beforeConvStr = "1";
23+
int afterConvInt = 1;
24+
25+
assertEquals(Integer.parseInt(beforeConvStr), afterConvInt);
2426
}
2527

2628
@Test
27-
public void whenConvertedToInteger_thenCorrect() {
28-
assertEquals(Integer.valueOf("12").equals(12), true);
29+
public void whenConvedToInteger_thenCorrect() {
30+
String beforeConvStr = "12";
31+
Integer afterConvInteger = 12;
32+
33+
assertEquals(Integer.valueOf(beforeConvStr).equals(afterConvInteger), true);
2934
}
3035

3136
@Test
32-
public void whenConvertedTolong_thenCorrect() {
33-
assertEquals(Long.parseLong("12345"), 12345);
37+
public void whenConvedTolong_thenCorrect() {
38+
String beforeConvStr = "12345";
39+
long afterConvLongPrimitive = 12345;
40+
41+
assertEquals(Long.parseLong(beforeConvStr), afterConvLongPrimitive);
3442
}
3543

3644
@Test
37-
public void whenConvertedToLong_thenCorrect() {
38-
assertEquals(Long.valueOf("14567").equals(14567L), true);
45+
public void whenConvedToLong_thenCorrect() {
46+
String beforeConvStr = "14567";
47+
Long afterConvLong = 14567l;
48+
49+
assertEquals(Long.valueOf(beforeConvStr).equals(afterConvLong), true);
3950
}
4051

4152
@Test
42-
public void whenConvertedTodouble_thenCorrect() {
43-
assertEquals(Double.parseDouble("1.4"), 1.4, 0.0);
53+
public void whenConvedTodouble_thenCorrect() {
54+
String beforeConvStr = "1.4";
55+
double afterConvDoublePrimitive = 1.4;
56+
57+
assertEquals(Double.parseDouble(beforeConvStr), afterConvDoublePrimitive, 0.0);
4458
}
4559

4660
@Test
47-
public void whenConvertedToDouble_thenCorrect() {
48-
assertEquals(Double.valueOf("145.67").equals(145.67d), true);
61+
public void whenConvedToDouble_thenCorrect() {
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 whenConvedToByteArr_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
60-
public void whenConvertedToboolean_thenCorrect() {
61-
assertEquals(Boolean.parseBoolean("true"), true);
77+
public void whenConvedToboolean_thenCorrect() {
78+
String beforeConvStr = "true";
79+
boolean afterConvBooleanPrimitive = true;
80+
81+
assertEquals(Boolean.parseBoolean(beforeConvStr), afterConvBooleanPrimitive);
6282
}
6383

6484
@Test
65-
public void whenConvertedToBoolean_thenCorrect() {
66-
assertEquals(Boolean.valueOf("true"), true);
85+
public void whenConvedToBoolean_thenCorrect() {
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 whenConvedToCharArr_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
78-
public void whenConvertedToDate_thenCorrect() throws ParseException {
79-
String str = "15/10/2013";
101+
public void whenConvedToDate_thenCorrect() throws ParseException {
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 whenConvedToLocalDateTime_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 = new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str);
123+
124+
assertEquals(afterConvDate.getDayOfMonth(), afterConvCalendarDay);
125+
assertEquals(afterConvDate.getMonth(), afterConvCalendarMonth);
126+
assertEquals(afterConvDate.getYear(), afterConvCalendarYear);
96127
}
97128
}

0 commit comments

Comments
 (0)