Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This test is a junit test. My thought is position A, since we\'re not supposed t

ID: 3719350 • Letter: T

Question


This test is a junit test. My thought is position A, since we're not supposed to fail the test right? If put in position A, fail() would not be reached because an exception is thrown before reaching it, and the junit test passes. This is what we want, right?

Otherwise, if the answer is position B (or something else), please specify and explain why. Thank you so much!!!

Another method,?jumpToLightSpeed. takes in a coordinate as parameter for the destination, and throws a HyperDriveOfflineException' if hyperdrive appears to be offline. public void testJumpToLightSpeed (Coordinate destination) // construct a new Death Star DeathStar ds - new DeathStar) // turn off hyperdrive ds.hyperDrive.off); // destination: Planet Alderaan Coordinate alderaanLocationUtil.getCoordinate("Alderaan") // trying to jump to light speed should throw an exception try ds.jumpToLightSpeed(alderaan); /I Position A fail); catch (HyperDri veOfflineException e) System.err.println(e) /1 Position B fail) ) finally -t I Position C fail) Choose the best place(s) to add the 'fail)' statement: (1 point)

Explanation / Answer

Yes, you are absolutely correct. The correct place to check the fail() condition is Position A.

So, As we are explicitly making the hyderdrive off, it means, we want the exception to be thrown. And if exception is not thrown in method jumpToLightSpeed, then it is an error.

So If we places the fail statement in Position B, Then that will for sure be hit, because once exception is raised in jumpToLightSpeed method, then execution will directly reach to catch block and will not continue any further in try block. Hence Putting at position B, will cause the test to fail, where a exception is expected to occur.

Similarly, even if exception occurs or not, which means no matter catch block is hit or not, Finally will always be executed.. so even when Exception is expected, Putting fail() condition in Point C, will make it to fail.

Hence we should keep fail() at position A only, If the exception occurs in jumpToLightSpeed method, execution will directly go to catch block and the next statement, which is fail() statement inside try block will not be hit. This is expected.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote