字符串条件检查未通过测试

时间:2015-06-04 08:56:56

标签: java string junit

我有一种验证石斑鱼串的方法:

@Test
public void testValidateGrouper(){
    DBUtils dbUtils = new DBUtils();
    assertEquals("Expect empty string to be replaced by AND","AND",dbUtils.validateGrouper(""));
    assertEquals("Expect spaced out string to be replaced by AND","AND",dbUtils.validateGrouper("   "));
    assertEquals("Expect the lower case and to be used as AND","AND",dbUtils.validateGrouper("and"));
    assertEquals("Expect the upper case AND to be used as AND","AND",dbUtils.validateGrouper("AND"));
    assertEquals("Expect the word CART to be ignored and replaced by AND","AND",dbUtils.validateGrouper("CART"));
    assertEquals("Expect AND to be used as the grouper if no grouper is provided","AND",dbUtils.validateGrouper(null));
    assertEquals("Expect the lower case or to be used as OR","OR",dbUtils.validateGrouper("or"));
    assertEquals("Expect the upper case OR to be used as OR","OR",dbUtils.validateGrouper("OR"));
}

我正在针对此测试运行它:

AND

我发现尽管使用ORusers [id | name | type] types [id | title] ,但始终会触发条件。我不明白为什么情况会失败。

1 个答案:

答案 0 :(得分:1)

如果允许使用AND或OR,则需要

if(grouper==null || !(grouper.equalsIgnoreCase("AND") || grouper.equalsIgnoreCase("OR")))

否则,如果石斑鱼是“或”,则它不等于“AND”且条件为真,如果石斑鱼为“AND”,则它不等于“OR”且条件为真。

此外,您可以删除grouper.equals("")支票。

相关问题