Relevance and usefulness of a mocking framework for emulating various physical devices

Is a mocking wireframe a good idea for dealing with multiple physical devices and making fun of them?

The main goal of my work is to link our software with several theater projectors (barco, sony ...), sound processor, IO controller (baryonet, wago).

Sometime a vendor exposes an API for communication, sometimes it is done over a socket, sometimes it is actually "vendor specific"

So far, here's our methodology:

- if it's easy: write a mock "devicexxxMock" that listens to the order and answers "yes" to every question - using ether, or Hercule - If really needed: writing an emulator (like the first method, but the answer is yes or no depends from what we want to check)

We are looking for a consistent way to test our code without real devices. And without wasting time to write an emulator.

Edit: We are talking with 4 types of devices:

Most of the devices we have to talk to with the response in a byte array on the socket. some use SNMP Some of them support HTTP ( http: // ip / commandToExecute ) Some have API in Java
+1


a source to share


5 answers


Hell. I think this is the perfect place for a mocking structure. Just create an interface layer that decouples the hardware (or hardware api) from the code that controls it ... IProjector, ISoundProcessor, etc.

Once you've decoupled hardware from software, and you're not hardware dependent, you can use a mocking structure to write mocks or stubs that pretend that IProjector is real hardware. If you know that HardwareX responds to a specific method with a specific value, write it down and return it with a mocking structure.



Note that a self-reliant framework, as such, is not required. You can always create a fake as you suggest, but sometimes this fake gets too complicated because they are used by too many tests and you get errors in your fake. In this case, the mocking framework comes in and you can only define the behavior you need in that particular test ...

+2


a source


As Brian Genicio says, this sounds like the perfect candidate for ridicule, and his arguments for ridicule versus counterfeiting are very compelling. I have not used mocks to test interactions with hardware, but I have used them to test interactions with third party APIs provided by vendors, which is very similar to use. The general scheme is: - have an interface that exposes an object that represents an input generated by an embedded API (or a hardware device in your case) - mock it to get the required input for your test case - assert that your code handles that input correctly.

This way you can test your functionality without using actual hardware (or vendor api, or other code, etc.). Mock objects are not only applicable for testing interoperability with hardware or third party apis - they can be used for all kinds of testing. When you start using mocks, you will wonder how you wrote code without using them.



I am code in Java using JUnit for my test environment and have used EasyMock ( http://easymock.org ) and JMock ( http://www.jmock.org ). I prefer (and currently use) JMock, but it's worth trying their philosophy as well as others. I also heard that Mockito ( http://mockito.org ) is very good, but didn't actually use it in anger.

+1


a source


Replace "yes" with self.Responder.Next()

. Pass an instance of the class (depending on your language) Responder

to your mocker constructor. The trivial implementation Next

is equal return "yes";

, but you can also return it to other things depending on how it was built. The actual data for this method will vary depending on what language the mocker is written in.

0


a source


Do not mock devices. I once made fun of my toaster and it tried to set my house on fire. It's not worth the risk, man.

0


a source


Atomic Object has some nice reports of their use of Mock Objects in C when working with embedded devices. See Articles at http://www.atomicobject.com/pages/Papers

0


a source







All Articles