What is the best practice for [Testing] in no-live env without live env resources?

Viewed 32

Let me explain this question in detail.

Background

Normally, we develop service/feature/system in no-live network and test the code also in no-live network. However, sometimes there are some dependencies which need to be accessed through live network. But it's not good to test the code in the live network directly since it may affect the online services.

Question

What is the best practice to solve such headache? (If some dependencies only can be accessed from live env)

Notes

in live network, we may have liveish env and live env

1 Answers

You either absolutely need to use the live resources to adequately test your application, or you don't. If you have no choice but to use the live resources, then you just need to do that, and I don't know what else anyone can say.

If you don't need to use the live resources, and so want to replace them with stand-ins, this is what Mocking is all about. Mocking is the standard way of not only removing the need to access live resources, but also minimizing how much of your code you're testing at any one time, thereby allowing you to better unit test small modules in a clear and consistent way.

Mocking is something usually done at a per-class level. For example, maybe you have a class that handles all of your access to a database. You want to remove the need to have a real database. So you Mock out that class. You say something like "when I submit SQL query X, return result Y as though that result had come from the database". A mocking framework makes this easy to do...to describe the desired behavior of an API for testing purposes such that the real business logic for that API is not involved, but is instead replaced by more hard-coded behavior used just for testing.

I would suggest that you find a tutorial or a book or something else that will introduce you to the idea of Mocking. That's the design pattern you're looking for. Here's one specific to Java and the Mockito mocking framework.

You mention both Java and Go. I don't know anything about Go, but there are a lot of really good mocking frameworks for the JVM. Check out Mockito or EasyMock, or Google "java mocking frameworks" to learn about others.

I assume that similar libraries exist for Go. Maybe google for "go mocking frameworks" for that.

Related