gsb
gsb

Reputation: 1229

Test and Mock framework for Java and .NET

I'd like to know your thoughts about test/mocking frameworks that are widely used and have a good level of compatibility between Java and .NET. I mean, I want to learn those tools to use in a .NET project, but I still wanna be able to apply that knowledge in Java projects.

Upvotes: 4

Views: 6423

Answers (8)

azheglov
azheglov

Reputation: 5523

First of all, it's important to separate the concepts of testing and mocking. For unit-testing (to drive your tests), JUnit (for Java) and NUnit (for .NET) are the most popular choices. The situation around mocking (or isolation) frameworks is more complicated.

Mocking frameworks divide into two camps, which I like to call "conventional" and "alternative."

Conventional frameworks rely primarily on design for testability and dependency injection. Alternative frameworks rely on things like Profiling API or the Java 5 SE instrumentation feature (the java.lang.instrument package) to modify CIL or bytecode at runtime, which makes any dependency mockable.

Examples of such alternative networks are Typemock Isolator (for .NET) and JMockit (for Java) (Typemock is not free unless you use it in an open-source project.)

As for the conventinal mocking frameworks for .NET, the two most popular ones currently are RhinoMocks and Moq. Both rely heavily on C# 3.0 features, particularly the lambda syntax, to allow concise specifications of mock object behaviors and expectations.

Upvotes: 2

Pawel Lesnikowski
Pawel Lesnikowski

Reputation: 6391

NMock is far from being best mock framework in .NET world. It is string based:

Expect.Once.On(mockView).GetProperty("FromAccount").Will(Return.Value("1234"));

I strongly suggest somethingmore up-to-date like Rhino.Mocks:

Expect.Call(mockView.FromAccount).Returns("1234");

Upvotes: 2

Petter Wigle
Petter Wigle

Reputation: 862

I would suggest that you take a look at Moq for mocking in .NET. It is conceptually very similar to Mockito on the Java side.

Upvotes: 5

Carl Bergquist
Carl Bergquist

Reputation: 3942

We use MbUnit and Rhino.Mocks for testing. I really like it.

The reason we use MbUnit is the Reflector.. Perhaps it can be found in NUnit also, but I found it in MbUnit first ;)

Upvotes: 0

Elijah
Elijah

Reputation: 13604

Here is what I see from my experience on the Java side of the fence.

Unit testing frameworks

As for unit testing in Java, pretty much everyone is using JUnit and with JUnit 4.0 using annotations I understand that it is more like NUnit now.

Mocking frameworks

We were using EasyMock on our project for about half a year and we determined that it was eating up a lot of our time just to do simple tasks. Actually, we made a lot of jokes about how EasyMock is not easy.

After attending a lecture on mocking frameworks, I decided to go with Mockito and have never looked back. It allows partial mocking painlessly - something that EasyMock required a separate library for. Also, Mockito has much better error messaging. When you do something wrong, it will give you verbose errors on how you violated Mockito's contract.

Anyways, give both a spin and I think you will agree that Mockito is a clear winner.

Upvotes: 4

Huxi
Huxi

Reputation: 4311

I've used EasyMock in some Java projects and I like it very much. There seems to be a .net port but I haven't used it yet, personally.

Concerning unit tests, I use JUnit 4.x, which has some nice extensions compared to 3.x. I guess NUnit provides similar functionality but haven't used it, either.

Upvotes: 1

John Feminella
John Feminella

Reputation: 311755

The N/J-series of frameworks (NUnit/JUnit, NMock/JMock, etc.) are typically parallel ports of each other or are based on the same starting principles. Those will definitely let you transfer at least some of your knowledge between them.

Upvotes: 5

crauscher
crauscher

Reputation: 6618

We use RhinoMocks and NUnit for our .NET projects. JUnit will be the Java alternative

Upvotes: 2

Related Questions