mercredi 29 novembre 2017

java unit test calculation exception error of custom exception

I have to test my code if I don't have errors in it, but when I try to call my class that calculates what I asked, it gives me an error about my HorsLimitException where I said that the limits are the min and the max of double I don't see why I get an error here's the code:

        public class MethodeCalcul {

        public MethodeCalcul(int uneVal1,int uneVal2,double uneValConv ) {
            }

        public static double methodeCalc(int val1, int val2, double valConv) throws Exception { //remonter exception
            int diffIndice;
            String nbZero = "1";
            double resultat;
           // String valConv2= String.valueOf(valConv);

            if(valConv < Double.MIN_VALUE || valConv > Double.MAX_VALUE) throw new HorsLimitException (); //lance exception 

            diffIndice = val1 - val2;
            for (int k = 0; k < Math.abs(diffIndice); k++) { //calcule la valeur absolue de la différence d'indice entre combo box 3 et 2 et ajoute le nombre de zero necessaire
                nbZero += "0";
            }

            if (diffIndice <= 0) {
                resultat = valConv / Double.parseDouble(nbZero); //division 
                nbZero = "1"; //remise a la valeur initial
            } else {
                resultat = valConv * Double.parseDouble(nbZero); //multiplication
                nbZero = "1";  //remise a la valeur initial
            }
            return resultat; //retourne un double
        }
    }
class HorsLimitException  extends  ArithmeticException {

        public HorsLimitException ()      {    //constructeur  par défaut
             super( "Tentative de d\u00e9passement de limite" );
           }

        public HorsLimitException ( String message )     {
             super( message );  
           }
}

that code is made to calculate numbers to convert between option like cm to m and etc. But my testing code doesn't work:

public class TestCalcul {

MethodeCalcul calcul;

public static void main (String args []) throws Exception{
    TestCalcul test= new TestCalcul();
    test.testMethodeCalcul(); 

}

//Test de la méthode  
public void testMethodeCalcul() throws Exception{

    // un premier cas de test
    calcul =  new MethodeCalcul(0, 0, 0);
    if ( calcul.methodeCalc(0,0,0) != 0)    
        System.out.println("Erreur de la methode methodeCalc()");

    // un deuxieme cas de test
    calcul =  new MethodeCalcul(1, 1, 1);
    if ( calcul.methodeCalc(1,1,1) != 0)        
        System.out.println("Erreur de la methode methodeCalc()");

         // un troisieme cas de test
    calcul =  new MethodeCalcul(2, 2, 2);
    if ( calcul.methodeCalc(2,2,2) != 0)        
        System.out.println("Erreur de la methode methodeCalc()");   

}
}

Aucun commentaire:

Enregistrer un commentaire