Test 094, IBM WID 6.0 cert
SCJP 1.2
SCBCD 1.3 Beta
SCWCD 1.4 Beta
SCMAD Beta
SCDJWS Beta
KS
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by timothy zimmerman:
The concern that I would have with the previous posts is that you are catching exceptions and assuming that they were caused by the invalid input and that your test has passed (meaning it should have thrown an exception). But, what if something else caused that exception to be thrown?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Tavia Young:
With regards to testing for null values that are passed into a method, would the logical step be to write a test to initially catch the NullPointerException (i.e. as Craig mentions above) and then test for the case in which such parameters are not null? How would these two tests be able to co-exist in the TestCase?
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Originally posted by Tavia Young:
But, what happens in the case if the Exception you're looking for is nested?
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ilja Preuss:
Than you catch the outer exception and in the catch block assert that it contains the expected nested exception.
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Originally posted by Tavia Young:
If the testInvalidInput() passes, this would mean that the code does not check for invalid input?! My confusion is that if I alter the code to check for null values, this test would fail.
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Originally posted by Tavia Young:
By testing for the specific exception, (e.g. a NullPointerException in the case where a parameter passed to a method results in a null) we are verifying the code does throw this exception. If code were written to include a check (e.g. parameter != null), this would lead to a fail. So, would it be correct to then check for the expected exception thrown when the parameter is not null?
Originally posted by Tavia Young:
I am testing this within a J2EE environment and I also get an error from the EJB even though the unit tests pass?
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Originally posted by Tavia Young:
The examples are helpful, Lasse. The confusing bit for me still is if the code included a line such as if(param != null), the test fails.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |