Design for using breadboard connection

I have a simple UI application with a backend connection from which I am fetching data. Now, during development, I would not want to connect to the backend as it slows me down. So, I have locally stored data and I 'mock' the connection to return locally stored data instead of calling the server.

Now it's not for writing test cases (hence I don't see if fake frameworks appear in the picture), but just the ability to switch between local and remote connection during development.

There are many ways to do this, I suppose:

  • Getting a connection to a factory (or via dependency injection) and setting a flag that returns a local connection
  • Maintaining a properties file where I have set 'local = true' or something similar which returns a local connection.

Are there other ways to achieve this?

+2


source to share


4 answers


Do not overdo it. There's an obvious need for different implementations to return - so use a factory. Then the factory needs to know which implementation to create. You can use a property or even get away with a static boolean variable.



+1


source


There are two halves of the problem:

  • Where does the app connect from? Factory? Smart annotations driving DI? ...
  • How the connection source determines which one to use, mock, or possibly several different life options (Prod, ST, Dev, etc.).


For the former, there is a lot of discussion about DI techniques elsewhere. I would favor approaches that also included unit testing easily - I found DI to be useful in this context.

As for any properties / configurations that can be used, it depends on the type of environment you have. You are not saying if this is a standalone Java application or something running in a JEE framework. If the latter using JNDI resources might be helpful.

+1


source


When I sometimes need to use, for example, a local database instead of a production database on the web I edit

/etc/hosts

so that the production database name is included in my own local node. Fast and dirty.

0


source


Have you looked into the framework of dependency injection?

I have Google Guice to deal with this exact problem.

0


source







All Articles