Junit test provided, but this question won\'t take you more than 15 mins. You ne
ID: 3891065 • Letter: J
Question
Junit test provided, but this question won't take you more than 15 mins.
You need to write 2 classes in the edu.buffalo.cse116 package:
MightBeFixableException must be a checked exception. It needs a constructor. The constructor should have a single String parameter. Your constructor should chain to the superclass constructor using the command super(parameterName); (whereparameterName is replaced with the constructor parameter's name) to set the exception's message.
FixesNotExpectedException must be an unchecked exception. It must define a single constructor that does not have any parameters. The constructor must start with the command super("TIFU: This exception was raised."); to set the exception's message.
--------------------------------------------------------------------
Class that already provided:
package edu.buffalo.cse116;
/**
* Class used to generate exceptions for the weekly assignment problem.
*
* @author Matthew Hertz
*/
public class RaiseException {
/**
* Method used to test students ability to write and pass on errors.
*
* @param errorNum Identifier of what type of error to be generated. Generate a FixItNowException if this is less than
* 0; generate a YouAreHosedException if this is greater than 0; and return the answer to life, the universe,
* and everything if this equals 0.
* @return The answer to life, the universe, and everything according to _Hitchhiker's Guide to the Galaxy_
* @throws MightBeFixableException Exception raised when errorNum is less than 0.
*/
public static int simpleMethodRaisingException(int errorNum) throws MightBeFixableException {
if (errorNum < 0) {
throw new MightBeFixableException("I just learned this; Exception is checked; I will raise it; Fix bug, maybe?");
} else if (errorNum > 0) {
throw new FixesNotExpectedException();
} else {
return 42;
}
}
}
----------------------------------------------------
package edu.buffalo.cse116;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.Test;
/**
* Test cases for your classes defining important methods.
*
* @author Matthew Hertz
*/
public class RaiseExceptionTest {
@Test
public void testProbablyUnfixableExceptionClassDefinition() {
try {
RaiseException.simpleMethodRaisingException(2);
fail("Should have thrown an exception when makeException(2) called!");
} catch (Exception e) {
assertTrue("When makeException(2) called, you should be throwing the exception that DOES NOT require being listed in a throws clause!",
e instanceof FixesNotExpectedException);
assertTrue("When makeException(2) called, you should be throwing the exception that DOES NOT require being listed in a throws clause!",
e instanceof RuntimeException);
assertEquals("Incorrect message set in ProbablyUnfixableException constructor",
"TIFU: This exception was raised.", e.getMessage());
}
try {
int answer = RaiseException.simpleMethodRaisingException(0);
assertEquals(42, answer);
} catch (MightBeFixableException e) {
fail("When makeException(0) called, your method should return 42!");
}
}
@Test
public void testprobablyUnfixableExceptionClassMembers() {
Field[] hosedFields = FixesNotExpectedException.class.getDeclaredFields();
assertEquals("ProbablyUnfixableException should not declare any fields!", 0, hosedFields.length);
Method[] hosedMethods = FixesNotExpectedException.class.getDeclaredMethods();
assertEquals("ProbablyUnfixableException should not define any methods.", 0, hosedMethods.length);
Constructor<?>[] hosedConstructors = FixesNotExpectedException.class.getConstructors();
assertEquals("ProbablyUnfixableException should only define exactly 1 constructor.", 1, hosedConstructors.length);
}
@Test
public void testFixItExceptionClassDefinition() {
try {
RaiseException.simpleMethodRaisingException(-4982);
fail("Should have thrown an exception when makeException(-4982) called!");
} catch (Exception e) {
assertTrue("When makeException(-4982) called, you should be throwing the exception that DOES require being listed in a throws clause!",
e instanceof MightBeFixableException);
assertTrue("When makeException(-4982) called, you should be throwing the exception that DOES require being listed in a throws clause!",
e instanceof Exception);
assertFalse("When makeException(-4982) called, you should be throwing the exception that DOES require being listed in a throws clause!",
e instanceof RuntimeException);
assertEquals("Incorrect message set in FixItNowException constructor",
"I just learned this; Exception is checked; I will raise it; Fix bug, maybe?", e.getMessage());
}
try {
int answer = RaiseException.simpleMethodRaisingException(0);
assertEquals(42, answer);
} catch (MightBeFixableException e) {
fail("When makeException(0) called, your method should return 42!");
}
}
@Test
public void testFixItExceptionClassMembers() {
Field[] fixItFields = MightBeFixableException.class.getDeclaredFields();
assertEquals("FixItNowException should not declare any fields!", 0, fixItFields.length);
Method[] fixItMethods = MightBeFixableException.class.getDeclaredMethods();
assertEquals("FixItNowException should not define any methods.", 0, fixItMethods.length);
Constructor<?>[] fixItConstructors = MightBeFixableException.class.getConstructors();
assertEquals("FixItNowException should only define exactly 1 constructor.", 1, fixItConstructors.length);
}
}
Explanation / Answer
public class MightBeFixableException {
public void MightBeFixableException(String exp)
{
super(exp);
}
}
public class FixesNotExpectedException {
public void FixesNotExpectedException (String exp)
{
super("TIFU: This exception was raised.");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.