Update CenteringTextUnitTest.java (#15011)

This commit aims to add new updates to the class "CenteringTextUnitTest" based on the review process recommendations.
This commit is contained in:
MohamedHelmyKassab 2023-10-18 05:03:31 +03:00 committed by GitHub
parent 3298d5fb96
commit faddb1d62c
1 changed files with 8 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package com.baeldung.centertext;
import liquibase.repackaged.org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
public class CenteringTextUnitTest {
@ -10,17 +11,16 @@ public class CenteringTextUnitTest {
@Test
public void givenTextAndTotalWidth_whenUsingStringFormat_thenTextIsCentered() {
String text = "Centered Text";
int totalWidth = 20;
int totalWidth = 15;
int padding = (totalWidth - text.length()) / 2;
String centeredText = String.format("%" + padding + "s%s%" + padding + "s", "", text, "");
String expectedCenteredText = " Centered Text ";
Assert.assertEquals("Centered Text", expectedCenteredText, centeredText);
Assert.assertEquals(" Centered Text ", centeredText);
}
@Test
public void givenTextAndTotalWidth_whenCenterUsingStringBuilder_thenTextIsCentered() {
String text = "Centered Text";
int width = 20;
int width = 15;
int padding = (width - text.length()) / 2;
StringBuilder centeredText = new StringBuilder();
for (int i = 0; i < padding; i++) {
@ -31,17 +31,15 @@ public class CenteringTextUnitTest {
centeredText.append(" ");
}
String centeredTextString = centeredText.toString();
String expectedCenteredText = " Centered Text ";
Assert.assertEquals("Centered Text", expectedCenteredText, centeredTextString);
Assert.assertEquals(" Centered Text ", centeredTextString);
}
@Test
public void givenTextAndTotalWidth_whenUsingStringUtilsCenterMethod_thenTextIsCentered() {
String text = "Centered Text";
int width = 20;
int width = 15;
String centeredText = StringUtils.center(text, width);
String expectedCenteredText = StringUtils.center("Centered Text", width);
assertEquals("Centered Text", expectedCenteredText, centeredText);
assertEquals(" Centered Text ", centeredText);
}
}