Partage
  • Partager sur Facebook
  • Partager sur Twitter

Tester des classes ?

    20 juin 2020 à 1:56:30

    Bonjour ,

    Je viens demander conseil car j'ai un exercice coté a faire mais le professeur explique super mal . Je doit faire tester des classes que le professeur nous a données avec des classes vides que nous on doit remplir. 

    Je travaille sur netbeans 8 avec Junit4.

    Je dois tous faire passer en vert mais je ne vois pas le problème ?

    Car dans ma classe Main() lorsque je fais les erreurs dans l'instanciation de mon objet les erreurs apparaissent bien mais si je lance les test sur la classe objet account() , il me mets que aucun test ne passe .Mais, je ne vois pas le problème. 

    Ma classe avec les valeurs à tester 

    public class AccountTest extends TestCase {
        
        public AccountTest(String testName) {
            super(testName);
        }
    
        public void testConstuctor(){
            try {
                new Account("BE1");
                fail("Account number is to short");
            } catch (WrongNumberAccountException ex) {
                assertEquals("Your account number must be between 14 and 34 charcters composed only by digit and letter"
                        , ex.getMessage(), "Your exception message is not correct");
            } catch (AccountAbsentException ex) {
                fail("Account existance not checked");
            }
            try {
                new Account("AZE12323233333333333333333333333333333333333333333");
                fail("Account number is to long");
            } catch (WrongNumberAccountException ex) {
                assertEquals("Your account number must be between 14 and 34 charcters composed only by digit and letter"
                        , ex.getMessage(), "Your exception message is not correct");
            }catch (AccountAbsentException ex) {
                fail("Account existance not checked");
            }
            try {
                new Account("FR12121212121212121%");
                fail("Account contains other that Digit or letter");
            } catch (WrongNumberAccountException ex) {
                assertEquals("Your account number must be between 14 and 34 charcters composed only by digit and letter"
                        , ex.getMessage(), "Your exception message is not correct");
            }catch (AccountAbsentException ex) {
                fail("Account existance not checked");
            }
            try {
                new Account("FR12121212121212121");
                fail("Account number wrong");
            } catch (WrongNumberAccountException ex) {
                assertEquals("Your exception message is not correct", "Your account number wrong, international check code not correct"
                        , ex.getMessage());
            }catch (AccountAbsentException ex) {
                fail("Account existance not checked");
            }
            try {
                new Account("BE87121212121295");
                fail("Account number wrong");
            } catch (WrongNumberAccountException ex) {
                assertEquals("Your exception message is not correct", "Your account number wrong, Belgian check code not correct"
                        , ex.getMessage());
            }catch (AccountAbsentException ex) {
                fail("Account existance not checked");
            }
            try {
                new Account("BE8712121212129");
                fail("Belgian Account number Short");
            } catch (WrongNumberAccountException ex) {
                assertEquals("Your exception message is not correct", "Your Belgian account number wrong, Belgian Number too Short"
                        , ex.getMessage());
            }catch (AccountAbsentException ex) {
                fail("Account existance not checked");
            }
            try {
                new Account("BE871212121212941");
                fail("Belgian Account number Long");
            } catch (WrongNumberAccountException ex) {
                assertEquals("Your exception message is not correct", "Your Belgian account number wrong, Belgian Number too long"
                        , ex.getMessage());
            }catch (AccountAbsentException ex) {
                fail("Account existance not checked");
            }
    ////        try {
    ////            new Account("BE87121212121295");
    ////            fail("Belgian Account national code wrong");
    ////        } catch (WrongNumberAccountException ex) {
    ////            assertEquals("Your exception message is not correct", "Your Belgian account number wrong, national check code not correct"
    ////                    , ex.getMessage());
    ////        }catch (AccountAbsentException ex) {
    ////            fail("Account existance not checked");
    ////        }
            try {
                new Account("BE88121212121294");
                fail("Belgian Account international code wrong");
            } catch (WrongNumberAccountException ex) {
                assertEquals("Your exception message is not correct", "Your Belgian account number wrong, international check code not correct"
                        , ex.getMessage());
            }catch (AccountAbsentException ex) {
                fail("Account existance not checked");
            }
            try {
                Account a = new Account("BE87121212121294");
            } catch (WrongNumberAccountException | AccountAbsentException ex) {
                fail("Belgian Account Correct");
            }
        }
        
        public void testGetAmount(){
            try {
                Account a = new Account("BE87121212121294");
                assertEquals("The amount is not correct",1572.12, a.getAmount());
            } catch (WrongNumberAccountException | AccountAbsentException ex) {
                fail("Belgian Account Correct");
            } 
        }
        
        
        public void testGiveMoney(){
            try {
                Account a = new Account("BE87121212121294");
                a.giveMoney(-100, "1234");
                fail("You pincode is not correct");
            } catch (WrongNumberAccountException | AccountAbsentException ex) {
                fail("Belgian Account Correct");
            } catch (IllegalAmountException ex){
                fail("Before Check pinCode");
            } catch (WrongPinCodeException ex){
                assertEquals("You PinCode is not Correct"
                        , ex.getMessage(), "Your exception message is not correct");
            }
            try {
                Account a = new Account("BE87121212121294");
                a.giveMoney(-100, "3212");
                fail("You cannot give negative amount");
            } catch (WrongNumberAccountException | AccountAbsentException ex) {
                fail("Belgian Account Correct");
            } catch (IllegalAmountException ex){
               assertEquals("Your exception message is not correct", "You cannot give a negative amount"
                        , ex.getMessage()); 
            } catch (WrongPinCodeException ex){
                fail("PinCode correct");
            }
            try {
                Account a = new Account("BE87121212121294");
                a.giveMoney(2000, "3212");
                fail("This amount is too high");
            } catch (WrongNumberAccountException | AccountAbsentException ex) {
                fail("Belgian Account Correct");
            } catch (IllegalAmountException ex){
                assertEquals("Your exception message is not correct", "You cannot give a to high amount"
                        , ex.getMessage());
            }catch (WrongPinCodeException ex){
                fail("PinCode correct");
            }
            try {
                Account a = new Account("BE87121212121294");
                assertEquals("The balance is not correct", 572.12, a.giveMoney(1000, "3212"));
            } catch (WrongNumberAccountException | AccountAbsentException ex) {
                fail("Belgian Account Correct");
            } catch (IllegalAmountException ex){
                fail("Amount Correct");
            }catch (WrongPinCodeException ex){
                fail("PinCode correct");
            }
        }
    }

    Et la classe qui doit être tester :

    public class Account {
        
        private String AccountNumber ="BE87121212121295";
        private int pinCode ;
        private int amountOfAccount = 0;
        
        public Account(String AccountNumber) throws WrongNumberAccountException, AccountAbsentException{
                String p = "BE";
                Pattern pattern = Pattern.compile("[^A-Za-z0-9]");
                Matcher match = pattern.matcher(AccountNumber);
                boolean val = match.find();
                
                if(val == true){
                    throw new WrongNumberAccountException("Your account number must composed only by digit and letter");
                }
                if(!AccountNumber.substring(0,2).equals(p)){ 
                    throw new WrongNumberAccountException("Your account number wrong, international check code not correct");
                }
                if("87".equals(AccountNumber.substring(2,4))){ 
                    throw new WrongNumberAccountException("Your account number wrong, Belgian check code not correct");
                }
                if(!AccountNumber.equals(this.AccountNumber)){ 
                    throw new WrongNumberAccountException("Your account number wrong");
                }
                if(!testLenghtAccount(AccountNumber)){
                     throw new WrongNumberAccountException("Your account number must be between 14 and 34 characters composed only by digit and letter");
                }if(AccountNumber.length() >= 14 || AccountNumber.length() <= 34){ 
                    throw new WrongNumberAccountException("Your account number must be between 14 and 34 characters composed only by digit and letter");
                }
        }
          
        private boolean checkPinCode(int pinCode){
            if(this.pinCode == pinCode){
                return true ;
            }else{
                return false;
            }
        }
        
        public double giveMoney(int amount, String pinCode) throws IllegalAmountException, WrongPinCodeException{
            if(amount < 0 ){
                throw new IllegalAmountException("You cannot give negative amount");
            }
            if(amount > 2000 ){
                throw new IllegalAmountException("You cannot give negative amount");
            }
            return 0;
        }
    
        public double getAmount(){
            return this.amountOfAccount;
        }
        
        public String getAccountNumber() {
            return AccountNumber;
        }
        
        
        public boolean testLenghtAccount(String AccountNumber){
                if(AccountNumber.length()<14 || AccountNumber.length()>34){ 
                    return false;
                }
                else{
                    return true;
                }
        }
        
    }
    

    Merci

    • Partager sur Facebook
    • Partager sur Twitter

    Tester des classes ?

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown