Bienvenida e Introducción
Paso a paso para testing básico en Java
Introducción a tests en software
Tipos y beneficios de los tests
Preparación del IDE, proyecto y librerías
Instalación de IntelliJ IDEA, creación del Proyecto con Maven y Tests Unitarios
Testing en Java con JUnit para Verificar Contraseñas
Creación de test unitario: lanzar una excepción para alertar sobre un error
Test unitario con JUnit
Organización de tests con JUnit
Test con Mockito para simular un dado
Test con Mockito: simular el uso de una pasarela de pago
Análisis de los tests y mejoras
Reto 1: crear la función isEmpty
TDD
TDD: Definición, Beneficios, Ciclos y Reglas
Ejemplos de TDD: calcular el año bisiesto
Ejemplos de TDD: cálculo de descuentos
Reto 2: Práctica de TDD
Tests en una aplicación
Organización de una aplicación
App de Películas: Test de Negocio
App de Películas: test de búsqueda de películas por su duración
Creación de la base de datos y tests de integración con bases de datos
Test de integración con base de datos: guardar películas y búsqueda de películas individuales
Reto 3: Nuevas opciones de búsqueda
Requerimientos y tests
Test a partir de requerimiento
Reto 4: Búsqueda por varios atributos
Conclusiones
Resumen y conclusiones
En una aplicación que estamos construyendo, nos hemos dado cuenta de que comprobamos muchas veces si un string está vacío o no. Por ello, vamos a implementar una función que realice esta tarea.
Crearemos una clase StringUtil y dentro pondremos la función isEmpty:
public class StringUtil {
public static boolean isEmpty(String str) {
...
}
}
Implementa esta función, y crea una clase StringUtilTest añadiendo tests que prueben varios escenarios:
Probar que un string cualquiera no es vacío
Probar que “” es un string vacío
Probar que null también es un string vacío
Extra: un string con espacios " " también lo queremos considerar como vacío (pista: puedes usar la función trim)
Comparte tu solución en el panel de discusiones.
Aportes 191
Preguntas 1
Les dejo mi aporte con JUnit5
Método:
public static boolean isEmpty(String string) {
return string == null || string.trim().length() == 0;
}
Tests:
class StringUtilsTest {
@Nested
@DisplayName("isEmpty method")
class IsEmpty {
@Test
@DisplayName("when string is null")
void trueWhenNull() {
assertTrue(StringUtils.isEmpty(null));
}
@Test
@DisplayName("when string just has spaces")
void trueWhenEmptySpace() {
assertTrue(StringUtils.isEmpty(" "));
}
@Test
@DisplayName("when string is empty")
void trueWhenEmpty() {
assertTrue(StringUtils.isEmpty(""));
}
@Test
@DisplayName("when has chars")
void falseWhenHasChars() {
assertFalse(StringUtils.isEmpty("hello"));
}
}
}
StringUtil.java
public static boolean isEmpty(final String str) {
return str == null || str.trim().isEmpty();
}
StringUtilTest.java
@Test
public void string_is_not_empty() {
assertFalse(StringUtil.isEmpty("hola"));
}
@Test
public void string_is_empty() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_null_is_empty() {
assertTrue(StringUtil.isEmpty(null));
}
// Class
public class StringUtil {
public static boolean isEmpty(String str) {
return str == null || str.trim().equals("");
}
}
// Test class
public class StringUtilTest {
@Test public void testNotEmptyString() {
assertFalse(StringUtil.isEmpty("abcde"));
}
@Test public void testEmptyString() {
assertTrue(StringUtil.isEmpty(""));
}
@Test public void testNullString() {
assertTrue(StringUtil.isEmpty(null));
}
@Test public void testSpacesString() {
assertTrue(StringUtil.isEmpty(" "));
}
}
public static boolean isEmpty(String str) {
return str == null || str.trim().length() <= 0; }
@Test
public void string_quotation_marks_is_empty() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_space_is_empty() {
assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void string_null_is_empty() {
assertTrue(StringUtil.isEmpty(null));
}
Esta es mi Solucion:
StringUtil.java
if( str==null || str.isBlank()){
return true;
}
return false;
StringUtilTest.java
public class StringUtilTest {
@Test
public void verifyStrNotEmpty() {
Assert.assertFalse(StringUtil.isEmpty("Oscar"));
}
@Test
public void verifyStrisEmpty() {
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void verifyStrisNull() {
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void verifyStrWithSpaces() {
Assert.assertTrue(StringUtil.isEmpty(" "));
}
}
public static boolean isEmpty(String str) {
return str == null || str.trim().length() <= 0;
}
@Test
public void string_is_not_empty() {
assertFalse(StringUtil.isEmpty(“hola”));
}
@Test
public void string_quotation_marks_is_empty() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_space_is_empty() {
assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void string_null_is_empty() {
assertTrue(StringUtil.isEmpty(null));
}
StringUtil.java
public static boolean isEmpty(String string) {
return string == null || string.equals("") || string.trim().length() == 0;
}
StringUtilTest.java
@Test
public void stringIsNotEmpty() {
boolean response = StringUtil.isEmpty("abcd");
Assert.assertFalse(response);
}
@Test
public void stringIsEmpty() {
boolean response = StringUtil.isEmpty("");
Assert.assertTrue(response);
}
@Test
public void stringIsNull() {
boolean response = StringUtil.isEmpty(null);
Assert.assertTrue(response);
}
@Test
public void stringWithSpaces() {
boolean response = StringUtil.isEmpty(" ");
Assert.assertTrue(response);
}
muy bien definidos los criterios de aceptación de la función, pero como sabriamos cuales son los criterios, porque puede que no se hayan contemplado algunas situaciones en alguna función que hayamos implementado.
public class StringUtil {
public static boolean isEmpty(String str){
return str == null || str.trim().equals("");
}
}
public class StringUtilTest {
StringUtil stringUtil;
@Before
public void setup(){
stringUtil = new StringUtil();
}
@Test
public void when_not_empty_string_it_will_return_false(){
//arrange
String anyNotEmptyString = "not_empty_string";
//act
boolean isEmptyString = stringUtil.isEmpty(anyNotEmptyString);
//assert
assertFalse(isEmptyString);
}
@Test
public void when_empty_string_it_will_return_true(){
//arrange
String emptyString = "";
//act
boolean isEmptyString = stringUtil.isEmpty(emptyString);
//assert
assertTrue(isEmptyString);
}
@Test
public void when_null_string_it_will_return_true(){
//arrange
String nullString = null;
//act
boolean isEmptyString = stringUtil.isEmpty(nullString);
//assert
assertTrue(isEmptyString);
}
@Test
public void when_string_with_space_it_will_return_true(){
//arrange
String spaceString = " ";
//act
boolean isEmptyString = stringUtil.isEmpty(spaceString);
//assert
assertTrue(isEmptyString);
}
}
Completado! =D
String Util
public static boolean isEmpty(String str){
if (str == null){ return true;}
return str.trim().equals("");
}
StringUtilTest
/* RETO: 26/02/2019 */
@Test
public void test_string_not_empty(){
Assert.assertFalse(StringUtil.isEmpty("any string"));
}
@Test
public void test_string_empty(){
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void test_string_empty_also_null(){
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void test_string_empty_with_spaces(){
Assert.assertTrue(StringUtil.isEmpty(" "));
}
public class StringUtil {
public static boolean isEmpty(String string){
return string == null || string.trim().equals("");
}
}
public class StringUtilTest {
@Test
public void is_invalid_when_string_is_empty(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void is_invalid_when_string_is_null() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void is_invalid_when_string_have_spaces() {
assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void is_string_valid() {
assertFalse(StringUtil.isEmpty("hello world"));
}
}
public class StringUtil
{
public static boolean isEmpty(String str)
{
if (str == null || str.trim().length() == 0)
{
return true;
}
return false;
}
}
import org.junit.Test;
import static org.junit.Assert.*;
public class StringUtilTest
{
@Test
public void when_is_only_double_quotes()
{
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void when_is_quotes_and_spaces()
{
assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void when_is_null()
{
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void when_is_string()
{
assertFalse(StringUtil.isEmpty("junit"));
}
}
En mi caso consideré también que “null” es vacío, esto porque en algunos lugares he visto que para realizar un cast a String suelen utilizar + “”.
public static boolean isEmpty(String str) {
return str == null || str.trim().replace("null", "").isEmpty();
}
Aquí los test
@Test
public void string_is_null() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_is_empty() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_is_empty_with_spaces() {
assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void string_is_not_empty() {
assertFalse(StringUtil.isEmpty(" hola"));
}
@Test
public void string_is_differente_from_null_text() {
assertTrue(StringUtil.isEmpty("null"));
}
En clase StringUtil
public static boolean isEmpty(String text) {
if (text == null || text.equals("") || text.trim().isEmpty()) {
return true;
}else {
return false;
}
StringUtil
@Test
public void isString_Con_Valor() {
Assert.assertFalse(StringUtil.isEmpty(“Prueba”));
}
@Test
public void isString_null() {
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void isString_Vacio() {
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void isString_Con_Espacio() {
Assert.assertTrue(StringUtil.isEmpty(" "));
}
public static Boolean isEmpty(String str) {
return (str != null && str.trim().length() > 0) ? true : false;
}
Reto1
public class Reto1 {
public static boolean isEmpty(String str) {
//si el tamaño del String es cero (cadena vacía), return true, sino, return false
return str == null || str.trim().length() == 0;
}
}
Reto1Test
public class Reto1Test {
@Test
public void test_any_string() {
assertFalse(Reto1.isEmpty("Carlos"));
}
@Test
public void test_empty_string() {
assertTrue(Reto1.isEmpty(""));
}
@Test
public void test_null_string() {
assertTrue(Reto1.isEmpty(null));
}
@Test
public void test_space_string() {
assertTrue(Reto1.isEmpty(" "));
}
}
Clase StringUtil
public class StringUtil {
public static boolean isEmpty(String str) {
if(str != null && !str.isEmpty() && !str.trim().isEmpty()) {
return true;
}else{
return false;
}
}
}
Clase StringUtilTest
public class StringUtilTest {
@Test
public void string_no_es_vacio() {
boolean result= StringUtil.isEmpty(" Hola Mundo ");
assertTrue(result);
}
@Test
public void string_es_vacio() {
boolean result= StringUtil.isEmpty("");
assertFalse(result);
}
@Test
public void string_es_null() {
boolean result= StringUtil.isEmpty(null);
assertFalse(result);
}
@Test
public void string_con_espacios() {
boolean result= StringUtil.isEmpty(" ");
assertFalse(result);
}
}
// class
public class StringUtil {
public static boolean isEmpty(String url){
return url == null || url.trim().isEmpty();
}
// test
public class StringUtilTest {
@Test
public void stringEmpty(){
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void stringNull(){
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void stringSpacesEmpty(){
Assert.assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void stringNotEmpty(){
Assert.assertFalse(StringUtil.isEmpty("osdijf"));
}
}
public static boolean isEmpty(String string){
boolean resultado = false;
if (string == null || string.trim().length() == 0){
resultado = true;
}
return resultado;
}
public class StringUtilTest {
@Test
public void isEmpty_case_null(){
boolean result = StringUtil.isEmpty(null);
assertTrue(result);
}
@Test
public void isEmpty_case_empty1(){
boolean result = StringUtil.isEmpty("");
assertTrue(result);
}
@Test
public void isEmpty_case_empty2(){
boolean result = StringUtil.isEmpty(" ");
assertTrue(result);
}
@Test
public void isEmpty_case_string(){
boolean result = StringUtil.isEmpty(" perro");
assertFalse(result);
}
}
RETO #1:
package me.javatest.util;
public class StringUtil {
public static boolean isEmpty(String str){
if (str == null || str.trim().equals("")){
return true;
}
return false;
}
}
package me.javatest.util;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class StringUtilTest {
@Test
public void string_is_not_empty(){
assertFalse(StringUtil.isEmpty("Javier Dagobeth"));
}
@Test
public void string_is_empty(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_is_null(){
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_is_one_white_space(){
assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void string_is_white_spaces(){
assertTrue(StringUtil.isEmpty(" "));
}
}
public class StringUtilTest {
@Nested
private String srt;
private StringUtil stringUtil;
@Before
public void setup(){
stringUtil = new StringUtil();
}
@Test
public void isEmptyNull() {
srt = null;
boolean resul = stringUtil.isEmpty(srt);
Assert.assertTrue(resul);
}
@Test
public void isNotEmpty() {
srt = "Hello Word";
boolean resul = stringUtil.isEmpty(srt);
Assert.assertFalse(resul);
}
@Test
public void isEmpty() {
srt = "";
boolean resul = stringUtil.isEmpty(srt);
Assert.assertTrue(resul);
}
@Test
public void isEmptyTrim() {
srt = " Hello Word ";
boolean resul = stringUtil.isEmpty(srt);
Assert.assertTrue(resul);
}
}
public static boolean isEmpty(String str){
return str == null || str.length() == 0 || str.trim() == "";
}
Metodo:
public static boolean isEmpty(String str) {
return str == null || str.trim().isEmpty();
}
tests:
@Test
public void when_str_isEmpty() {
assertTrue(StringEmptyUtil.isEmpty(""));
}
@Test
public void when_str_haveSpaces() {
assertTrue(StringEmptyUtil.isEmpty(" "));
}
@Test
public void when_str_isNull() {
assertTrue(StringEmptyUtil.isEmpty(null));
}
@Test
public void when_str_isString() {
assertFalse(StringEmptyUtil.isEmpty("word"));
}
package com.javatest.util;
public class StringUtil {
public static boolean isEmpty(String str){
return str == null || str.matches("^\\s+$|^$");
}
}
///////////////////////////////////////////////
package com.javatest.util;
import org.junit.Test;
import static org.junit.Assert.*;
public class StringUtilTest {
@Test
public void string_is_not_empty() {
assertFalse(StringUtil.isEmpty("Tobias"));
}
@Test
public void string_is_empty_with_space() {
assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void string_is_null() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_is_empty_without_spaces() {
assertTrue(StringUtil.isEmpty(""));
}
}
Implementación del método
public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
Test:
@Test
public void isEmpty_string_empty(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void isEmpty_string_only_space(){
assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void isEmpty_string_null(){
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void isEmpty_string_not_empty(){
assertFalse(StringUtil.isEmpty("shba"));
}
Practica 💻
Método en StringUtil.java
public static boolean isEmpty(String str){
return (str == null || str.trim().length() == 0);
}
StringUtilTest.java
@Test
public void string_is_not_empty() {
assertFalse(StringUtil.isEmpty("not empty"));
}
@Test
public void string_is_empty() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void null_is_empty() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void space_is_empty() {
assertTrue(StringUtil.isEmpty(" "));
}
public class StringUtilReto1 {
public static boolean isEmpty(String cadena) {
if (cadena == null) {
return true;
}
if (cadena.trim().equals("")) {
return true;
}
return false;
}
}
import org.junit.Test;
import static org.junit.Assert.*;
public class StringUtilReto1Test {
@Test
public void cadenaCualquieraNoEsVacio() {
assertFalse(StringUtilReto1.isEmpty("abddesf"));
}
@Test
public void cadenaComillasDoblesEsVacio() {
assertTrue(StringUtilReto1.isEmpty(""));
}
@Test
public void cadenaComillasDoblesConEspacioEsVacio() {
assertTrue(StringUtilReto1.isEmpty(" "));
}
@Test
public void cadenaNulaEsVacio() {
assertTrue(StringUtilReto1.isEmpty(null));
}
}
public static boolean isEmpty(String str) {
if (str == null) {
return true;
}
return str.trim().isEmpty();
}
//Comprobar valores vacíos o nulos
@Test
public void String_is_Empty() {
assertTrue(StringU.isEmpty(""));
}
@Test
public void String_isnt_Empty() {
assertFalse(StringU.isEmpty(" t "));
}
@Test
public void String_is_Null() {
assertTrue(StringU.isEmpty(null));
}
#isEmpty Method
#Tests
Clase StringUtil:
package com.platzi.javatest.util;
public class StringUtil {
public static Boolean isEmpty(String str){
boolean empty = false;
if (str == null){
empty = true;
}else if (str.isEmpty()){
empty = true;
}else if (str.trim().isEmpty()){
empty = true;
}
return empty;
}
}
Clase StringUtilTest:
package com.platzi.javatest.util;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
public class StringUtilTest {
@Test
public void string_is_not_empty(){
assertFalse(StringUtil.isEmpty("Algo"));
}
@Test
public void quotation_marks_empty(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void null_is_empty(){
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void white_space_is_empty(){
assertTrue(StringUtil.isEmpty(" "));
}
}
Aqui mi aporte:
Metodo isEmpty
public static boolean isEmpty(String str){
if(str==null||str.trim().isEmpty()){
return true;
}
return false;
}
Tests
@Test
public void stringIsNotEmpty() {
assertFalse(StringUtil.isEmpty("a"));
}
@Test
public void stringIsNull() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void stringWithOnlySpaces() {
assertTrue(StringUtil.isEmpty(" "));
}
Excelente reto para empezar en el mundo de pruebas unitarias. Consideren crear un nuevo curso con Junit 5. Saludos
package com.test.util;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class StringUtilTest {
// @Test
// public void testIsEmpty() {
// }
@BeforeClass
public static void setupOnce(){
System.out.println("setup once before");
}
@AfterClass
public static void endsetupOnce(){
System.out.println("end setup once before");
}
@Before
public void setup(){
System.out.println("doing before");
}
@After
public void afterTest(){
System.out.println("doing after");
}
@Test
public void empty_string() {
System.out.println("empty string");
boolean res = StringUtil.isEmpty("");
assertTrue("the string should be empty", res);
}
@Test
public void null_string() {
System.out.println("empty string");
boolean res = StringUtil.isEmpty(null);
assertTrue("the string should be empty", res);
}
@Test
public void not_empty_string() {
System.out.println("not empty string");
boolean res = StringUtil.isEmpty("asdf");
assertFalse("the string should be not empty", res);
}
}
Mi aporte.
package com.platzi.util;
public class StringUtilTest {
public static boolean isEmpty(String str){
return str==null || str.trim().equals("");
}
}
package com.platzi.util;
import org.junit.Assert;
import org.junit.Test;
public class StringUtilTestTest {
@Test
public void emptyStringValidation() {
Assert.assertTrue(StringUtilTest.isEmpty(""));
}
@Test
public void nullStringValidation() {
Assert.assertTrue(StringUtilTest.isEmpty(null));
}
@Test
public void filledStringValidation() {
Assert.assertFalse(StringUtilTest.isEmpty("Hola"));
}
@Test
public void spaceStringValidation() {
Assert.assertTrue(StringUtilTest.isEmpty(" "));
}
}
StringUtil
public static boolean isEmpty(String str) {
if (str == null){
throw new IllegalArgumentException("null no es un string");
}
if (str.trim().equals("")){
return true;
}
return false;
}
StringUtilTest
@Test
public void string_is_not_void() {
boolean result = StringUtil.isEmpty("dsad");
assertFalse(result);
}
@Test
public void string_is_void() {
boolean result = StringUtil.isEmpty("");
assertTrue(result);
}
@Test(expected = IllegalArgumentException.class)
public void null_is_void() {
boolean result = StringUtil.isEmpty(null);
}
@Test
public void space_is_void() {
boolean result = StringUtil.isEmpty(" ");
assertTrue(result);
}
Clase StringUtil.
public class RStringUtil {
public boolean isEmpty(String str){
return str == null || str.equals("") || str.contains(" ");
}
}
test:
public class RStringUtilTest {
RStringUtil rsStringUtil = new RStringUtil();
@Test
public void string_is_not_empty() {
assertFalse(rsStringUtil.isEmpty("Hola"));
}
@Test
public void string_is_empty() {
assertTrue(rsStringUtil.isEmpty(""));
}
@Test
public void string_is_empty_with_spaces() {
assertTrue(rsStringUtil.isEmpty(" foo space "));
}
@Test
public void string_is_empty_with_null() {
assertTrue(rsStringUtil.isEmpty(null));
}
}
Clase StringUtil:
public class StringUtil {
public static boolean stringIsEmpty(String str){
if(str == null){
throw new NullPointerException();
}else if(str.trim().isEmpty()){
return true;
}else{
return false;
}
}
}
Test:
import org.junit.Test;
import static org.junit.Assert.*;
public class StringUtilTest {
private boolean expectedFalse = false;
private boolean expectedTrue = true;
@Test
public void stringIsEmpty_stringIsFull_ReturnFalse() {
boolean currentResult = StringUtil.stringIsEmpty("Hello");
assertEquals(expectedFalse,currentResult);
}
@Test
public void stringIsEmpty_stringIsEmpty_ReturnTrue(){
boolean currentResult = StringUtil.stringIsEmpty("");
assertEquals(expectedTrue,currentResult);
}
@Test(expected = NullPointerException.class)
public void stringIsEmpty_stringIsNull_ReturnTrue() {
String stringNull = null;
boolean currentResult = StringUtil.stringIsEmpty(stringNull);
assertEquals(expectedTrue,currentResult);
}
@Test
public void stringIsEmpty_stringOnlyHaveSpaces_ReturnTrue() {
boolean currentResult = StringUtil.stringIsEmpty(" ");
assertEquals(expectedTrue,currentResult);
}
}```
Hola, este es el reto. 🤘🔥
Mi pequeño aporte 😄
StringUtil
public class StringUtil {
public static boolean isEmpty(String str){
return str == null || str.trim().isEmpty();
}
}
StringUtilTest
public class StringUtilTest {
@Test
public void string_is_not_empty() {
assertFalse(StringUtil.isEmpty("Cesar 1e"));
}
@Test
public void string_is_empty_with_quote() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_is_empty_with_null() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_is_empty_with_spaces() {
assertTrue(StringUtil.isEmpty(" "));
}
}
Les comparto mi solución
public static boolean isEmpty(String str) {
return str == null || str.trim().isEmpty();
}
@Test
public void string_is_null() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_is_empty() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_with_space() {
assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void string_is_not_empty() {
assertFalse(StringUtil.isEmpty("Hello"));
}
public static boolean isEmpty(String str) {
return Objects.isNull(str) || str.trim().isEmpty();
}
@Test
public void string_is_not_empty() {
assertFalse(StringUtil.isEmpty("hola"));
}
@Test
public void string_is_empty() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void null_is_empty() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_with_only_spaces_is_empty() {
assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void string_with_some_spaces_not_is_empty() {
assertFalse(StringUtil.isEmpty(" hola "));
}
public class StringUtil {
public static boolean isEmpty(String str){
return str == null || str.trim().length() == 0;
}
}
public class StringUtilTest {
private String strEmpty;
@Before
public void setup(){
strEmpty = new String(" ");
}
@Test
public void string_no_empty(){
assertFalse(StringUtil.isEmpty("Hola"));
}
@Test
public void string_empty(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_null(){
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_with_space(){
assertTrue(StringUtil.isEmpty(strEmpty));
}
}
public static boolean isEmpty(String text){
return(text == null || text.trim().isEmpty());
}
@Test
public void true_when_string_is_null(){
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void true_when_string_is_empty(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void true_when_string_contains_spaces(){
assertTrue(StringUtil.isEmpty(" "));
}
public static boolean isEmpty(String str) {
return str == null || str.trim().length() <= 0;
}
@Test
public void stringNotIsEmpty() {
Assert.assertFalse(StringUtil.isEmpty("hola"));
}
@Test
public void quotesStringIsEmpty() {
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void nullStringIsEmpty() {
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void stringSpacesIsEmpty() {
Assert.assertTrue(StringUtil.isEmpty(" "));
}
public static boolean isEmpty(String str){
return str.trim().length() <= 0 || str == null;
}
/
@Test
public void true_when_is_empty(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void true_when_is_only_spaces(){
assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void true_when_is_null(){
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void false_when_is_full(){
assertFalse(StringUtil.isEmpty("jfsdlkjf"));
}
public static boolean isEmpty(String str) {
return str == null || str.isBlank();
}
// tests
@Test
public void true_when_empty_string() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void true_when_null() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void true_when_whitespace() {
assertTrue(StringUtil.isEmpty(" "));
}
> public static boolean isEmpty(String str) {
return str == null || str.trim().isEmpty();
}
>
@Test
public void string_is_not_empty() {
assertFalse(StringUtil.isEmpty("hola"));
}
@Test
public void string_is_empty() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_null_is_empty() {
assertTrue(StringUtil.isEmpty(null));
}
public static boolean isEmpty(String str) {
return ((str == null) || (str.trim().equals("")));
}
@Test
public void any_string_is_not_empty() {
Assert.assertFalse(StringUtil.isEmpty("abc"));
}
@Test
public void empty_string_is_empty() {
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void null_string_is_empty() {
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_with_blanks_is_empty() {
Assert.assertTrue(StringUtil.isEmpty(" "));
}
La función:
public static boolean isEmpty(String str){
if (str == null || str.trim().equals("")){
return true;
}
return false;
}
Los test:
@Test
public void is_empty_when_string_empty() {
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void is_empty_when_string_null() {
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void is_empty_when_string_blank() {
Assert.assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void not_empty_when_string_provided() {
Assert.assertFalse(StringUtil.isEmpty("Test"));
}
Unit tests:
@Test
public void sucessIsEmptyWithAnEmptyString(){
assertTrue(Utils.isEmpty(""));
}
@Test
public void sucessIsEmptyWithNull(){
assertTrue(Utils.isEmpty(null));
}
@Test
public void sucessIsEmptyWithWhiteSpaceString(){
assertTrue(Utils.isEmpty(" "));
}
Utils:
public static boolean isEmpty(String aString) {
return aString == null || aString.isBlank();
}
public static boolean isEmpty(String str){
return str==null||str.length()<=0||str==" ";
}
en la case Test
@Test
public void test_string_in_not_empty() {
assertFalse(StringUtil.isEmpty("jhon"));
}
@Test
public void empty_String() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void null_is_String_empty() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_whit_empty_spaces() {
assertTrue(StringUtil.isEmpty(" "));
}
StringUtil
public static boolean isEmpty(String string){
return string == null || string.trim().length() == 0;
}
StringUtilTest
@Test
public void string_is_not_empty(){
Assert.assertFalse(StringUtil.isEmpty("Hola"));
}
@Test
public void string_is_empty(){
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_is_null_and_is_empty(){
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_whit_space_is_empty(){
Assert.assertTrue(StringUtil.isEmpty(" "));
}
Esta es mi Solucion:
public class StringUtil {
public static boolean isEmpty(String str) {
return str == null || str.trim().isEmpty();
}
}
public class StringUtilTest {
@Test
public void string_is_not_empty() {
Assert.assertFalse(StringUtil.isEmpty("Hola"));
}
@Test
public void string_is_empty() {
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_null_is_empty() {
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_with_spaces_is_empty() {
Assert.assertTrue(StringUtil.isEmpty(" "));
}
}
public static boolean isEmpty(String str) {
if(str == null)
return true;
str = str.trim();
return str.isEmpty();
}
public class StringUtilTest {
@Test
public void a_string_is_not_empty() {
assertFalse(StringUtil.isEmpty("Hello Platzi from Java with JUnit =D"));
}
@Test
public void this_is_an_empty_string() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void null_is_empty() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void a_string_with_spaces_is_empty() {
assertTrue(StringUtil.isEmpty(" "));
}
public static void main(String[] args) {
String result;
result = StringUtil.repeat("Hoola", 3);
assertEquals(result, "HoolaHoolaHoola");
result = StringUtil.repeat("Hoola", 1);
assertEquals(result, "Hoola");
}
}
Solución:
public class EmptyUtil {
public static boolean isEmpty(String str){
return str == null || str.trim().length() <= 0;
}
}
import org.junit.Assert;
import org.junit.Test;
public class EmptyUtilTest {
@Test
public void try_string_is_not_empty() {
Assert.assertFalse(EmptyUtil.isEmpty("Hello"));
}
@Test
public void try_string_is_empty() {
Assert.assertTrue(EmptyUtil.isEmpty(""));
}
@Test
public void try_string_is_null() {
Assert.assertTrue(EmptyUtil.isEmpty(null));
}
@Test
public void try_string_is_with_white_spaces() {
Assert.assertTrue(EmptyUtil.isEmpty(" "));
}
}
public static boolean isEmpty(String str) {
return str == null || str.trim().equals("");
}
@Test
public void isStringNotEmpty() {
//Aqui pruebo que un string no es vacio
assertFalse(StringUtil.isEmpty("Flavio"));
}
@Test
public void isStringEmpty() {
//Aqui pruebo que un string es vacio
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void isStringEmptyisNull() {
//Aqui pruebo que un string es null
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void isStringEmptyWhitSpaces() {
//Aqui pruebo que un string con espacios tambien es vacio
assertTrue(StringUtil.isEmpty(" "));
}
StringUtils
public static boolean isEmpty(String str){
return str == null || str.trim().isEmpty();
}
StringUtilsTest
@Test
public void check_empty_when_null(){
Assert.assertEquals(true, StringUtils.isEmpty(null));
}
@Test
public void check_empty(){
Assert.assertEquals(true, StringUtils.isEmpty(""));
}
@Test
public void check_not_empty(){
Assert.assertEquals(false, StringUtils.isEmpty("pepe"));
}
@Test
public void check_empty_whit_spaces(){
Assert.assertEquals(true, StringUtils.isEmpty(" "));
}
// Class
public class StringUtil {
public static boolean isEmpty(String str) {
return str == null || str.trim().equals("");
}
}
// Test class
public class StringUtilTest {
@Test public void testNotEmptyString() {
assertFalse(StringUtil.isEmpty(“abcde”));
}
@Test public void testEmptyString() {
assertTrue(StringUtil.isEmpty(""));
}
@Test public void testNullString() {
assertTrue(StringUtil.isEmpty(null));
}
@Test public void testSpacesString() {
assertTrue(StringUtil.isEmpty(" "));
}
}
isEmpty function
public static boolean isEmpty(String str) {
if (str != null) {
String trimStr = str.trim();
return trimStr.length() == 0;
}
return true;
}
Tests
@Test
public void is_empty_string() {
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void is_empty_string_with_spaces() {
Assert.assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void is_empty_null() {
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void is_not_empty_string() {
Assert.assertFalse(StringUtil.isEmpty("hola"));
}
//Clase StringUtil
package org.example.Prueba1.Util;
public class StringUtil
{
public static boolean isEmpty(String st) {
return st == null || st.trim().length() <=0;
}
}
//Clase StringUtilTest
package org.example.Prueba1.Util;
import org.junit.Test;
import static org.junit.Assert.*;
public class StringUtilTest{
@Test
public void string_is_not_empty(){
assertFalse(StringUtil.isEmpty(“abcde”));
}
@Test
public void string_is_empty(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_is_null(){
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_spaces(){
assertTrue(StringUtil.isEmpty(" "));
}
}
/**
* String.trim() = Método que elimina los caracteres blancos iniciales y finales
* de la cadena, retornando el resultado.
* String.isEmpty() = Método que retorna true si un String esa vacio.
*/
public static boolean isEmpty(String str) {
return str == null || str.trim().isEmpty();
}
/**
* Tests
*/
@Test
public void string_is_not_empty() {
Assert.assertFalse(StringUtil.isEmpty("Platzi"));
}
@Test
public void string_is_empty() {
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_is_empty_equals_null() {
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_spaces_is_empty() {
Assert.assertTrue(StringUtil.isEmpty(" "));
}
Aprovechando lo visto con las expresiones regulares se me ocurrió esta idea:
public class StringUtil {
public static boolean isEmpty(String string) {
return string == null || string.matches("^[\\s]*$");
}
}
Las siguientes son las pruebas que utilicé para verificar el comportamiento del método:
public class StringUtilTest {
@Test
public void nonEmptyString() {
try {
assertFalse(StringUtil.isEmpty("a"));
assertFalse(StringUtil.isEmpty("1"));
assertFalse(StringUtil.isEmpty("@"));
assertFalse(StringUtil.isEmpty("fY76r76rf&6fIUq43536"));
assertFalse(StringUtil.isEmpty("n o f a l l a"));
} catch (Exception ex) {
fail("An exception has ocurred: "+ex.getLocalizedMessage());
}
}
@Test
public void justQuotesIsAnEmptyString() {
try {
assertTrue(StringUtil.isEmpty(""));
} catch (Exception ex) {
fail("An exception has ocurred: "+ex.getLocalizedMessage());
}
}
@Test
public void nullIsConsideredAnEmptyString() {
try {
assertTrue(StringUtil.isEmpty(null));
} catch (Exception ex) {
fail("An exception has ocurred: "+ex.getLocalizedMessage());
}
}
@Test
public void justWhiteSpacesIsAnEmptyString() {
try {
assertTrue(StringUtil.isEmpty(" "));
assertTrue(StringUtil.isEmpty(" "));
assertTrue(StringUtil.isEmpty(" "));
} catch (Exception ex) {
fail("An exception has ocurred: "+ex.getLocalizedMessage());
}
}
}
public static boolean isEmpty (String str){
if(str == null ||str.trim().length()==0){
return true;
}
else{
return false;
}
}
Test
@Test
public void isEmpty_string_is_not_empty(){
assertFalse(StringUtil.isEmpty("test"));
}
@Test
public void isEmpty_string_empty_is_empty(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void isEmpty_string_null_is_empty(){
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void isEmpty_string_space_is_empty(){
assertTrue(StringUtil.isEmpty(" "));
}
Ya que isEmpty
de la clase String
valida si la longitud de la cadena es igual a 0, lo uso en mi método.
public static boolean isEmpty(String str) {
return str == null || str.trim().isEmpty();
}
Mi Aporte
Declaro un test parametrizado para validar varios casos con un solo test. Para eso uso las anotaciones @ParameterizedTest
y @ValueSource
sobre mi método de test, y para poder usarlas debo agregar como dependencia junit-jupiter-params
.
Gradle
dependencies {
....
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.7.2'
...
}
Maven
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.2</version>
</dependency>
Código StringUtilTest.java
@Test
public void should_validate_that_string_is_not_empty() {
assertFalse(StringUtil.isEmpty("sanders"));
}
@ParameterizedTest
@ValueSource(strings = {"", " ", "\t", "\n", "\r"})
public void should_validate_that_string_is_empty(String str) {
assertTrue(StringUtil.isEmpty(str));
}
Saludos, comparto mi clase stringUtil y test de prueba.
public class StringUtil {
public static boolean isEmpty(String str){
if(str == "" || str== null || str!=str.trim()){
return true;
}
return false;
}
}
Test
public class StringUtilTest {
StringUtil word;
@Test
void string_is_not_empty() {
assertFalse(word.isEmpty("hi"));
}
@Test
void string_is_empty() {
assertTrue(word.isEmpty(" hi"));
}
@Test
void string_null_is_empty() {
assertTrue(word.isEmpty(null));
}
}
Metodos:
1).
// StringUtil.java
public static boolean isEmpty(String string) {
return string == null ||
string.equals("") ||
string.trim().length() == 0;
}
2).
// StringUtilTest.java
// Test 1 - It is empty
@Test
public void stringIsEmpty(){
boolean response =
StringUtil.isEmpty(“wxyz”);
Assert.assertFalse(response);
}
// Test 2 - Not empty
@Test
public void stringIsNotEmpty(){
boolean response =
StringUtil.isEmpty("");
Assrt.assertTrue(response);
}
// Test 3 - Null is also an empty string
@Test
public void stringIsNull(){
boolean response =
StringUtil.is Empty(null);
Assert.assertTrue(response);
}
// Test - Contiene espacios
@Test
public void stringContainsSpaces(){
boolen response =
StringUtil.isEmpty(" "):
Assert.assertTrue(response);
}
}
public static boolean isEmpty(String string){
if(string.trim().isEmpty()){
return true;
}else{
return false;
}
}
Test
@Test
public void isNotEmptyString(){
assertEquals(false,StringUtil.isEmpty(“Hello”));
}
@Test
public void IsEmpty(){
assertEquals(true,StringUtil.isEmpty(""));
}
@Test
public void IsEmptyWhitSpaces(){
assertEquals(true,StringUtil.isEmpty(" "));
}
public static boolean isEmpty(String str){
return str == null || str.trim().length()==0;
}
@Test
public void testIsEmptyAnyString(){
assertFalse(StringUtil.isEmpty("h"));
}
@Test
public void testIsEmptyComillas(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void testIsEmptyNull(){
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void testIsEmptyVoid(){
assertTrue(StringUtil.isEmpty(" "));
}
StringUtil.java
public static boolean isEmpty(String text){
return text == null || text.trim().length() == 0;
StringUtilTest.Java
@Test
public void string_is not_empty(){
assertFalse(isEmpty("Hello world"));
}
@Test
public void null_is _empty_string(){
assertTrue(isEmpty(null));
}
@Test
public void quotes_is_empty(){
assertTrue(isEmpty(""));
}
@Test
public void quotes_whit_spaces_is_empty(){
assertTrue(isEmpty(" "));
}
Metodos
public static boolean isEmpty (String str){
if( str == null || str.trim().length() == 0){
return true;
}
return false;
}
Tests
@Test
public void validarVacio() {
StringUtil2 stringUtil2 = new StringUtil2();
assertTrue(stringUtil2.isEmpty(""));
}
@Test
public void validarNull() {
StringUtil2 stringUtil2 = new StringUtil2();
assertTrue(stringUtil2.isEmpty(null));
}
@Test
public void validarEspacios() {
StringUtil2 stringUtil2 = new StringUtil2();
assertTrue(stringUtil2.isEmpty(" "));
}
@Test
public void validarNombres() {
StringUtil2 stringUtil2 = new StringUtil2();
assertFalse(stringUtil2.isEmpty("Priscila"));
}
public static boolean stringEmpty(String str){
return str == null || str.trim().length() <= 0;
}
@Test
public void stringIsEmpty(){
boolean result = StringUtil.stringEmpty("");
Assert.assertEquals(true, result);
}
@Test
public void stringWithSpaceIsEmpty(){
boolean result = StringUtil.stringEmpty(" ");
Assert.assertEquals(true, result);
}
@Test
public void stringNullIsEmpty(){
boolean result = StringUtil.stringEmpty(null);
Assert.assertEquals(true, result);
}
@Test
public void stringIsNotEmpty(){
boolean result = StringUtil.stringEmpty("str");
Assert.assertEquals(false, result);
}
Método isEmpty
en la clase StringUtil
:
public static boolean isEmpty(String str) {
return str == null || str.trim().length() == 0;
}
Tests en la clase StringUtilTest
@Test
public void anyTextIsNotEmpty() {
Assert.assertFalse(StringUtil.isEmpty("bla bla bla"));
}
@Test
public void doubleQuotedStringIsEmpty() {
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void nullIsEmpty() {
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void stringOnlySpacesIsEmpty() {
Assert.assertTrue(StringUtil.isEmpty(" "));
}
public class StringUtil {
public static boolean isEmpty(String str) {
if (str == null || str == "" || str.trim().length() <= 0) {
return false;
} else {
return true;
}
}
}
public class StringUtilTestReto {
@Test
public void test_String_vacio_espacio(){
assertFalse(StringUtil.isEmpty(" "));
}
@Test
public void test_String_vacio(){
assertFalse(StringUtil.isEmpty(""));
}
@Test
public void test_String_vacio_null(){
assertFalse(StringUtil.isEmpty("null"));
}
@Test
public void test_String_no_vacio(){
assertTrue(StringUtil.isEmpty(""));
}
Función
public static boolean isEmpty(String str) {
return str == null || str.trim().equals("");
}
Tests:
@Test
public void string_is_full() {
assertFalse(StringUtil.isEmpty("Hola"));
}
@Test
public void string_is_empty() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_null_is_empty() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_spaces_is_empty() {
assertTrue(StringUtil.isEmpty(" "));
}
public class StringUtil {
public static boolean isEmpty(String str){
if (str != null) {
return str.trim().equals("");
}
return true;
}
}
public class StringUtilTest {
@Test
public void any_string_is_not_empty () {
Assert.assertFalse(StringUtil.isEmpty("abc"));
}
@Test
public void empty_string_is_empty () {
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void null_string_is_empty () {
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void space_string_is_empty () {
Assert.assertTrue(StringUtil.isEmpty(" "));
}
}
@Test
void correct_string_is_not_empty() {
Assertions.assertFalse(StringUtil.isEmpty(“test”));
}
@Test
void correct_double_quotes_is_empty() {
Assertions.assertTrue(StringUtil.isEmpty(""));
}
@Test
void correct_null_is_empty() {
Assertions.assertTrue(StringUtil.isEmpty(null));
}
@Test
void correct_string_with_spaces_is_empty() {
Assertions.assertTrue(StringUtil.isEmpty(" "));
}
public static boolean isEmpty(String str){
return str == null || str.trim().isEmpty();
}
@Test
public void any_string_is_not_empty(){
Assert.assertFalse(StringUtil.isEmpty(“any string”));
}
@Test
public void empty_string_is_empty(){
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void null_is_an_empty_string(){
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_with_spaces_is_an_empty_string(){
Assert.assertTrue(StringUtil.isEmpty(" "));
}
<code>
StringUtil.java
public static boolean isEmpty(String str){
return str == null || str.trim().equals("") ? true : false;
}
StringUtilTest.java
public class StringUtilsTest{
private StringUtils utils;
@Before
public void setup(){
utils = new StringUtils();
}
@Test
public void str_not_is_empty(){
assertFalse(utils.isEmpty("qwerty"));
}
@Test
public void str_is_empty(){
assertTrue(utils.isEmpty(""));
}
@Test
public void str_is_null(){
assertTrue(utils.isEmpty(null));
}
@Test
public void str_with_space(){
assertTrue(utils.isEmpty(" "));
}
}```
Clase StringUtil
package com.platzi.javatests.util;
public class StringUtil {
public static boolean isEmpty(String str){
return str==null || str.trim().equals("");
}
}
Test StringUtilTest
package com.platzi.javatests.util;
import com.platzi.javatests.payments.PaymentResponse;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import static org.junit.Assert.*;
public class StringUtilTest {
@Test
public void String_is_not_empty(){
assertFalse(StringUtil.isEmpty(" holas "));
}
@Test
public void quote_is_empty(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void null_is_empty(){
assertTrue(StringUtil.isEmpty(null));
}
}
StringUtil
public class StringUtil {
public static boolean isEmpty(String str) {
if (str == null) {
return true;
}
str = str.trim();
if (str.equals("")) {
return true;
}
return false;
}
}
StringUtilTest
class StringUtilTest {
@Test
public void string_is_not_empty() {
assertFalse(StringUtil.isEmpty("Alexis"));
}
@Test
public void string_is_empty() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_is_empty_with_spaces() {
assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void string_null_is_empty() {
assertTrue(StringUtil.isEmpty(null));
}
}
public class StringUtil {
public static boolean isEmpty(String str) {
if(str == null) {
return true;
}
if (str.isEmpty() || str.trim().equals("")) {
return true;
}
return false;
}
}
public class StringUtilTest {
@Test
public void empty_isEmpty() {
String cadena = "";
assertEquals(true, StringUtil.isEmpty(cadena));
}
@Test
public void empty_null() {
String cadena = null;
assertEquals(true, StringUtil.isEmpty(cadena));
}
@Test
public void empty_blanks() {
String cadena = " ";
assertEquals(true, StringUtil.isEmpty(cadena));
}
@Test
public void isNotEmpty() {
String cadena = "Hola amigos";
assertEquals(false, StringUtil.isEmpty(cadena));
}
}
StringUtil.java
public class StringUtil {
public static boolean isEmpty(String text) {
return (text == null) || text.trim().length() < 1;
}
}
StringUtilTest.java
@Test
public void string_is_not_empty() {
Assert.assertFalse(StringUtil.isEmpty("a"));
}
@Test
public void string_is_empty() {
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_is_null() {
Assert.assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_only_spaces() {
Assert.assertTrue(StringUtil.isEmpty(" "));
}
}
StringUtil
public static boolean isEmpty(String str) {
if (str == null){
return true;
}
return str.trim().isEmpty();
}
StringUtilTest
@Test
public void anyStringIsNotEmpty () {
assertFalse(StringUtil.isEmpty("anyString"));
}
@Test
public void DoubleQuotesIsEmpty () {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void nullIsEmpty () {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void spacesOnlyIsEmpty () {
assertTrue(StringUtil.isEmpty(" "));
}
StringUtil
public static boolean isEmpty(String str){
return !(str!=null && !str.trim().equals(""));
}
StringUtilTest
@Test
public void stringNullIsTrue(){
boolean result=StringUtil.isEmpty(null);
assertTrue(result);
}
@Test
public void stringEmpty(){
boolean result=StringUtil.isEmpty("");
assertTrue(result);
}
@Test
public void stringHasOnlySpaces(){
boolean result=StringUtil.isEmpty(" ");
assertTrue(result);
}
@Test
public void stringNotEmpty(){
boolean result=StringUtil.isEmpty("1 1");
assertFalse(result);
}
public static boolean isEmpty(String str) {
return str.length() <= 0;
}
Método isEmpty
public static boolean isEmpty (String str) {
if (str == null){
throw new NullPointerException();
} else {
return str.isEmpty() || str.trim().isEmpty();
}
}
Métodos del tipo test cumpliendo una única responsabilidad
@Test (expected = NullPointerException.class)
public void verify_is_str_null(){
StringUtil.isEmpty(null);
}
@Test
public void verify_is_str_empty(){
Assert.assertTrue(StringUtil.isEmpty(""));
}
@Test
public void verify_is_str_empty_with_spaces(){
Assert.assertTrue(StringUtil.isEmpty(" "));
}
@Test
public void verify_if_str_had_characters(){
Assert.assertFalse(StringUtil.isEmpty("test"));
}```
He subido mi reto a un repositorio en github
publicstaticbooleanisEmpty(String str){
return str == null || str.trim().length() <= 0;
}
@Test
publicvoidword_is_not_empty(){
assertFalse(StringUtil.isEmpty(“Palabra”));
}
@Test
publicvoidquotationMarks_is_Empty(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
publicvoidspace_is_Empty(){
String str = new String(" ");
assertTrue(StringUtil.isEmpty(str));
}
@Test
publicvoidnull_is_empty(){
assertTrue(StringUtil.isEmpty(null));
}
public class StringUtil {
public static boolean isEmpty(String str){
if(str == null){
return true;
}
return str.trim().length() <= 0;
}
public class StringUtilTest {
@Test
public void String_is_not_empty(){
assertTrue(StringUtil.isEmpty("1a2b"));
}
@Test
public void Just_quotes_are_empty(){
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void Null_is_empty(){
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void Space_is_empty(){
assertTrue(StringUtil.isEmpty(" "));
}
}
public class StringUtil {
public static boolean isEmpty(String str) {
str = str.trim();
return str!=null&&str!="";
}
}
public class StringUtilTest{
@Test
public void test_empty_string(){
assertTrue("This is an empty String", StringUtil.isEmpty(""));
}
@Test
public void test_spaced_string(){
assertTrue("Spaced Strings are empty Strings", StringUtil.isEmpty(" "));
}
@Test
public void test_null_string(){
assertTrue("Nulls are empty Strings", StringUtil.isEmpty(null));
}
@Test
public void test_correct_string(){
assertFalse("This is a correct String", StringUtil.isEmpty("This is a correct String"));
}
}
public static boolean isEmpty(String str) {
if (str != null && !str.trim().equals("")) {
return false;
} else {
return true;
}
}
@Test
public void string_is_not_empty() {
assertFalse(StringUtil.isEmpty("Hola"));
}
@Test
public void string_is_empty() {
assertTrue(StringUtil.isEmpty(""));
}
@Test
public void string_is_null() {
assertTrue(StringUtil.isEmpty(null));
}
@Test
public void string_have_empty_spaces() {
assertTrue(StringUtil.isEmpty(" "));
}
¿Quieres ver más aportes, preguntas y respuestas de la comunidad? Crea una cuenta o inicia sesión.