Série da semana: Primeiros passos no React

Veja mais

Teste de Software JUnit

14/03/2016

1

Olá Pessoal alguém sabe usar "sapoha" de JUnit.

Zuera, zuera, desculpa o palavreado ai, é que já estou pesquisando a um tempo e não consigo fazer com que o teste funcione com um arquivo csv, xml ou xlsx por exemplo.

Já utilizei uma biblioteca de uma tal de easytech (easytest-core) mais sem sucesso.

Grato.

Antes que solicitem segue o codigo.
Classe Aluno
package com.miseravi;

/**
 *
 * @author Luis Calegari
 */
public class Aluno {

    public String aprovado(double n1, double n2, int freq) {
        if (n1 < 0 || n2 < 0 || freq < 0 || n1 > 10 || n2 > 10 || freq > 100) {
            return "dadosInvalidos";
        }

        if (freq < 75) {
            return "reprovado";
        }

        if ((n1 + n2) / 2 < 6) {
            return "reprovado";
        }
        
        return "aprovado";
    }
}


Classe TesteAluno
package com.miseravi;

import org.easetech.easytest.annotation.DataLoader;
import org.easetech.easytest.annotation.Param;
import org.easetech.easytest.loader.LoaderType;
import org.easetech.easytest.runner.DataDrivenTestRunner;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths = {"/home/calegari/testData.csv"}, loaderType = LoaderType.CSV)
public class AlunoTeste {

    @Test
    public void hello(
            @Param(name = "n1") double n1, 
            @Param(name = "n2") double n2, 
            @Param(name = "freq") int freq, 
            @Param(name = "saidaEsperada") String saida) {
        //your test conditions here
        Assert.assertEquals(saida, new Aluno().aprovado(n1, n2, freq));
    }
}


