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!
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.
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!
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.
QtFalcon (August 3rd, 2014)
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"); } }
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 ); } // ...