try {
G.insertEdge(u,v,1);
System.out.println(" Test 1 failed: Method insertEdge must throw an exception when");
System.out.println(" trying to access an invalid edge.")
u = G.getNode(5);
System.out.println(" Test 1 failed: Method getNode must throw an exception when");
System.out.println(" trying to access an invalid node.")
uv = G.getEdge(u,v);
System.out.println(" Test 1 failed: Method getEdge must throw an exception when");
System.out.println(" trying to access an invalid edge!")
neighbours = G.incidentEdges(v);
System.out.println(" Test 1 failed: Method incidentEdges must throw an exception when");
System.out.println(" trying to access an invalid node!")
boolean adjacent = G.areAdjacent(u,v);
System.out.println(" Test 1 failed: Method areAdjacent must throw an exception when");
System.out.println(" trying to access an invalid node!")
}
catch (GraphException e){
System.out.println(" Test 1 passed");
}
The success condition here is that all attempts throw an exception. So in the successful case, the exception is indeed thrown and nothing else is tested.
No. The success case is that everything in the try block throws. If the first one "passes" then you never test the others.
For example. getNode(5) should throw, but if insertEdge(u,v,1) throws first, we never test getNode(5). So maybe getNode(5) wouldn't have thrown an exception (and thus failed), but we never tested it and said "it passed" because insertEdge threw.
Not really, this will actually print "test passed" if any 1 of the functions throws the exception. And depending on which one throws the exception, it will print "failed" messages for everything above it, and then simply skip the rest of the function calls 😄
Indeed. As an example for stanusNat: in this case one might write assertThrows(GraphException.class, () -> { G.insertEdge(u, v, 1); });. Which is indeed a lot clearer in what it expects, and it's output when it fails, than catching the exception and printing a success 😃
I would also think about integrating this to automated environments. How do you include this into any automated test suites, or CI/CD pipelines, where you change something and want to make checking things automated. You can't because for testing it is usually recommended to use a test framework that is well integrated into many tools and have a lot of easy to read asserting methods.
14
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Aug 23 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
17
u/[deleted] Aug 23 '21
Can anyone explain what in particular is wrong with this?