Dependency Management In Unit Tests

Lecture



  Dependency Management In Unit Tests

Dependency management

● In practice, cases of well-isolated code are quite rare.

Types of dependencies

● Injecting dependencies (respecting DI)

class A { B b;

public setB (B b) {this.b = b;}

...

}

Compiled dependencies (for instance referencies Filesystem API)

class A {

public void methodToTest () {

...

InputFile inStream = new InputFileStream ("input.txt");

...

inStream.read (buffer);

...

}

}

Dependency Management (“Bad Example”)

class A {
private B bInstance;
public setB (B bInstance) {
this.bInstance = bInstance;
}
public int myMethod (int number) {
...
bInstance.doSomething (1,2);
...
return number;
}
}
class B {
public void doSomething () {
...
}
}

class MyATest extends TestCase {
@Test
public void testWithSImpleInput () {
// initialization
A a = new A ();
B b = new b ();
a.setB (b);
// execution
int actual = a.myMethod (4);
// verification
int expected = 2;
assertEquals (expected, actual);
}
}

Unit Test Problems without Dealing with Addictions
● Floating errors. The difficulty of finding them. Loss of time
● Without eliminating dependencies, unit tests are de facto integration tests.

Dependency management

● For successful unit testing, it is necessary to eliminate all dependencies. Otherwise, potential defects in dependencies can affect the success of tests and complicate the search for defects.

● The solution is that all dependent objects are replaced by “fake” objects that behave exactly as you expect.

● Mocks ... replace a class with a small substitute (“mocks”) that implements the same interface.

● Shims ... modify the compiled code at run time, to inject and run a substitute (“shim”).

● Effective means are needed to create such fake objects on the fly.

  Dependency Management In Unit Tests

Mocking frameworks

Java   Dependency Management In Unit Tests

C #   Dependency Management In Unit Tests   Dependency Management In Unit Tests

Injecting dependencies

  Dependency Management In Unit Tests

Dependency Management Case - Stubbing (Shooters)

  Dependency Management In Unit Tests

Dependency Management Case - Stubbing (AnyParameter)

  Dependency Management In Unit Tests

Dependency Management Case - Verifying

  Dependency Management In Unit Tests

Dependency Management Case - Invocation Counting

  Dependency Management In Unit Tests

Dependency Management Case - Consecutive Calls

  Dependency Management In Unit Tests

Dependency Management Case - Partial Mocking

  Dependency Management In Unit Tests

Compiled dependencies

How to do?
● Wrap in a private method that can be easily overridden from a heir or partially replaced (partial mocking)
● Use gaskets and special structures (PowerMock) instead of a fake code

Dummy dependency management
Dummy - objects that are passed as
parameters but never used.
● Often used to fill an array.
test data:
int [] k = {1, 2, 3};
...
● Or call the method where the parameter is required but not
matter:
doSomething (123, new Integer (0));

Mocks Dependency Management

Mocks - objects that are reprogrammed to wait for what parameters they will receive and what they will return
Mocks are used where there are not enough Dummies, i.e., behavior is required from objects.
Mocks reflect the concept of "behavior verification".
● Mocks are created declaratively through the rules, as if “stringing behavior on each other”

Stubs dependency management

Stubs are stubs, very similar to Mocks, but the main difference is that Stub is created non-declaratively using
ordinary language primitives. Stubs reflect the concept of state verification.
● In many cases, Stubs can be implemented through Mocks which is preferred.

More Advices
Testing of objects in which there is a call to external devices, such as a database or a network (sending emails, data exchange).
● It is usually solved using Mocks or Fakes, depending on the complexity of the behavior.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Software and information systems development

Terms: Software and information systems development