Segue dados do arquivo csv tbm
hallo,n1,n2,freq,saidaEsperada
,-0.5,-0.5,-1,entradaInvalida
,-0.5,-0.5,0,entradaInvalida
,-0.5,-0.5,74,entradaInvalida
,-0.5,-0.5,75,entradaInvalida
,-0.5,-0.5,100,entradaInvalida
,-0.5,-0.5,101,entradaInvalida
,0,-0.5,-1,entradaInvalida
,0,-0.5,0,entradaInvalida
,0,-0.5,74,entradaInvalida
,0,-0.5,75,entradaInvalida
,0,-0.5,100,entradaInvalida
,0,-0.5,101,entradaInvalida
,5.5,-0.5,-1,entradaInvalida
,5.5,-0.5,0,entradaInvalida
,5.5,-0.5,74,entradaInvalida
,5.5,-0.5,75,entradaInvalida
,5.5,-0.5,100,entradaInvalida
,5.5,-0.5,101,entradaInvalida
,6,-0.5,-1,entradaInvalida
,6,-0.5,0,entradaInvalida
,6,-0.5,74,entradaInvalida
,6,-0.5,75,entradaInvalida
,6,-0.5,100,entradaInvalida
,6,-0.5,101,entradaInvalida
,10,-0.5,-1,entradaInvalida
,10,-0.5,0,entradaInvalida
,10,-0.5,74,entradaInvalida
,10,-0.5,75,entradaInvalida
,10,-0.5,100,entradaInvalida
,10,-0.5,101,entradaInvalida
,11,-0.5,-1,entradaInvalida
,11,-0.5,0,entradaInvalida
,11,-0.5,74,entradaInvalida
,11,-0.5,75,entradaInvalida
,11,-0.5,100,entradaInvalida
,11,-0.5,101,entradaInvalida
,0,0,-1,entradaInvalida
,0,0,0,reprovado
,0,0,74,reprovado
,0,0,75,reprovado
,0,0,100,reprovado
,0,0,101,entradaInvalida
,-0.5,0,-1,entradaInvalida
,-0.5,0,0,entradaInvalida
,-0.5,0,74,entradaInvalida
,-0.5,0,75,entradaInvalida
,-0.5,0,100,entradaInvalida
,-0.5,0,101,entradaInvalida
,5.5,0,-1,entradaInvalida
,5.5,0,0,reprovado
,5.5,0,74,reprovado
,5.5,0,75,reprovado
,5.5,0,100,reprovado
,5.5,0,101,entradaInvalida
,6,0,-1,entradaInvalida
,6,0,0,reprovado
,6,0,74,reprovado
,6,0,75,reprovado
,6,0,100,reprovado
,6,0,101,entradaInvalida
,10,0,-1,entradaInvalida
,10,0,0,reprovado
,10,0,74,reprovado
,10,0,75,reprovado
,10,0,100,reprovado
,10,0,101,entradaInvalida
,11,0,-1,entradaInvalida
,11,0,0,entradaInvalida
,11,0,74,entradaInvalida
,11,0,75,entradaInvalida
,11,0,100,entradaInvalida
,11,0,101,entradaInvalida
,5.5,5.5,-1,entradaInvalida
,5.5,5.5,0,reprovado
,5.5,5.5,74,reprovado
,5.5,5.5,75,reprovado
,5.5,5.5,100,reprovado
,5.5,5.5,101,entradaInvalida
,-0.5,5.5,-1,entradaInvalida
,-0.5,5.5,0,entradaInvalida
,-0.5,5.5,74,entradaInvalida
,-0.5,5.5,75,entradaInvalida
,-0.5,5.5,100,entradaInvalida
,-0.5,5.5,101,entradaInvalida
,5.5,5.5,-1,entradaInvalida
,5.5,5.5,0,reprovado
,5.5,5.5,74,reprovado
,5.5,5.5,75,reprovado
,5.5,5.5,100,reprovado
,5.5,5.5,101,entradaInvalida
,6,5.5,-1,entradaInvalida
,6,5.5,0,reprovado
,6,5.5,74,reprovado
,6,5.5,75,reprovado
,6,5.5,100,reprovado
,6,5.5,101,entradaInvalida
,10,5.5,-1,entradaInvalida
,10,5.5,0,reprovado
,10,5.5,74,reprovado
,10,5.5,75,reprovado
,10,5.5,100,aprovado
,10,5.5,101,entradaInvalida
,11,5.5,-1,entradaInvalida
,11,5.5,0,entradaInvalida
,11,5.5,74,entradaInvalida
,11,5.5,75,entradaInvalida
,11,5.5,100,entradaInvalida
,11,5.5,101,entradaInvalida
,6,6,-1,entradaInvalida
,6,6,0,reprovado
,6,6,74,reprovado
,6,6,75,reprovado
,6,6,100,aprovado
,6,6,101,entradaInvalida
,-0.5,6,-1,entradaInvalida
,-0.5,6,0,entradaInvalida
,-0.5,6,74,entradaInvalida
,-0.5,6,75,entradaInvalida
,-0.5,6,100,entradaInvalida
,-0.5,6,101,entradaInvalida
,5.5,6,-1,entradaInvalida
,5.5,6,0,reprovado
,5.5,6,74,reprovado
,5.5,6,75,reprovado
,5.5,6,100,reprovado
,5.5,6,101,entradaInvalida
,6,6,-1,entradaInvalida
,6,6,0,reprovado
,6,6,74,reprovado
,6,6,75,reprovado
,6,6,100,aprovado
,6,6,101,entradaInvalida
,10,6,-1,entradaInvalida
,10,6,0,reprovado
,10,6,74,reprovado
,10,6,75,reprovado
,10,6,100,aprovado
,10,6,101,entradaInvalida
,11,6,-1,entradaInvalida
,11,6,0,entradaInvalida
,11,6,74,entradaInvalida
,11,6,75,entradaInvalida
,11,6,100,entradaInvalida
,11,6,101,entradaInvalida
,10,10,-1,entradaInvalida
,10,10,0,reprovado
,10,10,74,reprovado
,10,10,75,reprovado
,10,10,100,aprovado
,10,10,101,entradaInvalida
,-0.5,10,-1,entradaInvalida
,-0.5,10,0,entradaInvalida
,-0.5,10,74,entradaInvalida
,-0.5,10,75,entradaInvalida
,-0.5,10,100,entradaInvalida
,-0.5,10,101,entradaInvalida
,5.5,10,-1,entradaInvalida
,5.5,10,0,reprovado
,5.5,10,74,reprovado
,5.5,10,75,reprovado
,5.5,10,100,aprovado
,5.5,10,101,entradaInvalida
,6,10,-1,entradaInvalida
,6,10,0,reprovado
,6,10,74,reprovado
,6,10,75,reprovado
,6,10,100,aprovado
,6,10,101,entradaInvalida
,10,10,-1,entradaInvalida
,10,10,0,reprovado
,10,10,74,reprovado
,10,10,75,reprovado
,10,10,100,aprovado
,10,10,101,entradaInvalida
,11,10,-1,entradaInvalida
,11,10,0,entradaInvalida
,11,10,74,entradaInvalida
,11,10,75,entradaInvalida
,11,10,100,entradaInvalida
,11,10,101,entradaInvalida
,10.5,10.5,-1,entradaInvalida
,10.5,10.5,0,entradaInvalida
,10.5,10.5,74,entradaInvalida
,10.5,10.5,75,entradaInvalida
,10.5,10.5,100,entradaInvalida
,10.5,10.5,101,entradaInvalida
,0,10.5,-1,entradaInvalida
,0,10.5,0,entradaInvalida
,0,10.5,74,entradaInvalida
,0,10.5,75,entradaInvalida
,0,10.5,100,entradaInvalida
,0,10.5,101,entradaInvalida
,5.5,10.5,-1,entradaInvalida
,5.5,10.5,0,entradaInvalida
,5.5,10.5,74,entradaInvalida
,5.5,10.5,75,entradaInvalida
,5.5,10.5,100,entradaInvalida
,5.5,10.5,101,entradaInvalida
,6,10.5,-1,entradaInvalida
,6,10.5,0,entradaInvalida
,6,10.5,74,entradaInvalida
,6,10.5,75,entradaInvalida
,6,10.5,100,entradaInvalida
,6,10.5,101,entradaInvalida
,10,10.5,-1,entradaInvalida
,10,10.5,0,entradaInvalida
,10,10.5,74,entradaInvalida
,10,10.5,75,entradaInvalida
,10,10.5,100,entradaInvalida
,10,10.5,101,entradaInvalida
,11,10.5,-1,entradaInvalida
,11,10.5,0,entradaInvalida
,11,10.5,74,entradaInvalida
,11,10.5,75,entradaInvalida
,11,10.5,100,entradaInvalida
,11,10.5,101,entradaInvalida
Responder

