sf-api
Unit testing
pom.xml
Add the dependency to pom.xml as below.
<dependency>
<groupId>org.mobicents.ssf</groupId>
<artifactId>sf-mock</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
<optional>false</optional>
</dependency>
This configuration builds the necessary environment for unit tests.
Unit testing
When test simple beans, implements goes like this.(using TestNG)
public class CheckRequireBeanTest extends TestBase {
MockSipFactory factory;
CheckRequireBean targetBean;
@BeforeMethod
public void setup() {
super.setup();
targetBean = new CheckRequireBean();
}
@AfterMethod
public void teardown() {
super.teardown();
}
@Test
public void testHandleRequest1() throws Exception {
SipServletRequest req = createRequest("INVITE", "sip:from@oki.com",
"sip:to@oki.com");
// Not include Require header field.
targetBean.handleRequest(req);
// When sent the response by target bean,
// the message will be added to MessageContextLocal.
List<SipServletMessage> messageList = MessageContextLocal.getList();
Assert.assertEquals(messageList.size(), 0);
}
}
This is a unit test for CheckRequireBean. CheckRequireBean checks the Require header field, and send 420 response when includes unsupported extensions.
This test is checking that CheckRequireBean sends no response, when the request does not include the Require header field.
TestBase class (that creating the request) is below:
public class TestBase {
MockSipFactory factory;
public void setup() {
MockSipServletContext context = new MockSipServletContext("check");
factory = new MockSipFactory(context);
}
public void teardown() {
MessageContextLocal.clear();
}
public SipServletRequest createRequest(String method, String from, String to)
throws ServletParseException {
SipApplicationSession appSession = factory.createApplicationSession();
SipServletRequest req = factory.createRequest(appSession, method, from,
to);
return req;
}
public SipServletResponse getSipServletResponse() {
List<SipServletMessage> list = MessageContextLocal.getList();
SipServletResponse res = (SipServletResponse) list.get(0);
return res;
}
}
Mock unit tests have functions that register the SipServlet and execute all SIP sequences. Please refer to the other document about these functions.
In simple bean unit test, the sequence is shown below.

- Create the MockSipServletContext
- Create the MockSipFactory
- Create a SipServletRequest
- You can create a SipServletRequest same as using SIP Servlet API.
- Dispatch the SipServletRequest to the target bean.
- If the bean send a SIP message, that is stored in MessageContextLocal.
- Check SipServletMessage(s).
Please deploy the application, and check the application operates normally.
In next, extend this application to operate Click To Call.
