Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Test noExceptions

  1. #1
    Junior Member QtFalcon's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    12
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Test noExceptions

    Hi,

    I want to test that a function has no any exceptions.

    In C++ and Google Test Framework (GTest) it will look like this:
    ASSERT_NO_THROW({
                        actual = divide(a, b);
                    });

    How will it look in Java? I am new in jUnit. I love you! Thank you!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,306
    My Mood
    Mellow
    Thanks
    194
    Thanked 669 Times in 656 Posts

    Default Re: Test noExceptions

    I hadn't seen this question before, so I did a little (very little) research. I found the usual articles on using JUnit to test for exceptions, and then this StackOverflow topic on testing for the lack of them.

    Hope it helps.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    QtFalcon (August 3rd, 2014)

  4. #3
    Junior Member QtFalcon's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    12
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Test noExceptions

    Thanks!

    Is this normal?

        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testNormalTest() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmail.com";
     
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            try {
                instance.checkPersonFields( person );
            } catch ( Exception e ) {
                fail("Should not have thrown any exception");
            }
        }

  5. #4
    Junior Member QtFalcon's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    12
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Test noExceptions

    I see that it is normal

    Such I test methods with exceptions:
        @Rule
        public ExpectedException exception = ExpectedException.none();
     
        // ...
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorEmail03() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorEmail.class );
            instance.checkPersonFields( person );
        }
     
        // ...

Similar Threads

  1. How to test
    By keepStriving in forum Java Networking
    Replies: 2
    Last Post: June 10th, 2013, 10:15 AM