Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New to mocking frameworks

I am trying to do Test Driven Development as a single developer (possibly increasing the team to four). I have experience using NUnit to a limited extend for unit testing.

I have been developing this system for a couple of years (VB.NET). The developer before me was working on the system for three years and he favored Martin Fowlers Transaction Script approach meaning that there are large classes with large monolithic functions with virtually no consideration about design/re useability etc.

I have looked into some of the Mocking frameworks available for Nunit and I believe the only option I have is the commercial product TypeMock (RhinoMock vs. TypeMock vs. NUnit's Mocking?) as it allows you to mock classes that are none abstract and without interfaces. Is this correct?

I have read some posts on here that suggest this is not the case. Hence the reason for this question. Can I use any free mocking frameworks for Transaction script/monolithic systems?

like image 766
w0051977 Avatar asked Jul 06 '13 12:07

w0051977


People also ask

What are the mocking frameworks?

What is a mocking framework? Mocking frameworks are used to generate replacement objects like Stubs and Mocks. Mocking frameworks complement unit testing frameworks by isolating dependencies but are not substitutes for unit testing frameworks.

Is mocking good practice?

Mocking isn't just about isolating the code under test—it's also about creating faster and simpler tests. But, if you don't do it right, mocking can also just generate more code for you to manage and maintain.

Why do we need mocking frameworks like Mockito?

Using a mocking framework frees you from needing to write, refactor and update your hand rolled mocks whenever you change the faked object. In other words using a mocking framework is just like any other 3rd party library e.g. ORM - someone else wrote the code so you don't have to.

Which framework is used to create mocked objects?

We can use Mockito class mock() method to create a mock object of a given class or interface. This is the simplest way to mock an object. We are using JUnit 5 to write test cases in conjunction with Mockito to mock objects.

Is Mockito a mocking framework?

Mockito is a java based mocking framework, used in conjunction with other testing frameworks such as JUnit and TestNG. It internally uses Java Reflection API and allows to create objects of a service. A mock object returns a dummy data and avoids external dependencies.


1 Answers

Short Answer

If you don't have an abstract class, or an interface, then you will have to use product like TypeMock or Microsoft's Fakes.

Long Answer

Don't go down this route. The chief virtue of TDD is that it forces you to have loosely coupled code, and clear abstractions. Without that, then your tests will become ridiculously hard to setup, and maintain.

What you really need to do is to refactor. Slowly. Here is my recommendation.

  1. Start by finding a component that is hard to test, and identify all your dependencies.
  2. Start extracting interfaces from those classes, and then make the dependent class depend on the interfaces instead.
  3. Invert the dependencies by making them "injectable" via the constructor or a property.
  4. Start writing tests to describe the behavior of that class, mocking the dependencies.
  5. Rinse repeat until you can begin to tame your code base.

Mocking classes without interfaces can be useful for code that you don't control. But it is still hard, and will get unwieldy and complicated over time.

Let your tests talk to you. If your tests are difficult to work with, or complicated to setup, then it means your code needs to be refactored. Don't ignore that feedback.

like image 102
Josh Avatar answered Oct 15 '22 19:10

Josh