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?
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.
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.
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.
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.
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.
If you don't have an abstract class, or an interface, then you will have to use product like TypeMock or Microsoft's Fakes.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With