Results 1 to 6 of 6
- 02-11-2013, 07:58 AM #1
Member
- Join Date
- Dec 2012
- Posts
- 22
- Rep Power
- 0
JUnit Test Help. 99% of the code working perfectly, only need help on 1%
Hi. So i am having trouble with J unit Tests. got 4 out 6 of the tests working correctly, with the last 2 being interlinked so if someone can guide me how to do 1, i can do the other one. Below is my code.
Main class being tested
Java Code:import java.util.ArrayList; public class PasswordChecker { static int length = 0; static int Digit = 0; static int Alpha = 0; static boolean Acceptable = false; public static boolean isValidPassword(String passwordString) throws lengthException, noDigitException, noAlphaException { if (passwordString.matches("[0-9]+")) throw new noAlphaException(); { Digit = 1; } if (passwordString.toCharArray().length < 6) throw new lengthException(); { length = 1; } if ((passwordString.matches("[A-Za-z]+"))) throw new noDigitException(); { Alpha = 1; } if ((Digit == 1) && (Alpha == 1) && (length == 1)) { Acceptable= true; } return Acceptable; } public static ArrayList<String> validPasswords(ArrayList<String> passwords) { boolean zeta = false; ArrayList<String> FaultyList = new ArrayList<String>(); String space = " "; for (int beta = 0; beta < passwords.size(); beta++) { try { zeta = isValidPassword(passwords.get(beta)); } catch (lengthException Fault1) { space = passwords.get(beta) + " The password must be at least 6 characters long"; FaultyList.add(space); } catch (noAlphaException Fault2) { space = passwords.get(beta) + " The password must contain at least one alphabetic character."; FaultyList.add(space); } catch (noDigitException Fault3) { space = passwords.get(beta) + " The password must contain at least one digit"; FaultyList.add(space); } if (zeta == true) { FaultyList.add(passwords.get(beta) + " This is a valid password."); } } return FaultyList; } public static boolean matchedPassword(String Entry1, String Entry2) throws unmatchedException { if (Entry1.equals(Entry2)) throw new unmatchedException(); { return true; } } }
Java Code:import static org.junit.Assert.*; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * Test the methods of PasswordChecker * @author Prabhdeep Singh * */ public class PasswordCheckerTest { ArrayList<String> passwords; String password1, password2; @Before public void setUp() throws Exception { String[] p = {"334455", "Im2cool", "george", "4sale", "bertha22", "4wardMarch", "august30", "abcdef", "apples", "aa11b", "pilotproject", "mypassword", "mypassword2"}; passwords = new ArrayList<String>(); passwords.addAll(Arrays.asList(p)); // puts strings into the ArrayList } @After public void tearDown() throws Exception { passwords = null; } /** * Test if the password is less than 6 characters long. * This test should throw a lengthException. */ @Test public void testIsValidPasswordTooShort() { try{ PasswordChecker.isValidPassword("abc12"); assertTrue("Did not throw lengthException",false); } catch(lengthException e) { assertTrue("Successfully threw a lengthExcepetion",true); } catch(Exception e) { assertTrue("Threw some other exception besides lengthException",false); } } /** * Test if the password has at least one alpha character * This test should throw a noAlphaException */ @Test public void testIsValidPasswordnoAlpha() { try{ PasswordChecker.isValidPassword("1234567"); assertTrue("Did not throw noAlphaException",false); } catch(noAlphaException e) { assertTrue("Successfully threw a noAlphaExcepetion",true); } catch(Exception e) { assertTrue("Threw some other exception besides noAlphaException",false); } } /** * Test if the password has at least one alpha character * This test should throw a noDigitException * TO BE IMPLEMENTED BY STUDENT */ @Test public void testIsValidPasswordNoDigit() { try { PasswordChecker.isValidPassword("Spartan"); assertTrue("Did not throw NoNumException", false); } catch (noDigitException e) { assertTrue("Successfully threw a noDigitExcepetion", true); } catch (Exception e) { assertTrue("Threw some other exception besides noAlphaException", false); } ; } /** * Test correct passwords * This test should not throw an exception * TO BE IMPLEMENTED BY STUDENT */ @Test public void testIsValidPasswordSuccessful() { try { if (PasswordChecker.isValidPassword("Spartan117") == true) { assertTrue(true); } } catch (lengthException Fault1) { // TODO Auto-generated catch block Fault1.printStackTrace(); } catch (noAlphaException Fault2) { // TODO Auto-generated catch block Fault2.printStackTrace(); } catch (noDigitException Fault3) { // TODO Auto-generated catch block Fault3.printStackTrace(); } } /** * Test the validPasswords method * Check the results of the ArrayList of Strings returned by the validPasswords method */ @Test public void testValidPasswords() { ArrayList<String> results; results = PasswordChecker.validPasswords(passwords); Scanner scan = new Scanner(results.get(0)); // assertEquals(scan.next(), "334455"); assertEquals(scan.nextLine(), " The password must contain at least one alphabetic character."); scan = new Scanner(results.get(3)); // assertEquals(scan.next(), "abcdef"); assertEquals(scan.nextLine(), " The password must contain at least one digit."); scan = new Scanner(results.get(5)); // assertEquals(scan.next(), "aa11b"); assertEquals(scan.nextLine(), " The password must be at least 6 characters long."); } /** * Test the validPasswords method * TO BE IMPLEMENTED BY STUDENT * Additional tests with different data than testValidPasswords() */ @Test public void testValidPasswordsStudent() { fail("Not implemented by student yet"); } }
- 02-11-2013, 01:16 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: JUnit Test Help. 99% of the code working perfectly, only need help on 1%
Stick some debugging into our code.
For example in isValidPassword, print out the strig being checked, and the path it went through.
Can't see how 'george' ends up in the 4th slot, though, even if you reject everything.
Ah!
Hang on.
Print out the value of 'zeta' as you go round the loop.
I think you'll find it getting stuck on 'true'.Please do not ask for code as refusal often offends.
** This space for rent **
- 02-11-2013, 03:53 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 22
- Rep Power
- 0
Re: JUnit Test Help. 99% of the code working perfectly, only need help on 1%
Yes, the testvalidpasswords keeps failing work, the ones before it work. the testvalidpasswords is where i am getting expecting george but got abcdef.
- 02-11-2013, 05:07 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: JUnit Test Help. 99% of the code working perfectly, only need help on 1%
Print out the value of 'zeta' as you loop over the array.
Does it ever get reset to false?
Indeed, similarly with 'Acceptable', though I would argue that is unecessary if you are going down the exception route.
The only time you would return a value from that method is if the password has passed. All failures result in an exception, so bypassing the normal return.Please do not ask for code as refusal often offends.
** This space for rent **
- 02-11-2013, 05:42 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 22
- Rep Power
- 0
Re: JUnit Test Help. 99% of the code working perfectly, only need help on 1%
The value of zeta should only be false if it doesnt match the required one of the required componenets, i dont think it should reset to false. So what you are telling me is to go to the passwordchecker class and use sysout to print out the value of zeta?
- 02-11-2013, 05:55 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: JUnit Test Help. 99% of the code working perfectly, only need help on 1%
Yes, so you can see exactly why it is going wrong.
Put it this way, what happens with the second password?
'zeta' gets set to 'true'.
Then when george goes through and fails, resulting in a failure entry in your results, 'zeta' is still true...so it then adds george in as a valid result as well, which is where it's going wrong.
Print out the results array and you'll see all failures after the second password will have two entires. One a failure and the other a success.Please do not ask for code as refusal often offends.
** This space for rent **
Similar Threads
-
JDBCrealm FORM based problem. But BASIC working perfectly alright!
By salman4u in forum New To JavaReplies: 0Last Post: 01-28-2010, 03:46 AM -
JUnit Test??? What is it all about????? Please help!!
By nikosa in forum New To JavaReplies: 1Last Post: 08-03-2009, 06:31 PM -
JUnit Test Help!
By pharo in forum New To JavaReplies: 0Last Post: 04-10-2009, 06:15 PM -
I am receiving an error while the code is perfectly compiling
By redasu in forum New To JavaReplies: 5Last Post: 11-09-2008, 02:58 PM -
Junit test
By alice in forum New To JavaReplies: 1Last Post: 06-14-2008, 02:24 AM
Bookmarks