tags:

views:

66

answers:

7

The following code does not compile, becouse of unreachable catch block.

I want write "tues" function and call it in many other unit test functions.

Is that possible and how to implement that?

private void catchException(boolean condition) {
        try
        {
            assertTrue(condition);
        }
        catch (SomeException e)
        {
            fail("exception = " + e.getMessage());
        }
}

Thanks!

A: 

Modifying SomeException by Exception will cause the code to compile.

Manuel Selva
+2  A: 

The problem with your code is that assertTrue does not throw SomeException, if you substitute it with the function that does, it will compile.

Alexander Pogrebnyak
+2  A: 

I'm not entirely sure what you're asking here, but there are a couple things wrong with the code you posted.

You can only have a catch block for a checked exception if the code in the try can potentially throw an exception of the same type you are catching. Since checked exceptions must be declared in the throws clause of a method, you can check to see if any of the methods you are calling throw the exception types you are catching. From the JUnit documentation on assertTrue:

public static void assertTrue(boolean condition)

You can see it doesn't throw any checked exceptions.

Also, calling fail() in a catch block in a JUnit test is not really necessary. A unit test will automatically fail if an uncaught exception is thrown.

Tom Tresansky
Thank you for your answer.I know that I should wrote checked exception int catch block and that is reasone why that function cant work.I call fail() becouse if I don't do that, unit test pass.
@coban22, no, uncaught exceptions will cause a JUnit test to fail: http://junit.sourceforge.net/doc/faq/faq.htm#tests_8 just add it to the `throws` clause of the test method so your code compiles.
matt b
+1  A: 

Just add throws WhateverException to your test signature and the test will fail if an exception is thrown.

tob
+3  A: 

There is zero need to catch an exception within a test method to fail it:

public Object methodUnderTest() throws SomeException {
    ...
}

@Test
public void testMethod() throws SomeException() {
      Object obj = methodUnderTest();
      assert...
}

If SomeException is thrown by methodUnderTest(), testMethod() will fail.

matt b
+1  A: 

It is actually not really clear what you what to achieve with your code. If you want to have a nicely formatted message in the exception fired by jUnit if your assert fails then consider writing it in this way:

assertTrue("Condition should hold because....", conditionToCheck);

In this way jUnit will print the message you provided if the check fails. I highly recommend this, especially if you have lots of tests because it

  • helps you to quickly identify the problem
  • helps your team member to understand the purpose of your assert
Juri
A: 

Obviously my question is not clear.

I have many unit tests and all methods are throwing same exception with different error message. "SomeException" is exception I must catch and read error message from it.

What I want to achive is to write one method which will be common to all unit tests and where I could print error message.

Now unit tests looks like this

 public void test_something()
    {     
        try
        {
            assertTrue(bw.doSomething("test"));
        }
        catch (SomeException e)
        {
            fail("exception = " + e.getReason());
        }
    }

public void test_something1()
    {
        IBroadworks bw = getSomehting1();
        try
        {
            assertTrue(bw.doSomething1("test1"));
        }
        catch (SomeException e)
        {
            fail("exception = " + e.getReason());
        }
    }
...

So below code is repeating in all unit tests and that is not ok.

...
try{
assertTrue(condition);
}

catch (SomeException e)
        {
            fail("exception = " + e.getReason());
        }
...