-1

I want to expect the target method will cause an Exception, but the target method has a try-catch block to deal with this exception, so I can't get the Exception in my test method.

kryger
  • 12,906
  • 8
  • 44
  • 65
crowngym
  • 41
  • 1
  • 8
  • possible duplicate of [How to test already handled exception using jUnit?](http://stackoverflow.com/questions/18944895/how-to-test-already-handled-exception-using-junit) – Stefan Birkner Jul 02 '15 at 06:58

3 Answers3

1

You have to test the target method and your junit must be according to it.if in your target method, exception is being caught and processed your junit test case must assert that in no condition target method ends up in exception.

Check what is being done in the catch block of the target method and write junit to check the proper functioning of code in catch block.

KDP
  • 1,481
  • 7
  • 13
1

Does the method return any values? If so you can assert on that. But this post explains your problem well:

How to test that no exception is thrown?

Community
  • 1
  • 1
YesR
  • 100
  • 1
  • 6
0

Why do you want to get the exception?
You should try to see unit testing as black-box testing. Establish some pre-conditions (setup the scenario) and then verify the post-conditions (check the actual outcome, e.g. return values, object state or mock interactions).

If you want to test that a certain type is being caught, it might be enough to see that the test simply passes even though an exception is thrown in the production code.

Jocke
  • 414
  • 3
  • 6