Java Reference
In-Depth Information
Listing 16.9
JUnit4OSGi test for our CalculatorService application
[...]
import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
import org.osgi.framework.ServiceReference;
import com.manning.junitbook.ch16.service.CalculatorService;
public class TestClientActivator extends OSGiTestCase {
public void testServiceAvailability() {
ServiceReference ref =
context.getServiceReference(CalculatorService. class .getName());
assertNotNull("Assert Service Availability", ref);
}
public void testParseUserCorrectInput() {
ServiceReference ref =
context.getServiceReference(CalculatorService. class .getName());
assertNotNull("Assert Availability", ref);
CalculatorService cs = (CalculatorService)
context.getService(ref);
double [] result = cs.parseUserInput("11.5 12.2 13.7");
assertNotNull("Result must not be null", result);
assertEquals("Result must be 11.5", 11.5, result[0]);
assertEquals("Result must be 12.2", 12.2, result[1]);
assertEquals("Result must be 13.7", 13.7, result[2]);
}
public void testParseUserIncorrectInput() {
ServiceReference ref =
context.getServiceReference(CalculatorService.class.getName());
assertNotNull("Assert Availability", ref);
CalculatorService cs = (CalculatorService)
context.getService(ref);
B
C
D
E
F
G
H
I
try {
double [] result = cs.parseUserInput("THIS IS A RANDOM STRING TO
TEST EXCEPTION HANDLING");
fail("A NumberFormatException was
supposed to be thrown but was not");
} catch (NumberFormatException nex) {
assertTrue(true); //this is the normal execution flow
}
}
J
1)
public void testAddMethod() {
assertTrue("Check availability of the service",
isServiceAvailable(CalculatorService. class .getName()));
CalculatorService cs = (CalculatorService)
getServiceObject(CalculatorService. class .getName(), null);
double [] numbers = cs.parseUserInput("1.2 2.4");
assertNotNull("Result from parseUserInput must not be null",
numbers);
double result = cs.add(numbers);
assertNotNull("Result from add must not be null", result);
assertEquals("Result must be 3.6", 3.6, result);
}
}
 
 
Search WWH ::




Custom Search