Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing Domain model objects

In our Core domain model design, we have got a class called "Category" whose constructor is internal by design. Since the constructor is internal, when writing unit test cases I won't be able to create the object of "Category".

So my question, is it a best practice to make the constructor public just for making the "Category" class testable? Or I shouldn't be testing that "Category", instead I should have tested the Class/method responsible for creating this object?

Ta,

Rajeesh

like image 834
Rajeesh Avatar asked Mar 02 '10 11:03

Rajeesh


People also ask

What is an example of unit testing in JavaScript?

For example, you might have a function that needs variables or objects that are not created yet. In unit testing, those will be accounted for in the form of mock objects created solely for the purpose of the unit testing done on that section of code.

What is a unittest framework?

A coder generally uses a UnitTest Framework to develop automated test cases. Using an automation framework, the developer codes criteria into the test to verify the correctness of the code. During execution of the test cases, the framework logs failing test cases.

What is a mock object in unit testing?

Unit Test Example: Mock Objects. Unit testing relies on mock objects being created to test sections of code that are not yet part of a complete application. Mock objects fill in for the missing parts of the program. For example, you might have a function that needs variables or objects that are not created yet.

What is a unit in software testing?

A unit may be an individual function, method, procedure, module, or object. In SDLC, STLC, V Model, Unit testing is first level of testing done before integration testing. Unit testing is a WhiteBox testing technique that is usually performed by the developer.


1 Answers

Don't make the constructor public only for the sake of unit tests. If from a design point you decided that it should be internal, leave it that way. Test the classes that invoke this constructor.

In .NET there's the InternalsVisibleToAttribute which allows you to expose internal members to unit tests.

like image 70
Darin Dimitrov Avatar answered Oct 05 '22 23:10

Darin Dimitrov