Results 1 to 13 of 13
Thread: problem with JUnit test
- 12-19-2011, 12:37 AM #1
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
problem with JUnit test
Hi, i have class to generate random chars(a-f)
here is source:
This class worl for 100% i think...but test pass it only in 50% of cases...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(); } } }
here is test source:
WHY????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"); } }Last edited by Norm; 12-19-2011 at 01:20 AM. Reason: Tags changed to code
- 12-19-2011, 01:22 AM #2
Re: problem with JUnit test
What does the testing software require? What are the 50% of cases that the code fails?
- 12-19-2011, 12:19 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: problem with JUnit test
Which test fails, and why?
- 12-19-2011, 12:44 PM #4
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: problem with JUnit test
What are the possible values of this?
getThisElement = pseudoNumber.nextInt(generate.size());
- 12-19-2011, 02:15 PM #5
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
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.
- 12-19-2011, 02:39 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 12-19-2011, 02:49 PM #7
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
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 :(((
- 12-19-2011, 04:12 PM #8
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: problem with JUnit test
getThisElement = pseudoNumber.nextInt(generate.size());
is selecting numbers from 0 to hex F, not A to F
- 12-19-2011, 04:55 PM #9
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
- 12-19-2011, 06:44 PM #10
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
- 12-19-2011, 07:20 PM #11
Member
- Join Date
- Dec 2011
- Posts
- 7
- Rep Power
- 0
- 12-19-2011, 07:22 PM #12
Re: problem with JUnit test
That is often the way it is until you find out why.I dont know why
Printing out details as the code executes will often show you what is happening.
- 12-19-2011, 07:49 PM #13
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Similar Threads
-
JUnit test - AssertEquals
By dellacpa in forum New To JavaReplies: 2Last Post: 11-21-2010, 09:19 PM -
junit test problem
By moamen in forum EclipseReplies: 2Last Post: 03-14-2010, 09:41 PM -
JUnit Test??? What is it all about????? Please help!!
By nikosa in forum New To JavaReplies: 1Last Post: 08-03-2009, 05:31 PM -
JUnit Test Help!
By pharo in forum New To JavaReplies: 0Last Post: 04-10-2009, 05:15 PM -
Junit test
By alice in forum New To JavaReplies: 1Last Post: 06-14-2008, 01:24 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks