Help with Java Junit testing for some piece of code that you create with one or
ID: 3888024 • Letter: H
Question
Help with Java Junit testing
for some piece of code that you create with one or more faults: create five tests (with different inputs):
1. One passes whenever the fault is not reached. (!Rx)
2. One passes only if the fault is reached, but does not infect. (R)
3. One passes only if the fault is reached, infects, but does not propagate. (RI^P)
4. One passes only if the fault is reached, infects, and propagates – i.e., you detect a failure in the output. (RIPR)
5. One passes only if the fault is not reached and the result is correct.(!R no other failure)
Explanation / Answer
public class JUnitExample
{
public static int last (int[] arr, int key)
{
if(arr!=null){
for (int i=arr.length-1; i > 0; i--)
{
if (arr[i] == key)
{
return i;
}
}
}
return -1;
}
}
For the above method if we write the tests:
public class JUnitExampleTest
{
@test
public void testFaultIsNotReached()
{
assertTrue("Default value is expected!",JUnitExample.last(null,7)==-1);
}
@test
public void testFaultIsReachedButNotInfected()
{
assertTrue("Should not infect!",JUnitExample.last(new arr[]{6,7,8},7)==1);
}
@test
public void testFaultIsReachedAndInfectedNotPropagated()
{
assertTrue("Infected but should not propagate",JUnitExample.last(new arr[]{6,7,8},9)==-1);
}
@test
public void testFaultIsReachedInfectedAndPropagated()
{
assertTrue("Infected but should propagate",JUnitExample.last(new arr[]{6,7,8},6)==0);
}
@test
public void testFaultIsNotReachedAndResultIsCorrect()
{
assertTrue("Infected but should not propagate",JUnitExample.last(null,6)==-1);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.