Post mais votado

14/03/2016

Tambem quero rsrsrs..."sapoha" hahaha.

Pelo seu código, está usando o Netbeans ok?

Veja os links:

[url]https://netbeans.org/kb/docs/java/junit-intro_pt_BR.html[/url]

[url]https://www.youtube.com/watch?v=ThuwLm0O6zE[/url]
Responder

Mais Posts

14/03/2016

Luis Calegari

Vlw Eduardo Pessoa...
Ótimos links... qdo conseguir fazer os testes com um arquivo externo posto aqui pra galera.
Responder

14/03/2016

Luis Calegari

Ai galera consegui.... Segue exemplo utilizando netbeans e um projeto maven.

pom.xml
        <dependency>
            <groupId>pl.pragmatists</groupId>
            <artifactId>JUnitParams</artifactId>
            <version>1.0.2</version>
            <scope>test</scope>
        </dependency>


Classe Aluno
package com.miseravi;

/**
 *
 * @author Luis Calegari
 */
public class Aluno {

    public String aprovado(double n1, double n2, int freq) {
        if (n1 < 0 || n1 > 10 || n2 < 0 || n2 > 10 || freq < 0 || freq > 100) {
            return "entradaInvalida";
        }

        if (freq < 75 || (n1 + n2) / 2 < 6) {
            return "reprovado";
        }

        return "aprovado";
    }
}


Classe AlunoTeste
package com.miseravi;

import junitparams.FileParameters;
import junitparams.JUnitParamsRunner;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 *
 * @author Luis Calegari
 */
@RunWith(JUnitParamsRunner.class)
public class AlunoTeste {

    @Test
    @FileParameters("testData.csv")
    public void hello(double n1, double n2, int freq, String saidaEsperada) {
        Assert.assertEquals(saidaEsperada, new Aluno().aprovado(n1, n2, freq));
    }
}


