Use a Mock Shim to Test a Chaincode

This method of testing involves using a mock version of the stub shim.ChaincodeStubInterface. With this you can simulate some functionality of your chaincode before deploying it to Oracle Blockchain Platform. You can also use this library to build unit tests for your chaincode.

Manually Vendor the Shim with a Chaincode

This applies to Oracle Blockchain Platform 19.1.1, 19.1.3, 19.2.1, and 19.2.3.

In Hyperledger Fabric, the fabric-ccenv image contains the (shim) package. This allows you to package a chaincode without needing to include the shim. However, this may cause issues in future Hyperledger Fabric releases, and it may cause issues when using packages that are included with the shim.

Workaround: To avoid potential issues, you should manually vendor the shim package with the chaincode prior to using the peer command-line interface for packaging and installing a chaincode, or packaging or installing a chaincode. See

Use a Mock Shim to Test a Chaincode

  1. Create a test file that matches the name of the chaincode file.
    For example, if car_dealer.go is the actual implementation code for you smart contract, you would create a test suite called car_dealer_test.go containing all the tests for car_dealer.go. The test suite filename should be in the *_test.go format.
  2. Create your package and import statements.
    package main
    import (
  3. Create your unit test.
    * TestInvokeInitVehiclePart simulates an initVehiclePart transaction on the CarDemo cahincode
    func TestInvokeInitVehiclePart(t *testing.T) {
        fmt.Println("Entering TestInvokeInitVehiclePart")
        // Instantiate mockStub using CarDemo as the target chaincode to unit test
        stub := shim.NewMockStub("mockStub", new(CarDemo))
        if stub == nil {
            t.Fatalf("MockStub creation failed")
        var serialNumber = "ser1234"
        // Here we perform a "mock invoke" to invoke the function "initVehiclePart" method with associated parameters
        // The first parameter is the function we are invoking
        result := stub.MockInvoke("001",
                []byte("airbag 2020"),
                []byte("aaimler ag / mercedes")})
        // We expect a shim.ok if all goes well
        if result.Status != shim.OK {
            t.Fatalf("Expected unauthorized user error to be returned")
        // here we validate we can retrieve the vehiclePart object we just committed by serianNumber
        valAsbytes, err := stub.GetState(serialNumber)
        if err != nil {
            t.Errorf("Failed to get state for " + serialNumber)
        } else if valAsbytes == nil {
            t.Errorf("Vehicle part does not exist: " + serialNumber)


Not all interfaces of the stub are implemented. Stub functions
  • GetQueryResult

  • GetHistoryForKey

are not supported, and attempting to call either of these will result in an error.