[BAEL-6297_boolean-to-str] convert boolean to java article (#13729)

* [BAEL-6297_boolean-to-str] convert boolean to java article

* [BAEL-6297_boolean-to-str] using Boolean.FALSE instead of false for the Boolean variable

* [BAEL-6297_boolean-to-str] rename test methods
This commit is contained in:
Kai Yuan 2023-03-31 01:01:25 +02:00 committed by GitHub
parent 11f740192c
commit c40bdd9f36
4 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,5 @@
## Core Java Booleans
This module contains articles about Java Booleans.
### Relevant Articles:

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-booleans</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-booleans</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,48 @@
package com.baeldung.booleans;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class BooleanToStringUnitTest {
String optionToString(String optionName, boolean optionValue) {
return String.format("The option '%s' is %s.", optionName, optionValue ? "Enabled" : "Disabled");
}
@Test
void givenPrimitiveBoolean_whenConvertingUsingTernaryOperator_thenSuccess() {
boolean primitiveBoolean = true;
assertEquals("true", primitiveBoolean ? "true" : "false");
primitiveBoolean = false;
assertEquals("false", primitiveBoolean ? "true" : "false");
assertEquals("The option 'IgnoreWarnings' is Enabled.", optionToString("IgnoreWarnings", true));
}
@Test
void givenBooleanObject_whenConvertingUsingBooleanToString_thenSuccess() {
Boolean myBoolean = Boolean.TRUE;
assertEquals("true", myBoolean.toString());
myBoolean = Boolean.FALSE;
assertEquals("false", myBoolean.toString());
Boolean nullBoolean = null;
assertThrows(NullPointerException.class, () -> nullBoolean.toString());
}
@Test
void givenBooleanObject_whenConvertingUsingStringValueOf_thenSuccess() {
Boolean myBoolean = Boolean.TRUE;
assertEquals("true", String.valueOf(myBoolean));
myBoolean = Boolean.FALSE;
assertEquals("false", String.valueOf(myBoolean));
Boolean nullBoolean = null;
assertEquals("null", String.valueOf(nullBoolean));
}
}

View File

@ -38,6 +38,7 @@
<module>core-java-arrays-convert</module> <module>core-java-arrays-convert</module>
<module>core-java-arrays-operations-basic</module> <module>core-java-arrays-operations-basic</module>
<module>core-java-arrays-operations-advanced</module> <module>core-java-arrays-operations-advanced</module>
<module>core-java-booleans</module>
<module>core-java-char</module> <module>core-java-char</module>
<module>core-java-collections</module> <module>core-java-collections</module>
<module>core-java-collections-2</module> <module>core-java-collections-2</module>