Ajuda JUnit

2 respostas
E

boa tarde,

Estou tentando fazer teste do meu validar cpf com JUnit. Só que não estou conseguindo.Espero que vocês possam me ajudar mais uma vez.

Código Do Test.

public class UtilsTest {
    
    public UtilsTest() {
    }
    
    @BeforeClass
    public static void setUpClass() {
    }
    
    @AfterClass
    public static void tearDownClass() {
    }
    
    @Before
    public void setUp() {
    }
    
    @After
    public void tearDown() {
    }

    /**
     * Test of validaCPF method, of class Utils.
     */
    @Test
    public void testValidaCPF() {
        System.out.println("validaCPF");
        String CPF = "";
        boolean expResult = false;
        boolean result = Utils.validaCPF(CPF);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    }
}

Código da Classe Util - ValidaCPF

public class Utils {

      /**
     * Realiza o cálculo de digito verificador de CPF
     */

    private static String calcDigVerif(String num) {    
        Integer primDig, segDig;    
        int soma = 0, peso = 10;    
        for (int i = 0; i < num.length(); i++)    
                soma += Integer.parseInt(num.substring(i, i + 1)) * peso--;    
   
        if (soma % 11 == 0 | soma % 11 == 1)    
            primDig = new Integer(0);    
        else    
            primDig = new Integer(11 - (soma % 11));    
   
        soma = 0;    
        peso = 11;    
        for (int i = 0; i < num.length(); i++)    
                soma += Integer.parseInt(num.substring(i, i + 1)) * peso--;    
             
        soma += primDig.intValue() * 2;    
        if (soma % 11 == 0 | soma % 11 == 1)    
            segDig = new Integer(0);    
        else    
            segDig = new Integer(11 - (soma % 11));    
   
        return primDig.toString() + segDig.toString();    
    }    

    /**
     * Valida cpf
     * @return true se for válido, false se inválido
     */
    public static boolean validaCPF(String CPF) {
        
        
         CPF = CPF.replace(".", "");
        CPF = CPF.replace("-", "");
        if (CPF.length() != 11)    
            return false; 
        
        if (CPF.equals("[telefone removido]") || CPF.equals("[telefone removido]") ||
        CPF.equals("[telefone removido]") || CPF.equals("[telefone removido]") ||
        CPF.equals("[telefone removido]") || CPF.equals("[telefone removido]") ||
        CPF.equals("[telefone removido]") || CPF.equals("[telefone removido]") ||
        CPF.equals("[telefone removido]") || CPF.equals("[telefone removido]") ||
       (CPF.length() != 11))
       return(false);

      
              
          
   
        String numDig = CPF.substring(0, 9);    
        return calcDigVerif(numDig).equals(CPF.substring(9, 11));    
    }
}


Como faço para fazer um teste do cpf pois o meu código do Test ?
Até onde eu fiz até agora no da que o teste falhou.

2 Respostas

Luiz_Aguiar
@Test  
    public void testValidaCPF() {  
        String CPF = "[CPF removido]"; 
        assertTrue(Utils.validaCPF(CPF));  
    }

Se colocar um CPF real ele tem que passar, se não passar ai vc verifica se o código do calculo está correto, até o teste passar.

[]s

E

Obrigado pela resposta, funcionou perfeitamente.

Criado 8 de abril de 2013
Ultima resposta 8 de abr. de 2013
Respostas 2
Participantes 2