Hikaku (比較) is japanese and means "comparison". This library tests if a REST-API implementation meets its specification.
If you create your REST-API contract-first without using any type of generation, you have to make sure that specification and implementation don't diverge. The aim of this project is to meet this need and offer a mechanism to check specification and implementation for equality without having to create requests which are fired against a mock server. So this library won't check the behavior of the API, but the structural correctness. Please see also the section limitations
- Specifications
- Implementations
Please refer to the list of all features. To check the feature support for each converter.
It is possible that not every converter supports every feature. Only the intersection of the features of two EndpointConverter
s is used for the matching. Please keep that in mind regarding the equality of implementation and specification.
In order to use this library you need a github account and a repository read token.
When logged-in open the personal access tokens page. Create a new token having read:packages
as the only permission.
Add the repository to your existing list of repositories:
repositories {
maven {
name = "nagare"
url = uri("https://maven.pkg.github.com/cc-jhr/hikaku")
credentials {
username = "your-github-username-here" // you should probably use environment variables
password = "your-github-packages-read-token-here" // or gradle properties here to inject the values
}
}
}
There is an artifact for each converter. So you need one dependency for the specification and one for the implementation. Here is an example for OpenAPI as specification and Spring as implementation.
dependencies {
testImplementation "io.github.ccjhr.hikaku:hikaku-openapi:$hikakuVersion"
testImplementation "io.github.ccjhr.hikaku:hikaku-spring:$hikakuVersion"
}
Setting up a test with hikaku is very simple. You just instantiate the Hikaku
class and provide an EndpointConverter
for the specification and another one for the implementation. Optionally, you can also pass an instance of HikakuConfig
. Check the list of options and default values of the config. Then you call match()
on the Hikaku
class.
The match result is sent to one or multiple Reporter
. If the test fails kotlin's DefaultAsserter.fail()
method is called.
In the following example our project consists of an OpenAPI specification and a Spring implementation. The specification does not contain the /error endpoints created by spring, so we want to omit those.
And now we can create the test case:
@SpringBootTest
class SpecificationTest {
@Autowired
private lateinit var springContext: ApplicationContext
@Test
fun `specification matches implementation`() {
Hikaku(
specification = OpenApiConverter(Paths.get("openapi.yaml")),
implementation = SpringConverter(springContext),
config = HikakuConfig(
filters = listOf(SpringConverter.IGNORE_ERROR_ENDPOINT),
),
)
.match()
}
}
Same example in Java:
@SpringBootTest
public class SpecificationTest {
@Autowired
private ApplicationContext springContext;
@Test
public void specification_matches_implementation() {
List<Function1<Endpoint, Boolean>> filters = new ArrayList<>();
filters.add(SpringConverter.IGNORE_ERROR_ENDPOINT);
List<Reporter> reporters = new ArrayList<>();
reporters.add(new CommandLineReporter());
new Hikaku(
new OpenApiConverter(Paths.get("openapi.json")),
new SpringConverter(springContext),
new HikakuConfig(
reporters,
filters
)
)
.match();
}
}
Hikaku checks the implementation with static code analysis. So everything that is highly dynamic is not covered by hikaku. There might be other libraries and frameworks that can cover these aspects by checking the behavior.
For implementations the status codes are very dynamic. There are various ways to set a http status. For example using a ResponseEntity
object in spring or using additional filters and so on. That's why hikaku does not support http status codes.
For implementations both request and response objects are highly dynamic. For response objects there might be a generic ResponseEntity
as well or interfaces with different implementations can be used. In both cases (request and response) the objects can be altered by a serialization library and there a lot of different libs out there. That's why hikaku neither supports request nor response objects.
- Blog (english): Spotting mismatches between your spec and your REST-API with hikaku
- Blog (german): Abweichungen zwischen Spezifikation und REST-API mit hikaku erkennen
- Sample project A complete sample project