Antony Securo
Antony Securo

Reputation: 41

Java JUnit test does not pass

I have a method that has to return true if the name is matched by the regular expression or null if the name has special characters or numbers.

This is the method:

@SuppressWarnings("null")
public boolean containsSpecialCharacters(String text) {
    Pattern p = Pattern.compile("/^[a-zA-Z\\s]+$/");
    //check if the name has special characters
    Matcher m = p.matcher(text);
    boolean b = m.find();
    //cast the null to boolean
    Boolean boolean1 = (Boolean) null;
    if (m.matches()) {
        return true;
    }
    else {
        return boolean1;
    }    
}

And this is the test for the method that cannot pass:

@Test
public void parseBeanCheck() throws NumberFormatException, IOException, SAXException, IntrospectionException {

    IngenicoForwardingHelper helper = new IngenicoForwardingHelper();

    String test1 = "Steve Jobs";
    Assert.assertTrue(helper.containsSpecialCharacters(test1));
    //This should return Null 
    String test2 = "Steve Jobs1";
    Assert.assertNull(helper.containsSpecialCharacters(test2));
    //This should return Null 
    String test3 = "Steve Jöbs";
    Assert.assertNull(helper.containsSpecialCharacters(test3));
}

Upvotes: 0

Views: 190

Answers (2)

ᴇʟᴇvᴀтᴇ
ᴇʟᴇvᴀтᴇ

Reputation: 12781

Your method returns a boolean which is a primitive type that only allows values true and false. It does not allow null, so your tests that assertNull() will never work!

You could change the method signature to return a Boolean instead, but it's generally better to avoid returning null from methods, if possible. Returning true or false would make more sense than true or null anyway.

In Java, your regular expression doesn't need (and shouldn't have) the slashes at the start and end.

You can change your code to something like this:

public boolean containsSpecialCharacters(String text) {
    Pattern p = Pattern.compile("^[a-zA-Z\\s]+$");
    Matcher m = p.matcher(text);
    return !m.matches();
}

Or even more simply:

public boolean containsSpecialCharacters(String text) {
    return !text.matches("[a-zA-Z\\s]+");
}

And the test to something like this:

@Test
public void parseBeanCheck() throws NumberFormatException, IOException, SAXException, IntrospectionException {

    IngenicoForwardingHelper helper = new IngenicoForwardingHelper();
    Assert.assertFalse(helper.containsSpecialCharacters("Steve Jobs"));
    Assert.assertTrue(helper.containsSpecialCharacters("Steve Jobs1"));
    Assert.assertTrue(helper.containsSpecialCharacters("Steve Jöbs"));
}    

It's also worth mentioning that \s will match not only spaces, but also tabs, newlines, carriage-returns etc. So make sure that's what you want.

Upvotes: 4

Viktor Mellgren
Viktor Mellgren

Reputation: 4506

You should probably simplify your special character check to something like:

public boolean containsSpecialCharacters(String text) {
    Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
    Matcher m = p.matcher(text);
    return m.find();
}

And use Assert.assertTrue and Assert.assertFalse for your tests

Upvotes: -1

Related Questions