检查Junit中的2个预期值

Swi*_*tle 3 java junit


我有一个java程序,它针对2种不同的场景抛出了2个不同消息的异常,我希望Junit测试用例检查这两个消息是否相等.举个例子 -

public void amethod() {
           // do some processing
        if(scenario1 == true) {
            throw new MySystemException("An error occured due to case 1 being incorrect.");
        }
        else if(scenario2 == true) {
            throw new MySystemException("An error occured as case 2 could not be found");
        }
    }  
Run Code Online (Sandbox Code Playgroud)

现在JUnit就像这样 -

public void testAMethod() {
    // do something
    assertEquals("Expected", "Actual");
}
Run Code Online (Sandbox Code Playgroud)

据我所知,在上面的例子中,如果我使用Scenario1异常消息,则在抛出异常时junit将失败,Scenario2反之亦然.
我想知道Junit中是否提供了其他方法,我可以使用这个方法test method并检查测试通过的两个消息?如果可能
的话OR,像这样的预期消息提供"预期"值.
我希望我的查询足够清楚.

谢谢

UPDATE

对于延迟回复感到抱歉,已经遇到了其他一些紧急事项.
谢谢大家提出的非常好的建议,它现在帮助我更好地理解了.
最后,为了保持相当简单,我决定实施Don Roby建议的类似解决方案.所以创建了一个新的测试类,看起来像 -

public void testAMethodScenario1() {
    // do the necessary
    assertEquals("Expected Exception Message 1", "Actual");
}

public void testAMethodScenario2() {
    // do the necessary
    assertEquals("Expected Exception Message 2", "Actual");
}  
Run Code Online (Sandbox Code Playgroud)

再次感谢大家的回复.

Cos*_*atu 5

我认为您需要手动捕获异常(针对每个方案)并单独检查消息:

try {
    // trigger scenario 1
    fail("An exception should have been thrown here !");
} catch (MySystemException e1) {
    assertEquals("Wrong error message", m1, e1.getMessage());
}

try {
    // trigger scenario 2
    fail("An exception should have been thrown here !");
} catch (MySystemException e2) {
    assertEquals("Wrong error message", m2, e2.getMessage());
}
Run Code Online (Sandbox Code Playgroud)

当然,您可以将这些场景定义为枚举常量,并简单地遍历它们并在循环内检查它们中的每一个,因为"复制/粘贴设计模式"在上面的代码中非常明显.:)