2

I have the following method wherein the business layer is interacting with the data access layer and returning the collection object. I'm new to unit testing, but need to add automated unit tests to the solution. I read multiple articles and theory related to unit testing, but I'm confused with how to proceed. It would be really helpful If somebody can guide me with approach,

[DataObjectMethod(DataObjectMethodType.Select, true)]
public static WorkQueueBE GetItemByDetailsID(int detailsID)
{  return WorkQueueDB.GetItemByDetailsID(detailsID); }

This method gives call to GetItemsByDetailsID method in db layer, which in turn calls a stored procedure, gets the data from database, fills the collection and returns an object.

10
  • 1
    Try to implement a Mock for your data-access objects and inject them into your business layer. Commented May 4, 2015 at 13:08
  • If OP's purpose is to test the business logic is calling the expected data access method, then yes. But I'm guessing its more so the data-access layer should be tested so that the they are calling the actual stored procedure which is actually found from the deployed database and they return appropriate object(s)? Commented May 4, 2015 at 13:26
  • @JanneMatikainen which would make it an integration test, and not a unit test. Commented May 4, 2015 at 14:03
  • Given the static methods, the lack of Dependency Injection and the wording of the question, I suspect that you'll need to study up a little more on what a unit test is and what it generally tests. Commented May 4, 2015 at 14:04
  • @jessehouwing its existing code and i am suppose to introduce automated unit testing for the product. Stucked. Commented May 5, 2015 at 8:51

1 Answer 1

1

I'm gonna summarize the comments a bit as well as add some new thoughts. You write

This method gives call to GetItemsByDetailsID method in db layer, which in turn calls a stored procedure, gets the data from database, fills the collection and returns an object.

A comment to this is -> A unit test should only test an isolated part of your logic, that will say a single method. Not the entire flow, that's an integration test.

From what I see in your code snippet you use concrete classes. If you really want to make your application easy to test you need to use interfaces and abstract classes that can be instantiated to concrete classes as well as easily mocked and stubbed. An natural way on how to learn how to implement interfaces, abstract classes and concrete classes is to do Test Driven Development. Start with a small project and learn from there :)

If I would want to unit test your method that you've provided I would separate your logic from the data-access layer. This I would do by making the data-access layer classes implement interfaces of what they should do. This way I can mock the data-access layer and just return a specific snippet of data, just the part I need to create my unit tests for the business-layer method. After all, in this case I want to test the business-layer-method's logic, not the data-access-layer-method's.

It is quite tough to start doing unit-testing-friendly code but when you start getting a grip of you are gonna love it :)

This was a lot of theory and no concrete example because I think you need to start with a small project of your own and do it the TDD way, by doing this you will understand how everything works concerning unit testing.

Some links to get you started https://msdn.microsoft.com/en-us/library/aa730844(v=vs.80).aspx https://msdn.microsoft.com/en-us/library/ff847525(v=vs.100).aspx http://www.codeproject.com/Articles/321154/Test-Driven-Development-TDD-in-Csharp

Also Pluralsight has some courses on this. Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.