简体   繁体   中英

Junit test the Business class that invokes Dao class

I am taking over a product which is implemented using Struts and plain JDBC framework. I am planning to implement the Junit testing into the project to speed up the testing. FYI, the project is not yet in to production. This is still in Development phase, but is using very old technologies. The project does not have any interface and the respective implementation. Old programming like, the class is directly instantiated and used as such. So, cannot use mockito framework. With all these limitations, I need to develop the TDD into the project. I cannot completely scrap and develop it with new technologies.

My question is: I am writing down a junit test for a business layer to check if that method is working or not. This business class is also making a call to DAO. As the DB connection is not created at the time of test, its giving the connection error. How do I write a junit test to test only the business layer? Should I need to include the DAO connections in test method? If yes, how do I achieve this?

I am a newbie to Junit and the TDD. So, please guide me if my understand is wrong or to fix this.

Could you refactor your codes? If so, you could start from refactoring your code to define DAO interfaces for your real DAOs, implement mock version of newly created interfaces, and inject them to your services in unit test.

The simplest way to inject DAO to your service classes without any frameworks might be constructor injection, ie passing DAO instance to your service class constructor.

public class YourService {
    private final YourDAOInterface dao;

    public YourService(YourDAOInterface dao) {
        this.dao = dao;
    }
}

Here the parameter of constructor dao can be a real DAO, or mock implementation for testing (you need to make those two DAO implement the same interface YourDAOInterface ).

I suggest to refactor to a testable state before doing anything else. If design is really as broken as you said, there's now way to write a useful Test for a specific class.

I guess, the first paragraph of your write up is not related to the question that you eventually asked in second paragraph.

We call it unit testing for a reason - because we test only a unit - its not an integration test. We don't mix up layers here.

If your goal is to test only business layer - you test only business layer and you mock everything below ( like DAO etc ). So provide data to your business layer code by using some mocking framework ( Mockito, PowerMock, JMockit etc ) and assume that your DAO layer is correct.

By using mocking frameworks, you provide same data to business function that DAO layer is supposed to handover from database and you don't execute DAO layer at all. You will need these mocking APIs in addition to JUnit to properly write unit tests.

A comment to your question suggested about using In Memory database and framework ( Apache DBUnit and H2 DB are there ) but you will need those when writing unit tests for DAO layer and not for business layer.

As suggested in other answers, refactoring and making code testable is always recommended.

Hope it helps !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM