|
| 1 | +/* |
| 2 | +Installar JUnit para las pruebas unitarias en |
| 3 | +https://github.com/junit-team/junit4/wiki/Download-and-Install |
| 4 | +Se puede usar el .jar para proyectos creados con ant |
| 5 | +Para proyectos creados con Maven se puede usar el plugin |
| 6 | +<dependency> |
| 7 | + <groupId>junit</groupId> |
| 8 | + <artifactId>junit</artifactId> |
| 9 | + <version>4.13</version> |
| 10 | + <scope>test</scope> |
| 11 | +</dependency> |
| 12 | +Agregando esto a las dependencias del proyecto en el pom.xml |
| 13 | +*/ |
| 14 | + |
| 15 | +package com.jimsimrodev; |
| 16 | + |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.Collection; |
| 19 | +import org.junit.jupiter.api.Assertions; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | +import org.junit.runners.Parameterized; |
| 23 | +import java.time.LocalDate; |
| 24 | +import static java.time.format.DateTimeFormatter.ofPattern; |
| 25 | +import static com.jimsimrodev.Suma.suma; |
| 26 | +import static org.junit.Assert.assertEquals; |
| 27 | +import static org.junit.Assert.assertFalse; |
| 28 | +import static org.junit.Assert.assertNotNull; |
| 29 | + |
| 30 | +import static java.time.format.DateTimeFormatter.ofPattern; |
| 31 | + |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.Collection; |
| 34 | +import java.time.LocalDate; |
| 35 | + |
| 36 | +/** |
| 37 | + * Suma |
| 38 | + */ |
| 39 | +public class Suma { |
| 40 | + public static double suma(double a, double b) { |
| 41 | + return a + b; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @return |
| 46 | + */ |
| 47 | + public static Collection<String[]> data() { |
| 48 | + return Arrays.asList( |
| 49 | + new String[][] { |
| 50 | + { "Name: ", "JimsimroDev" }, |
| 51 | + { "age: ", "28" }, |
| 52 | + { "birthDate: ", LocalDate.now().format(ofPattern("dd/MM/yyyy")) }, |
| 53 | + { "programinLenguages: ", "Java, Python, C++, C#" }, |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + public static void print(double resultado) { |
| 58 | + System.out.printf("La suma es: %s%n", resultado); |
| 59 | + } |
| 60 | + |
| 61 | + // Esta clase debe ir en el paquete de pruebas unitarias |
| 62 | + |
| 63 | + @RunWith(Parameterized.class) |
| 64 | + public class SumaTest { |
| 65 | + private final String key; |
| 66 | + private final String value; |
| 67 | + |
| 68 | + public SumaTest(String key, String value) { |
| 69 | + this.key = key; |
| 70 | + this.value = value; |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void sumaTest() { |
| 75 | + |
| 76 | + // Para operaciones con decimales |
| 77 | + Assertions.assertEquals(6.0, suma(4.0, 2.0), 0.0001); |
| 78 | + assertEquals(-2.0, suma(5.0, -7.0), 0.0001); |
| 79 | + assertEquals(0.0, suma(0.0, 0.0), 0.0001); |
| 80 | + assertEquals(4.6, suma(2.5, 2.1), 0.0001); |
| 81 | + assertEquals(4.1, suma(2.1, 2.0), 0.0001); |
| 82 | + assertEquals(5.0, suma(2.5, 2.5), 0.0001); |
| 83 | + assertEquals(4.5, suma(2, 2.5), 0.0001); |
| 84 | + // assertEquals(5.0, suma("2.5", 2.5), 0.0001);//error |
| 85 | + } |
| 86 | + |
| 87 | + // Pruebas parametrizables |
| 88 | + @Parameterized.Parameters |
| 89 | + public static Collection<String[]> data() { |
| 90 | + return Arrays.asList( |
| 91 | + new String[][] { |
| 92 | + { "name: ", "jimsimrodev" }, |
| 93 | + { "age: ", "28" }, |
| 94 | + { "birthDate: ", LocalDate.now().format(ofPattern("dd/MM/yyyy")) }, |
| 95 | + { "programinlenguages: ", "java, python, c++, c#" }, |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testfieldsexist() { |
| 101 | + assertNotNull("La clave no debe ser nula", key); |
| 102 | + assertFalse("La clave no debe estar vacía", key.trim().isEmpty()); |
| 103 | + assertNotNull("El valor no debe ser nulo", value); |
| 104 | + assertFalse("El valor no debe estar vacío", value.trim().isEmpty()); |
| 105 | + |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + public static void main(String[] args) { |
| 111 | + print(suma(4, 2)); |
| 112 | + print(suma(-4.0, 2.0)); |
| 113 | + |
| 114 | + for (String[] data : data()) { |
| 115 | + for (String string : data) { |
| 116 | + System.out.print(string); |
| 117 | + } |
| 118 | + System.out.println(" "); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments