[BAEL-2963] - String initialization (#6962)

This commit is contained in:
cdjole 2019-05-14 22:57:08 +02:00 committed by maibin
parent ef8b8232a7
commit 0825c88d7a
1 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,58 @@
package com.baeldung.initialization;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import org.junit.Test;
public class StringInitializationUnitTest {
private String fieldString;
void printDeclaredOnlyString() {
String localVarString = null;
System.out.println(localVarString); // compilation error
System.out.println(fieldString);
}
@Test
public void givenDeclaredFeldStringAndNullString_thenCompareEquals() {
String localVarString = null;
assertEquals(fieldString, localVarString);
}
@Test
public void givenTwoStringsWithSameLiteral_thenCompareReferencesEquals() {
String literalOne = "Baeldung";
String literalTwo = "Baeldung";
assertTrue(literalOne == literalTwo);
}
@Test
public void givenTwoStringsUsingNew_thenCompareReferencesNotEquals() {
String newStringOne = new String("Baeldung");
String newStringTwo = new String("Baeldung");
assertFalse(newStringOne == newStringTwo);
}
@Test
public void givenEmptyLiteralStringsAndNewObject_thenCompareEquals() {
String emptyLiteral = "";
String emptyNewString = new String("");
assertEquals(emptyLiteral, emptyNewString);
}
@Test
public void givenEmptyStringObjects_thenCompareEquals() {
String emptyNewString = new String("");
String emptyNewStringTwo = new String();
assertEquals(emptyNewString, emptyNewStringTwo);
}
}