Results 1 to 4 of 4
  1. #1
    sandy is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Post How to add User Defined Exception for JUnit (expected) tag

    I just want to know how to add userdefined exception for Test method of Junit.

    for instance, we put exception as

    @Test (expected = NullPointerException.class)
    public void testMethod()
    {

    }

    instead of that, can we specify our new user defined exception like,

    @Test (expected = MyException.class)
    public void testMethod()
    {

    }

    where MyException.java includes :

    public class MyException extends NullPointerException
    {

    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: How to add User Defined Exception for JUnit (expected) tag

    What happened when you tried it?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    sandy is offline Member
    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Default Re: How to add User Defined Exception for JUnit (expected) tag

    Quote Originally Posted by jim829 View Post
    What happened when you tried it?

    Regards,
    Jim
    Sorry, I just found a way out. Actually my Test method was returning "DataIntegrityViolationException" and I was catching "RuntimeException".
    there was a Expected Exception.

  4. #4
    Join Date
    Oct 2008
    Posts
    73
    Rep Power
    0

    Default Re: How to add User Defined Exception for JUnit (expected) tag

    You have found you way but in General..

    This code should be there in Junit Test method:

    try {
    callMethodToTest();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    if(e.getCause() instanceof DataIntegrityViolationException){
    retflag = true;
    }
    }
    Rakesh Mehta

Similar Threads

  1. Set Return Value on User Defined Type
    By Jdsfighter in forum New To Java
    Replies: 3
    Last Post: 11-12-2012, 05:36 AM
  2. Set of user defined class
    By TelliameD in forum New To Java
    Replies: 4
    Last Post: 07-05-2012, 09:58 AM
  3. Problem with user defined class
    By moosethmucha in forum New To Java
    Replies: 8
    Last Post: 04-17-2012, 05:58 PM
  4. User Defined Method
    By overcranked in forum New To Java
    Replies: 6
    Last Post: 04-09-2010, 01:02 AM
  5. Replies: 2
    Last Post: 12-31-2009, 12:45 PM

Posting Permissions

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