Arquivo de Dados "csv"
-0.5,-0.5,-1,entradaInvalida
-0.5,-0.5,0,entradaInvalida
-0.5,-0.5,74,entradaInvalida
-0.5,-0.5,75,entradaInvalida
-0.5,-0.5,100,entradaInvalida
-0.5,-0.5,101,entradaInvalida
0,-0.5,-1,entradaInvalida
0,-0.5,0,entradaInvalida
0,-0.5,74,entradaInvalida
0,-0.5,75,entradaInvalida
0,-0.5,100,entradaInvalida
0,-0.5,101,entradaInvalida
5.5,-0.5,-1,entradaInvalida
5.5,-0.5,0,entradaInvalida
5.5,-0.5,74,entradaInvalida
5.5,-0.5,75,entradaInvalida
5.5,-0.5,100,entradaInvalida
5.5,-0.5,101,entradaInvalida
6,-0.5,-1,entradaInvalida
6,-0.5,0,entradaInvalida
6,-0.5,74,entradaInvalida
6,-0.5,75,entradaInvalida
6,-0.5,100,entradaInvalida
6,-0.5,101,entradaInvalida
10,-0.5,-1,entradaInvalida
10,-0.5,0,entradaInvalida
10,-0.5,74,entradaInvalida
10,-0.5,75,entradaInvalida
10,-0.5,100,entradaInvalida
10,-0.5,101,entradaInvalida
11,-0.5,-1,entradaInvalida
11,-0.5,0,entradaInvalida
11,-0.5,74,entradaInvalida
11,-0.5,75,entradaInvalida
11,-0.5,100,entradaInvalida
11,-0.5,101,entradaInvalida
0,0,-1,entradaInvalida
0,0,0,reprovado
0,0,74,reprovado
0,0,75,reprovado
0,0,100,reprovado
0,0,101,entradaInvalida
-0.5,0,-1,entradaInvalida
-0.5,0,0,entradaInvalida
-0.5,0,74,entradaInvalida
-0.5,0,75,entradaInvalida
-0.5,0,100,entradaInvalida
-0.5,0,101,entradaInvalida
5.5,0,-1,entradaInvalida
5.5,0,0,reprovado
5.5,0,74,reprovado
5.5,0,75,reprovado
5.5,0,100,reprovado
5.5,0,101,entradaInvalida
6,0,-1,entradaInvalida
6,0,0,reprovado
6,0,74,reprovado
6,0,75,reprovado
6,0,100,reprovado
6,0,101,entradaInvalida
10,0,-1,entradaInvalida
10,0,0,reprovado
10,0,74,reprovado
10,0,75,reprovado
10,0,100,reprovado
10,0,101,entradaInvalida
11,0,-1,entradaInvalida
11,0,0,entradaInvalida
11,0,74,entradaInvalida
11,0,75,entradaInvalida
11,0,100,entradaInvalida
11,0,101,entradaInvalida
5.5,5.5,-1,entradaInvalida
5.5,5.5,0,reprovado
5.5,5.5,74,reprovado
5.5,5.5,75,reprovado
5.5,5.5,100,reprovado
5.5,5.5,101,entradaInvalida
-0.5,5.5,-1,entradaInvalida
-0.5,5.5,0,entradaInvalida
-0.5,5.5,74,entradaInvalida
-0.5,5.5,75,entradaInvalida
-0.5,5.5,100,entradaInvalida
-0.5,5.5,101,entradaInvalida
5.5,5.5,-1,entradaInvalida
5.5,5.5,0,reprovado
5.5,5.5,74,reprovado
5.5,5.5,75,reprovado
5.5,5.5,100,reprovado
5.5,5.5,101,entradaInvalida
6,5.5,-1,entradaInvalida
6,5.5,0,reprovado
6,5.5,74,reprovado
6,5.5,75,reprovado
6,5.5,100,reprovado
6,5.5,101,entradaInvalida
10,5.5,-1,entradaInvalida
10,5.5,0,reprovado
10,5.5,74,reprovado
10,5.5,75,aprovado
10,5.5,100,aprovado
10,5.5,101,entradaInvalida
11,5.5,-1,entradaInvalida
11,5.5,0,entradaInvalida
11,5.5,74,entradaInvalida
11,5.5,75,entradaInvalida
11,5.5,100,entradaInvalida
11,5.5,101,entradaInvalida
6,6,-1,entradaInvalida
6,6,0,reprovado
6,6,74,reprovado
6,6,75,aprovado
6,6,100,aprovado
6,6,101,entradaInvalida
-0.5,6,-1,entradaInvalida
-0.5,6,0,entradaInvalida
-0.5,6,74,entradaInvalida
-0.5,6,75,entradaInvalida
-0.5,6,100,entradaInvalida
-0.5,6,101,entradaInvalida
5.5,6,-1,entradaInvalida
5.5,6,0,reprovado
5.5,6,74,reprovado
5.5,6,75,reprovado
5.5,6,100,reprovado
5.5,6,101,entradaInvalida
6,6,-1,entradaInvalida
6,6,0,reprovado
6,6,74,reprovado
6,6,75,aprovado
6,6,100,aprovado
6,6,101,entradaInvalida
10,6,-1,entradaInvalida
10,6,0,reprovado
10,6,74,reprovado
10,6,75,aprovado
10,6,100,aprovado
10,6,101,entradaInvalida
11,6,-1,entradaInvalida
11,6,0,entradaInvalida
11,6,74,entradaInvalida
11,6,75,entradaInvalida
11,6,100,entradaInvalida
11,6,101,entradaInvalida
10,10,-1,entradaInvalida
10,10,0,reprovado
10,10,74,reprovado
10,10,75,aprovado
10,10,100,aprovado
10,10,101,entradaInvalida
-0.5,10,-1,entradaInvalida
-0.5,10,0,entradaInvalida
-0.5,10,74,entradaInvalida
-0.5,10,75,entradaInvalida
-0.5,10,100,entradaInvalida
-0.5,10,101,entradaInvalida
5.5,10,-1,entradaInvalida
5.5,10,0,reprovado
5.5,10,74,reprovado
5.5,10,75,aprovado
5.5,10,100,aprovado
5.5,10,101,entradaInvalida
6,10,-1,entradaInvalida
6,10,0,reprovado
6,10,74,reprovado
6,10,75,aprovado
6,10,100,aprovado
6,10,101,entradaInvalida
10,10,-1,entradaInvalida
10,10,0,reprovado
10,10,74,reprovado
10,10,75,aprovado
10,10,100,aprovado
10,10,101,entradaInvalida
11,10,-1,entradaInvalida
11,10,0,entradaInvalida
11,10,74,entradaInvalida
11,10,75,entradaInvalida
11,10,100,entradaInvalida
11,10,101,entradaInvalida
10.5,10.5,-1,entradaInvalida
10.5,10.5,0,entradaInvalida
10.5,10.5,74,entradaInvalida
10.5,10.5,75,entradaInvalida
10.5,10.5,100,entradaInvalida
10.5,10.5,101,entradaInvalida
0,10.5,-1,entradaInvalida
0,10.5,0,entradaInvalida
0,10.5,74,entradaInvalida
0,10.5,75,entradaInvalida
0,10.5,100,entradaInvalida
0,10.5,101,entradaInvalida
5.5,10.5,-1,entradaInvalida
5.5,10.5,0,entradaInvalida
5.5,10.5,74,entradaInvalida
5.5,10.5,75,entradaInvalida
5.5,10.5,100,entradaInvalida
5.5,10.5,101,entradaInvalida
6,10.5,-1,entradaInvalida
6,10.5,0,entradaInvalida
6,10.5,74,entradaInvalida
6,10.5,75,entradaInvalida
6,10.5,100,entradaInvalida
6,10.5,101,entradaInvalida
10,10.5,-1,entradaInvalida
10,10.5,0,entradaInvalida
10,10.5,74,entradaInvalida
10,10.5,75,entradaInvalida
10,10.5,100,entradaInvalida
10,10.5,101,entradaInvalida
11,10.5,-1,entradaInvalida
11,10.5,0,entradaInvalida
11,10.5,74,entradaInvalida
11,10.5,75,entradaInvalida
11,10.5,100,entradaInvalida
11,10.5,101,entradaInvalida
Responder