Results 1 to 13 of 13
  1. #1
    exltus is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default problem with JUnit test

    Hi, i have class to generate random chars(a-f)
    here is source:
    Java Code:
    package kodovac_rc1;
    
    
    
    import java.util.ArrayList;
    import java.util.Random;
     
        /**
        Třída slouží pro vygenerování neopakujících se hodnot a přepočítání do šestnáctkové soustavy
        */ 
    
    public class randomGenerator {
         
        private static ArrayList<Integer> generate;
        private static final int INITIAL_SIZE = 16;
        private static String key = "";
     
        /**
        Metoda pro předávání hodnoty private proměnné jiné třídě
        */ 
        public static String vratKlic()
        {
            return key;
        }
        
        /**
        Metoda pro vytvoření generátoru
        */ 
        public static void randomGenerator() {
            generate = new ArrayList<Integer>(INITIAL_SIZE);
            fillList();
        }
         
        /**
        Metoda pro naplnění array čísli 10-16
        */    
        private static void fillList() {
            for (int i=10; i<INITIAL_SIZE; i++)
                generate.add(i);
        }
     
        /**
        Metoda pro vybrání čísla a převod do šestnáctkové soustavy
        */          
        public static void printRandomInt() {
            randomGenerator();
            Random pseudoNumber = new Random();
            int getThisElement;
            key = "";
             
            for (int i=10; i<INITIAL_SIZE; i++) {
                getThisElement = pseudoNumber.nextInt(generate.size());
                String c =Integer.toString(generate.remove(getThisElement), 16);
                key = key + c;
            }
        if(key.length()!=6)
        {
        printRandomInt(); 
        }
        }
    }
    This class worl for 100% i think...but test pass it only in 50% of cases...
    here is test source:
    Java Code:
    /*
    Testy třídy randomGenerator
     */
    package kodovac_rc1;
    
    import org.junit.AfterClass;
    import org.junit.BeforeClass;
    import org.junit.Test;
    import static org.junit.Assert.*;
    
    /**
     *
     * @author Radek Mikulka
     */
    public class randomGeneratorTest {
        
        public randomGeneratorTest() {
        }
    
        @BeforeClass
        public static void setUpClass() throws Exception {
        }
    
        @AfterClass
        public static void tearDownClass() throws Exception {
        }
    
        /**
         * Test metody vratKlic třídy randomGenerator.
         */
        @Test
        public void testVratKlic() {
            System.out.println("vratKlic");
            String expResult = "[a-f][a-f][a-f][a-f][a-f][a-f]";
            String result = randomGenerator.vratKlic();
            if(result.matches(expResult))
            {
            System.out.print("pass");    
            }else
            fail("Výstup neodpovídá regulárnímu výrazu. Někdy test neprojde i když metoda funguje na 100% správně.");
        } 
    
        /**
         * Test metody testPrintRandomInt třídy randomGenerator.
         */
        @Test
        public void testPrintRandomInt() {
            System.out.println("printRandomInt");
            randomGenerator instance = new randomGenerator();
            randomGenerator.printRandomInt();
            System.out.println("Metoda musí proběhnout");
        }
    }
    WHY????
    Last edited by Norm; 12-19-2011 at 02:20 AM. Reason: Tags changed to code

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: problem with JUnit test

    What does the testing software require? What are the 50% of cases that the code fails?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: problem with JUnit test

    Which test fails, and why?

  4. #4
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: problem with JUnit test

    What are the possible values of this?

    getThisElement = pseudoNumber.nextInt(generate.size());

  5. #5
    exltus is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: problem with JUnit test

    Output is string with lenght = 6. Is created by 6 random nonrecurring chars connected together to one string. posslible values can be for example : abcdef, bfdace,efbcad,....... My problem is, that Test public void testVratKlic() fail sometime. For example: when I start tests, then testVratKlic() pass, i try it next time and void pass again, but when i try it several times the test will fail some times... Test is testing only if output of class matches to reguar expression. Thats why Iam afraid that my class does not work correctly. Sorry for my bad eng.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: problem with JUnit test

    Would it help if you actually printed out the value of "result" on a failure?
    Then you might actually see what it is that's being failed...at the moment you;re simply guessing.

  7. #7
    exltus is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: problem with JUnit test

    problem is, that result is string.matech([a-f][a-f][a-f][a-f][a-f][a-f]) most of time...but sometimes imput value for test is empy...I dont know why :(((

  8. #8
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: problem with JUnit test

    getThisElement = pseudoNumber.nextInt(generate.size());

    is selecting numbers from 0 to hex F, not A to F

  9. #9
    exltus is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: problem with JUnit test

    Quote Originally Posted by 2by4 View Post
    getThisElement = pseudoNumber.nextInt(generate.size());

    is selecting numbers from 0 to hex F, not A to F
    Y but "generate" contins only numbers 10-15 and it is a-f in hex

  10. #10
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: problem with JUnit test

    Quote Originally Posted by exltus View Post
    Y but "generate" contins only numbers 10-15 and it is a-f in hex
    ok I see. I agree with Tolls. Why not print out result?

  11. #11
    exltus is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: problem with JUnit test

    Quote Originally Posted by 2by4 View Post
    ok I see. I agree with Tolls. Why not print out result?
    This is problem...I dont know why...And i dont see any error :(

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: problem with JUnit test

    I dont know why
    That is often the way it is until you find out why.
    Printing out details as the code executes will often show you what is happening.

  13. #13
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: problem with JUnit test

    I think it may be because you are using static methods to initialize your data.

    Is JUnit reloading your class between each test? What is the garbage collector doing???

    Have you tried calling randomGenerator() in your test, just before you call vratKlic()?

Similar Threads

  1. JUnit test - AssertEquals
    By dellacpa in forum New To Java
    Replies: 2
    Last Post: 11-21-2010, 10:19 PM
  2. junit test problem
    By moamen in forum Eclipse
    Replies: 2
    Last Post: 03-14-2010, 10:41 PM
  3. Replies: 1
    Last Post: 08-03-2009, 06:31 PM
  4. JUnit Test Help!
    By pharo in forum New To Java
    Replies: 0
    Last Post: 04-10-2009, 06:15 PM
  5. Junit test
    By alice in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 02:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •