jeudi 23 novembre 2017

Regex resulting in additional null field

I'm working on a java method that's supposed to check a string for whitespace between a character and a left parenthesis and secondly check the contents within the parathesis is a value of (blue)||(red)||(yellow).

 Matcher m = Pattern.compile("\\w\\s\\(").matcher(desc.toLowerCase());
 Matcher m2 = Pattern.compile("\\(blue\\)$|\\(red\\)$|\\(yellow\\)$").matcher(desc.toLowerCase());

    if (m.find() && m2.find())
    {
        this.pallet.setDesc(desc);
    }
    else
    {
        throw new IllegalArgumentException();
    }
}

public void testDescription()
{
    WidgetPallet wp = new WidgetPallet();
    WidgetLot wl = new WidgetLot(wp);
    wl.setDescription("A Widget (red)");
    assertEquals("A Widget (red)", wl.getDescription());
}

However, my junit tests result in: expected:A Widget (red)[] but was:A Widget (red)[ null]

Just curious on what I'm missing here. Thanks!

Aucun commentaire:

Enregistrer un commentaire