Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

I have a Springframework integrated Junit test. I had seen annotating a Junit test class with @Test(expected = Exception.class) to test for exceptions before. In this case, we needed to test for Exception and the message was made the difference. This can be accomplished by @Rule

    public ExpectedException expectedEx = ExpectedException.none();

    @Test(expected = Exception.class)
    public void testAddNegativeTransaction() throws Exception {
        expectedEx.expectMessage("Cannot withdraw more than available balance.");
        transactionService.addNegativeTransaction(new TransactionRequest("1", "acw", 10, 2000.00));

February 5th, 2014

Posted In: Integration Tests, Java, JUnit, Test Driven Development

Tags: , , , , , , , , , ,

Leave a Comment

LinkedIn Auto Publish Powered By :