Skip to content

Implement your own Shark ASAP application (4 of 5)

Thomas Schwotzer edited this page Jan 4, 2022 · 16 revisions

Step 4: Implement and test

There are German video explaining a) testing with ASAP and b) adding your code to an Android app.

Software must be tested. Full stop. No exceptions. Fortunately, we can follow / copy / adopt existing tests. We assume, you implement a SharkComponent. Good choice! Have a look into our test case in project SharkPeer. That test case is explained in more detail in this section.

Setup a Shark component

Setting up your component is not our business, though. There is a only a sketch of a real component in our tests case. That is the setupComponent() method:

private YourComponent setupComponent(SharkPeer sharkPeer) throws SharkException {
    // give peer anything that is needed to create your component
    sharkPeer.addComponent(new YourComponentFactory(), YourComponent.class);
    // force peer to create your component
    YourComponent yourComponent = (YourComponent) sharkPeer.getComponent(YourComponent.class);
    // return the object reference of your component
    return yourComponent;
}

A SharkPeer object is before setting up your component. This object comes as parameter. Remaining lines are already explained in a previous section. You add your component to the peer by creating a factory object. You can now force the peer to create an object of your component. This method simply returns this object reference.

Pretty simple. No hidden tricks.

Setup a Shark peer

Have a look at our sendAMessage() test case.

    SharkTestPeerFS.removeFolder(ALICE_ROOTFOLDER);
    SharkTestPeerFS aliceSharkPeer = new SharkTestPeerFS(ALICE, ALICE_ROOTFOLDER);

We remove a folder first. That's helpful in automated tests. That is no good idea in a real application. You would remove anything that was kept by our peer. Any message, any setting and memory of eras would be gone. Good for tests.

We create a SharkPeer in the second line. Actually, we create an instance of SharkTestPeerFS which is a subclass of SharkPeerFS. This test class adds just a few methods which come in handy for automated tests. All other Shark and ASAP related methods are derived from the real SharkPeer implementation. That is not a variant of mocking. You actually test your application with the real SharkPeer implementation.

Rule: Use SharkTestPeerFS instances in your tests. Use instances of SharkPeerFS in your application.

Clone this wiki locally