Results 1 to 4 of 4
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default junit exception testing

    Hi, if im testing a method, and i delibrartely put in a value that it cannot store. it will throw an exception.

    In JUNIT testing how do I say assertTrue (if i put invalid value into method, Exeption)

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,020
    Rep Power
    6

    Default Re: junit exception testing

    I'm not quite certain I understand what you are asking. Do you want to test that a proper exception is being thrown? For example, the test would pass as green if division by zero threw an ArithmeticException.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: junit exception testing

    yes. I know like with AssertEquals you have to make sure the value and expected value is the same. Is there way to show that if an incorrect input of a method which threw an exception is properly thrown via Junit test?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: junit exception testing

    What version of JUnit are we talking about?
    JUnit 4 has:
    @Test(expected=<YourException>.class)

    4.7 has the ExpectedException structure, which is a bit more flexible, I believe.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Junit Testing within local classes
    By danboy4 in forum New To Java
    Replies: 3
    Last Post: 10-08-2012, 02:44 PM
  2. Junit testing, testing list<e> interface
    By mackavelirip in forum New To Java
    Replies: 0
    Last Post: 10-05-2011, 07:08 AM
  3. JUNIT testing assertEquals function
    By Drun in forum New To Java
    Replies: 7
    Last Post: 01-22-2010, 07:08 PM
  4. JUnit testing
    By hedonist in forum Advanced Java
    Replies: 4
    Last Post: 10-04-2009, 01:06 PM
  5. Testing JUnit PDE with Ant
    By keynan in forum Eclipse
    Replies: 0
    Last Post: 02-15-2008, 12:35 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •