Skip to content

Commit f499f86

Browse files
fpistrittoKevinGilmore
authored andcommitted
BAEL-1272: Java String Pool (eugenp#2952)
* Add StringPool unit tests * Refactor tests to use AssertJ instead of JUnit asserts
1 parent 7b4d644 commit f499f86

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.stringpool;
2+
3+
import org.junit.Test;
4+
import static org.assertj.core.api.Assertions.*;
5+
6+
public class StringPoolUnitTest {
7+
8+
@Test
9+
public void whenCreatingConstantStrings_thenTheirAddressesAreEqual() {
10+
String constantString1 = "Baeldung";
11+
String constantString2 = "Baeldung";
12+
13+
assertThat(constantString1).isSameAs(constantString2);
14+
}
15+
16+
@Test
17+
public void whenCreatingStringsWithTheNewOperator_thenTheirAddressesAreDifferent() {
18+
String newString1 = new String("Baeldung");
19+
String newString2 = new String("Baeldung");
20+
21+
assertThat(newString1).isNotSameAs(newString2);
22+
}
23+
24+
@Test
25+
public void whenComparingConstantAndNewStrings_thenTheirAddressesAreDifferent() {
26+
String constantString = "Baeldung";
27+
String newString = new String("Baeldung");
28+
29+
assertThat(constantString).isNotSameAs(newString);
30+
}
31+
32+
@Test
33+
public void whenInterningAStringWithIdenticalValueToAnother_thenTheirAddressesAreEqual() {
34+
String constantString = "interned Baeldung";
35+
String newString = new String("interned Baeldung");
36+
37+
assertThat(constantString).isNotSameAs(newString);
38+
39+
String internedString = newString.intern();
40+
41+
assertThat(constantString).isSameAs(internedString);
42+
}
43+
}

0 commit comments

Comments
 (0)