Post on 04-Sep-2019
Black box & Junit
Ex 1
public float temperature(float powSupply, float clock, char cooler)
A Java method estimates a CPU temperature according to:
• power supply 1.5...1.8 (steps of 0.1 V)
• clock, between 2.0 e 3.4 GHz, (steps of 0.1 GHz)
• An uppercase char denoting the kind of cooler {‘A’,’ B’,’ C’ } (decreasing
cooling capability, e.g., 100, 50, 25 W)
The output is the temperature, ranging between 30.0 e 70.0 degrees.
If the clock is > 3.0 GHz it is mandatory to use cooler A or B
Design test cases according to the black box techniques, implementing the test in
Junit and designing the method stub
Identify equivalence classes
• Classes must me listed with an ID
• You can use for that:
– value intervals
– number of values
– set of values
– constraints
Value intervals
• VC1 Power supply [1.5,1.8]
• IC2 Power supply <1.5
• IC3 Power supply >1.8
• VC4 clock [2.0,3.5]
• IC5 clock <2.0
• IC6 clock >3.5
Set of values
• VC7 cooler = ‘A’
• VC8 cooler = ‘B’
• VC9 cooler = ‘C’
• IC10 cooler not in {‘A’,’B’,’C’}
Number of values
• VC11 3 inputs
• IC12 less than 3 inputs
• IC13 more than 3 inputs
• IC14 format error on power supply
• IC15 format error on clock
• IC16 format error on cooler
Constraints
IF clock>3.0 THEN cooler in {A,B}
• VC17 clock >3.0 and cooler in {A,B}
• IC18 clock >3.0 and cooler ==C
Test cases
Power supply, clock, cooler
T1:1.6, 3.1,’A’ (VC1,VC4,VC7,VC11,VC17): temperature
T2:1.6, 3.1,’B’ (VC8): temperature
T3:1.6, 2.1,’C’ (VC9): temperature
T4:1.6, 2.1,’D’ (IC10): ERROR Unknown cooler
T5:1.4, 2.1,’A’ (IC2): ERROR Power supply too low
T6:1.9, 2.1,’A’ (IC3): ERROR Power supply too high
T7:1.6, 1.8,’A’ (IC5): ERROR Clock too low
T8:1.6, 3.7,’A’ (IC6): ERROR Clock too high
T9:1.6, 3.1 (IC12): ERROR few inputs
T10:1.6, 3.1,’A’,’B’ (IC13): ERROR too many inputs
T11:1.62, 3.1,’A’ (IC14): ERROR Power supply wrong format
T12:1.6, 3.15,’A’ (IC15): ERROR Clock wrong format
T13:1.6, 3.1,’a’ (IC16): ERROR Cooler wrong format
T14:1.6, 3.1,’C’ (IC18): ERROR Wrong cooler
Boundary analysis…
• Power supply [1.5,1.8] ( 50mV steps )
• VC4 clock [2.0,3.5]
• T15: 1.45, 3.1, A | 1.50, 3.1,A | 1.55, 3.1,A 1.75, 3.1,A | 1.80, 3.1,A | 1.85, 3.1,A
1.60, 1.9, A | 1.60, 2.0,A | 1.60, 2.1,A 1.60, 3.4,A | 1.60, 3.5,A | 1.60, 3.6,A
Number of inputs : 3
• Maximum=minimum=3 already tested 2 3 e 4 (T8, T1, T9)
…Boundary analysis...
IF clock>3.0 THEN cooler >= B (We assume A>B>C)
• The IF boundary is 3.1 (i.e., the smallest value that makes the condition true ) -> 3.0 3.1 3.2
• The THEN boundary is B (i.e., the smallest value that makes the condition true ) -> A B C
We use all the 9 combinations:
• T16: 1.6, 3.0,A | 1.6, 3.0,B | 1.6, 3.0, C
• 1.6, 3.1,A | 1.6, 3.1,B |1.6, 3.1, C
• 1.6, 3.2,A | 1.6, 3.2,B |1.6, 3.2, C
…Boundary analysis
Output
• The function between input and output is not available
• However it makes sense to test (minimum output temperature):
• T17: 1.5, 2.0,A
• and the two options (maximum output temperature):
• T18: 1.8, 3.0,C
• T19: 1.8, 3.5,B
Junit: annotations
Junit: assertions
Junit : Ex1 simple Stub
public class Ex1 {
public static double temperature(double powSupply, double clock, char cooler){
return 65.0;
}
}
What about input errors?
Junit : Ex1 Stubpublic class Ex1 {
public static double temperature(double powSupply, double clock, char cooler) throws TestException{
String s=""+ cooler;
if (!s.toUpperCase().equals(s))
throw new TestException("ERROR Wrong cooler format");
if (!s.equals("A") && !s.equals("B") && !s.equals("C") )
throw new TestException("ERROR Unknown cooler");
if (clock>3.0 && cooler=='C')
throw new TestException("ERROR Wrong cooler");
if ( (int)(powSupply*100)%10>0)
throw new TestException("ERROR Power supply wrong format");
if ( (int)(clock*100)%10>0)
throw new TestException("ERROR Clock wrong format");
if ( (int)(powSupply*100)%10>0 )
throw new TestException("ERROR Power supply wrong format");
if (powSupply < 1.5)
throw new TestException("ERROR Power supply too low");
if (powSupply > 1.8)
throw new TestException("ERROR Power supply too high");
if (clock < 2.0)
throw new TestException("ERROR Clock too low");
if (clock > 3.4)
throw new TestException("ERROR Clock too high");
return 65.0;
}
public static double temperature(Object powSupply, Object clock ) throws TestException{
throw new TestException("ERROR few inputs");
}
public static double temperature(Object powSupply, Object clock, Object a, Object b) throws TestException{
throw new TestException("ERROR too many inputs");
}
}
Junit : TestException
public class TestException extends
Throwable{
public TestException(String msg){
super(msg);
}
}
Junit test
classimport org.junit.*;
import static org.junit.Assert.*;
public class Ex1Test {
public static double tMin;
public static double tMax;
@BeforeClass //Before the start of all tests
public static void setUpBeforeClass() throws Exception {
tMin=30;
tMax=70;
System.out.println("Start testing... ");
}
@AfterClass //After the completion of all tests
public static void tearDownAfterClass() throws Exception {
System.out.println("Testing done...");
}
Junit test
VCs@Test
public void testT1() throws TestException{
assertTrue("ERROR VC1,VC4,VC7,VC11,VC17: output not in range",
tMin<=Ex1.temperature(1.6,3.1,'A') &&
Ex1.temperature(1.60,3.1,'A')<=tMax);
}
@Test
public void testT2() throws TestException{
assertTrue("ERROR VC8: output not in range",
tMin<=Ex1.temperature(1.6,3.1,'B') &&
Ex1.temperature(1.60,3.1,'B')<=tMax);
}
@Test
public void testT3() throws TestException{
assertTrue("ERROR VC9: output not in range",
tMin<=Ex1.temperature(1.6,2.1,'C') &&
Ex1.temperature(1.60,2.1,'C')<=tMax);
Junit test
ICs@Test
public void testT4() throws TestException{
try {
Ex1.temperature(1.4,2.1,'D');
fail("Expected a TestException to be thrown");
} catch (TestException e) {
assertTrue(e.getMessage().equals("ERROR Unknown cooler"));
}
}
@Test
public void testT5() throws TestException{
try {
Ex1.temperature(1.4,2.1,'A');
fail("Expected a TestException to be thrown");
} catch (TestException e) {
assertTrue(e.getMessage().equals("ERROR Power supply too low"));
}
}
@Test
public void testT6() throws TestException{
try {
Ex1.temperature(1.9,2.1,'A');
fail("Expected a TestException to be thrown");
} catch (TestException e) {
assertTrue(e.getMessage().equals("ERROR Power supply too high"));
}
}
Junit test
ICs
public void testT7() throws TestException{
try {
Ex1.temperature(1.6,1.8,'A');
fail("Expected a TestException to be thrown");
} catch (TestException e) {
assertTrue(e.getMessage().equals("ERROR Clock too low"));
}
}
@Test
public void testT8() throws TestException{
try {
Ex1.temperature(1.6,3.7,'A');
fail("Expected a TestException to be thrown");
} catch (TestException e) {
assertTrue(e.getMessage().equals("ERROR Clock too high"));
}
}
@Test
public void testT9() throws TestException{
try {
Ex1.temperature(1.6,3.7);
fail("Expected a TestException to be thrown");
} catch (TestException e) {
assertTrue(e.getMessage().equals("ERROR few inputs"));
}
}
@Test
public void testT10() throws TestException{
try {
Ex1.temperature(1.6,3.1,5,9);
fail("Expected a TestException to be thrown");
} catch (TestException e) {
assertTrue(e.getMessage().equals("ERROR too many inputs"));
}
}
Junit test
ICs
@Test
public void testT11() throws TestException{
try {
Ex1.temperature(1.62,3.1,'A');
fail("Expected a TestException to be thrown");
} catch (TestException e) {
assertTrue(e.getMessage().equals("ERROR Power supply wrong format"));
}
}
@Test
public void testT12() throws TestException{
try {
Ex1.temperature(1.6,3.19,'A');
fail("Expected a TestException to be thrown");
} catch (TestException e) {
assertTrue(e.getMessage().equals("ERROR Clock wrong format"));
}
}
@Test
public void testT13() throws TestException{
try {
Ex1.temperature(1.6,3.19,'a');
fail("Expected a TestException to be thrown");
} catch (TestException e) {
assertTrue(e.getMessage().equals("ERROR Wrong cooler format"));
}
}
@Test
public void testT14() throws TestException{
try {
Ex1.temperature(1.6,3.1,'C');
fail("Expected a TestException to be thrown");
} catch (TestException e) {
assertTrue(e.getMessage().equals("ERROR Wrong cooler"));
}
}