Question: In preliminaryRound() you should call exceptionGenerator.runCourse(). If no exception is thrown, print out No exception thrown. If the call to runCourse() resulted in a checked
In preliminaryRound() you should call exceptionGenerator.runCourse(). If no exception is thrown, print out "No exception thrown". If the call to runCourse() resulted in a checked exception, print out "Caught checked exception". If runCourse() resulted in an unchecked exception, print out "Caught unchecked exception".
grandFinale() will consider what can happen when finding the ultimate winner. If the throwChecked parameter is true, your code should raise the checked exception If the throwUncheckedparameter is true instead, your code should raise an unchecked exception. If both parameters are false, nothing else needs to be done.
public class SpartanExceptionChallenge {
/** * Method which attempts to complete the preliminary by conquering the Qualifying Course. QualifyingCourse has a * single method: {@code runCourse()}. When this method is called, it could throw a checked exception * ({@code TimeException}), throw an unchecked exception ({@code RopeClimbException}), or not throw any exception. If * an exception is thrown, the method should catch the exception and print "Caught checked exception" or "Caught * unchecked exception" (as appropriate). If no exception is thrown, the method should print "No exception thrown" * * @param exceptionGenerator Class whose method, runCourse(), will be used to help test this Exception Warrior. */ public void preliminaryRound(QualifyingCourse exceptionGenerator) {
}
/** * Method to conquer the course at the grand finale -- throwing exceptions on your own. This has two parameters: * throwChecked and throwUnchecked. These determine which "flavor" of exception should be raised. When both * parameters are false, the method should not raise any exceptions. * * @param throwChecked When true, the method should raise a checked exception * @param throwUnchecked When true (and throwChecked is false), the method should raise an unchecked exception */ public void grandFinale(boolean throwChecked, boolean throwUnchecked) throws TimeException, RopeClimbException {
} }
public class RopeClimbException extends RuntimeException {
}
public class TimeException extends Exception {
}
import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream; import java.io.OutputStream; import java.io.PrintStream;
import org.junit.Before; import org.junit.Test;
public class SpartanExceptionChallengeTest {
private SpartanExceptionChallenge ninja;
@Before public void setUp() { ninja = new SpartanExceptionChallenge(); }
@Test public final void testPreliminaryRoundChecked() { try { OutputStream outStr = new ByteArrayOutputStream(); PrintStream sysOut = new PrintStream(outStr); System.setOut(sysOut); QualifyingCourse qc = new QualifyingCourse(2); ninja.preliminaryRound(qc); String response = outStr.toString(); response = response.trim(); assertEquals("preliminaryRound() must print out a mesage stating \"Caught checked exception\" when it catches the checked exception.", "Caught checked exception", response); } catch (Exception e) { fail("preliminaryRound() must catch the checked exception and print out a mesage stating \"Caught checked exception\". Your code threw the exception: " + e.toString()); } }
@Test public final void testCityFinalUnchecked() { try { OutputStream outStr = new ByteArrayOutputStream(); PrintStream sysOut = new PrintStream(outStr); System.setOut(sysOut); QualifyingCourse qc = new QualifyingCourse(1); ninja.preliminaryRound(qc); String response = outStr.toString(); response = response.trim(); assertEquals("preliminaryRound() must print out a mesage stating \"Caught unchecked exception\" when it catches the unchecked exception.", "Caught unchecked exception", response); } catch (Exception e) { fail("preliminaryRound() must catch the unchecked exception and print out a mesage stating \"Caught unchecked exception\". Your code threw the exception: " + e.toString()); } }
@Test public final void testPreliminaryRoundNoException() { try { OutputStream outStr = new ByteArrayOutputStream(); PrintStream sysOut = new PrintStream(outStr); System.setOut(sysOut); QualifyingCourse qc = new QualifyingCourse(0); ninja.preliminaryRound(qc); String response = outStr.toString(); response = response.trim(); assertEquals("preliminaryRound() must print out a mesage stating \"No exception thrown\" when no exception is thrown from runCourse()", "No exception thrown", response); } catch (Exception e) { fail("preliminaryRound() must print out a mesage stating \"No exception thrown\" when no exception is thrown. Your code threw the exception: " + e.toString()); } }
@Test public final void testGrandFinaleChecked() { try { ninja.grandFinale(true, false); fail("grandFinale() must throw (and not catch) the checked exception when the first parameter is true."); } catch (TimeException e) { } catch (Exception e) { fail("grandFinale() must throw (and not catch) a TimeException when the first parameter is true. Your code threw the exception: " + e.toString()); } }
@Test public final void testGrandFinaleUnchecked() { try { ninja.grandFinale(false, true); fail("grandFinale() must throw (and not catch) the unchecked exception when the second parameter is true."); } catch (RopeClimbException e) { } catch (Exception e) { fail("grandFinale() must throw (and not catch) a RopeClimbException when the second parameter is true. Your code threw the exception: " + e.toString()); } }
@Test public final void testGrandFinaleNone() { try { ninja.grandFinale(false, false); } catch (Exception e) { fail("grandFinale() should not be throwing any exceptions when neither parameter is true. Your code threw the exception: " + e.toString()); } } }
public class RopeClimbException extends RuntimeException {
}
public class TimeException extends Exception {
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
