Why are fake test frames useful?

It seems that all the Mockito examples that I looked at are “fake” behavior of the object they are testing.

If I have an object that has a method:

public int add(int a, int b) {return a+b} 

I would just use JUnit to confirm whether two integers have passed as a result of the correct output.

With all the examples I've seen with Mockito, people do things like when.Object.add(2,3).thenReturn(5) . What is the point of using this testing platform if everything you do tells the object how to act on the testing side and not on the object side?

0
tdd testing mockito mocking
source share
2 answers

Mocking frameworks are good for system testing, mocking these system dependencies; you would not use a fake framework for the layout or stub add if you are testing add . Let's break it down a bit further:

Testing add

The mocking framework is not suitable for testing your add method above. There are no dependencies except the very stable and extremely well tested JVM and JRE.

 public int add(int a, int b) {return a+b} 

However, it may be useful to check your add method if it should interact with another object as follows:

 public int add(int a, int b, AdditionLogger additionLogger) { int total = a + b; additionLogger.log(a, b, total); return total; } 

If AdditionLogger is not yet written, or if it was written to communicate with a real server or other external process, then a fake structure will be absolutely useful: it will help you come up with a fake implementation of AdditionLogger so that you can check your real interactions with it.

 @Test public void yourTest() { assertEquals(5, yourObject.add(2, 3, mockAdditionLogger)); verify(mockAdditionLogger).log(2, 3, 5); } 

Testing add consumers

By the way, the mocking structure is also hardly good for testing the consumers of your method above. After all, there is nothing particularly dangerous about calling add , so assuming it exists, you can probably name the real one in an external test. 2 + 3 will always be equal to 5, and there will be no side effects from your calculations, so very little can be obtained through ridicule or verification.

However, give your object another method that adds two numbers with a bit of random noise:

 public int addWithNoise(int a, int b) { int offset = new Random().nextInt(11) - 5; // range: [-5, 5] int total = a + b + offset; return total; } 

However, it can be very difficult for you to write a reliable assert style test against this method; in the end, the result will be somewhat random! Instead, it is easier to run the assert style test, perhaps we can drown out addWithNoise to make some of these more predictable.

 @Test public void yourTest() { when(yourObjectMock.addWithNoise(2, 3)).thenReturn(6); // You're not asserting/verifying the action you stub, you're making the dependency // *fast and reliable* so you can check the logic of *the real method you're testing*. assertEquals(600, systemUnderTestThatConsumesYourObject.doThing(yourObjectMock)); } 

Finally

It is easier to explain the mocking and syntax layout when interacting with well-known operations like add or with well-known interfaces like List , but these examples are usually not realistic cases when mocks are needed. Remember that bullying is really only useful for modeling dependencies around your system test when you cannot use real ones.

+4
source share

The purpose of unit testing is to test functionality without connecting to any external systems. If you connect to any external system, this is considered integration testing.

When performing unit testing, the system may require some data that could be obtained from external systems in the form of a database, web / leisure services, API, etc. during system testing / integration. In such scenarios, we need to provide false / fake data to validate some business rules or any other form of logic.

With the above, unit tests ensure that a particular block of code works with a given set of fake / scoffing data and should behave similarly in an integrated environment.

0
source share

All Articles