0
I have an exercise to solve involving classes parameterized in Java and I’m a little confused.
The exercise is as follows:
Program a parameterized class to test the method with the following input and output values:
{b:0, a:0, saída:0}, {b:1, a:1, saída:1}, {b:2, a:0, saída:0}, {b:0, a:2, saída:0}
The class that has the method corresponding to the exercise is this:
package aula;
import java.util.concurrent.TimeUnit;
public class Operacao {
/* retorna a área de um quadrado */
public double areaRetangulo(double b, double a) throws Exception {
if (b < 0 || a < 0) {
throw new Exception("Valor negativo");
}
return b * a;
}
/*
* retorna true se o objeto é um subtipo de Number
*/
public boolean isNumber(Object obj) throws Exception {
return obj instanceof java.lang.Number;
}
public int timer(int cont) throws InterruptedException {
/* sleep por cont segundos */
TimeUnit.SECONDS.sleep(cont);
return 1;
}
}
In order to test Operation, I created the following class using Junit4:
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import aula.Operacao;
import org.junit.Test;
@RunWith(Parameterized.class)
public class OurTest {
// @Test
// public void test() {
// fail("Not yet implemented");
// }
private double a;
private double b;
private double saida;
private Operacao op;
@Before
public void initialize() {
op = new Operacao();
}
public OurTest(double a, double b, double saida) {
this.a = a;
this.b = b;
this.saida = saida;
}
@Parameterized.Parameters
public static Collection parametros() {
return Arrays.asList(new Object[][]{
{0, 0, 0},
{1, 1, 1},
{2, 0, 0},
{0, 2, 0} });
}
@Test
public void test1() throws Exception {
System.out.println("Testando: " +saida);
assertEquals(b,a, op.areaRetangulo(b, a));
}
}
However, the results left me a little confused, were the following: the first two turned green and the last two tests, blue. I need to make them all green, can someone explain to me how it works right?