REST – How to Test a REST Client Against a REST Server with Fixtures?

apirestunit testing

When writing unit tests, it's common to use fixtures: little testable data, so we can say:
1. Get all clients should include Willy Wonka.
2. Delete client 3, and now get clients should not include Willy Wonka anymore.

That's fine for unit tests. Use setup/teardown to re-load the fixtures or rollback the transaction. So testing creates, updates, and deletes are done inside a transaction. The new temporary data lasts just for the length of that test, then is reset.

But what about when we've separated the REST server from the REST client?

We want to make sure our REST client is not just reading correctly, but creating, updating, and deleting correctly.

I haven't been able to find any examples or suggestions for how to do this against a remote test REST server.

Assuming I've got a test REST server serving only fixtures. The whole stateless nature of HTTP means it'd be hard to send a "BEGIN TRANSACTION" and "ROLLBACK TRANSACTION" or "RELOAD FIXTURES" type of message, right?

I can't be the first to want to do this, so I have a feeling I need a different way of thinking about this.

Any suggestions?

Best Answer

Software systems ideally have well-defined system boundaries and interfaces between them. REST services are good examples of this.

To that end, I would recommend against doing what you're trying to do.

Specifically:

We want to make sure our REST client is not just reading correctly, but creating, updating, and deleting correctly.

What I would suggest instead is:

  • Building tests for your REST client, to ensure that it behaves correctly, given specific input and output. Account for good (expected) and bad (unexpected) values.

  • Building tests for your REST service (if you control it, that is), to behave according to its intended function

  • Keep tests close to their problem domain, so they can help guide the design and development of what is important in that context