I am currently attempting to extend test coverage for a school-based-assignment web application after I came across a completely empty class that extends Exception and contains no coverage according to the coverage report.
Normally, I would assume that something like this could be disregarded. However, our overall test coverage factor directly into our individual grading for this course. Because of this and it being near the end of the semester, I am trying to go through the code flagged within the coverage report line-by-line and tidy up anything I can.
After going through course content and searches online, I am unsure of how to proceed with writing a test for a class such as this. This server-side class was included in our initial code base that was given to us at the start of the semester by the instructor (we build onto the code base as the semester progresses).
The entire code for the Java class:
package <package_name>;
/*
* This is a custom exception that fits our personal
* needs and won't collide with existing issues.
*/
public class BadRequestException extends Exception {}
One example of how the class is used (still code that was provided by instructor):
private String processHttpRequest(spark.Request httpRequest, spark.Response httpResponse, Type requestType) {
setupResponse(httpResponse);
String jsonString = httpRequest.body();
try {
JSONValidator.validate(jsonString, requestType);
Request requestObj = new Gson().fromJson(jsonString, requestType);
return buildJSONResponse(requestObj);
} catch (IOException | BadRequestException e) { // <---- Here
log.info("Bad Request - {}", e.getMessage()); // <----
httpResponse.status(HTTP_BAD_REQUEST);
} catch (Exception e) {
log.info("Server Error - ", e);
httpResponse.status(HTTP_SERVER_ERROR);
}
return jsonString;
}
What I have so far (practically nothing):
package <package_name>;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
public class TestBadRequestException {
@Test
@DisplayName("Test Empty BadRequestException Class")
public void testBadRequestException() {
}
}
Prior to the start of the semester, I had no experience with JUnit. So, any feedback/references/recommendations are greatly appreciated.
EDIT (Solution):
The first comment on this post provided the solution I was looking for. I had not occurred to me that it would be this simple.
The solution is in the answer below with proper credit.
CodePudding user response:
Solution credit to @f1sh
f1sh's comment (original solution) recommended to add new BadRequestException()
within the JUnit test.
This gave an idea on how I could hopefully improve upon that recommendation.
My final result:
package <package_name>;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class TestBadRequestException {
@Test
@DisplayName("Test Empty BadRequestException Class")
public void testBadRequestException() {
assertNotNull(new BadRequestException()); // <---- Here
}
}
CodePudding user response:
I think write a unit test for that class is unnecessary. You can try exclude that on your code coverage rules