Service Injection

Some of your contracts may contain injectable service fields that are annotated with @CordaInject. You can inject your own mocks into these fields using the contract testing framework.

You can define two levels of service mocks:

To implement test-class-level mock services, implement the classLevelMockServices() function.

For example:

@NotNull
@Override
protected final Map<Class<?>, Object> classLevelMockServices() {
    return Map.of(JsonMarshallingService.class, dummyJsonMarshallingService, DigestService.class, dummyDigestService);
}
override fun classLevelMockServices() = mapOf(
    JsonMarshallingService::class.java to dummyJsonMarshallingService,
    DigestService::class.java to dummyDigestService
)

You can also inject mock services when verifying your transaction. The scope of these services only last until the verification runs. This might be useful when you want to test multiple negative paths that all require different mock services.

assertFailsWith(
    issueTransaction,
    "Default digest algorithm must be \"SHA2_256D\".",
    Map.of(DigestService.class, invalidDigestService)
);
assertFailsWith(
    issueTransaction,
    "Default digest algorithm must be \"SHA2_256D\".",
    mapOf(DigestService::class.java to invalidDigestService)
)

Was this page helpful?

Thanks for your feedback!

Chat with us

Chat with us on our #docs channel on slack. You can also join a lot of other slack channels there and have access to 1-on-1 communication with members of the R3 team and the online community.

Propose documentation improvements directly

Help us to improve the docs by contributing directly. It's simple - just fork this repository and raise a PR of your own - R3's Technical Writers will review it and apply the relevant suggestions.

We're sorry this page wasn't helpful. Let us know how we can make it better!

Chat with us

Chat with us on our #docs channel on slack. You can also join a lot of other slack channels there and have access to 1-on-1 communication with members of the R3 team and the online community.

Create an issue

Create a new GitHub issue in this repository - submit technical feedback, draw attention to a potential documentation bug, or share ideas for improvement and general feedback.

Propose documentation improvements directly

Help us to improve the docs by contributing directly. It's simple - just fork this repository and raise a PR of your own - R3's Technical Writers will review it and apply the relevant suggestions.