vaibhav.g
vaibhav.g

Reputation: 759

How to create mock test of following method

I have created an application in which i have used asyncronous api of zookeeper. I am not able to write mock test of following method.

public static void createAssignNode(String path, long endPointer) {
        zk.create(path, Bytes.toBytes(endPointer),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT,assignTaskWorkerCallback,endPointer);
}

static AsyncCallback.StringCallback assignTaskWorkerCallback = new AsyncCallback.StringCallback() {
    public void processResult(int resultCode, String path, Object endPointer, String name) {
        switch (KeeperException.Code.get(resultCode)) {
            case CONNECTIONLOSS:
                LOG.error("Connection Loss");
                throw new IllegalStateException();
            case OK:
                LOG.info("My created task name: " + name);
                break;
            default:
                LOG.error("Something went wrong" + KeeperException.create(KeeperException.Code.get(resultCode), path));
        }
    }
};

I read about how to write mock test cases a lot but still facing problem in writing mock test of above method.

I am writing mock test for the first time

Upvotes: 0

Views: 365

Answers (1)

Gavin Clarke
Gavin Clarke

Reputation: 389

To write a test you first need to know what behaviour you are trying to test. If you cannot concisely describe the behaviour you are testing then you won't be able to write a concise test case.

The only part of this code that seems suitable for testing with a mock is testing that you register your callback with zookeeper. To test this you'd need to inject a mock zookeeper instance into your zk variable. That might be tricky with your current design because you've made it a static. If zk was an instance member injected in the constructor then you'd easily be able to replace it with a mock object in the test case.

Upvotes: 1

Related Questions