Classes Parameterized with Junit 4

Asked

Viewed 312 times

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?

1 answer

1


The problem is that you have committed a certain confusion in the method assertEquals.

This method receives the parameters in that order:

assertEquals(valorEsperado, valorAtualQueFoiRetornado)

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

assertEquals(valorEsperado, valorAtualQueFoiRetornado, delta)

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

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

That is to say:

  1. Expected value: output
  2. Value being tested: op.areaRetangulo(b, a)
  3. Accuracy: 0.0001 - means that the numbers will be checked taking into account only 4 decimal places. If there is a difference in the fifth decimal place, the numbers will still be considered equal.

You will put the delta according to your need. If you want 2 decimal places, for example, the delta would be 0.01

Browser other questions tagged

You are not signed in. Login or sign up in order to post.