Classes Parametrizadas with JUnit 4

0

I have an exercise to solve involving classes parameterized in Java and I'm a bit confused.

The exercise is as follows:

  

Program a parameterized class to   test the areaA square 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 corresponding method 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 made me a bit 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?

    
asked by anonymous 08.03.2018 / 03:00

1 answer

1

The problem is that you have made some confusion in the assertEquals method.

This method receives the parameters in that order:

assertEquals(valorEsperado, valorAtualQueFoiRetornado)

However, in the case of values of type double , the assertEquals with only these two parameters is considered obsolete. For values double besides these two values it is also necessary to pass a delta, like this:

assertEquals(valorEsperado, valorAtualQueFoiRetornado, delta)

This delta is the precision of the decimal places. In your case, your call should look like this:

assertEquals(saida, op.areaRetangulo(b, a), 0.0001);

That is:

  • Expected value: output
  • Value being tested: op.areaRetangle (b, a)
  • Accuracy: 0.0001 - means that the numbers will be checked taking into account only four decimal places. If there is a difference in the fifth decimal place, the numbers will still be considered equal.
  • You will place the delta according to your need. If you want 2 decimal places, for example, the delta would be 0.01

        
    10.03.2018 / 16:18