Integration testing in Spring

Source: Internet
Author: User

In unit testing, we try to focus on the correctness of the internal logic of the module in the case of interference between the modules. Integration testing is the test of integrating modules together, and its purpose is to discover some of the problems of integration between modules. Some functions are difficult to simulate by simulating objects, but they are often only after the real module is integrated, can really run up, such as transaction management is one of the more typical examples.

As recommended by Spring (quote: You should not normally using the Spring container for unit tests:simply populate your POJOs in plain JUnit tests!), you should not rely on the spring container for unit testing. In other words, you should not start Applicatoncontext and get the bean from the unit test, instead you should complete the unit test by simulating the object. The prerequisite of integration testing is to assemble the association class between the module and the module beforehand, such as assembling the DAO layer's real Userdao and Loginlogdao to Userserviceimpl and then testing. The specific assembly work is done in the spring configuration file, so in general, the integration test needs to start the spring container, and you can simply take the target bean out of the test class and test it from the spring container.

Business interfaces that need to be tested

Let's say we have a UserService business layer interface in our application that has 4 business methods, and the code looks like this:

Code Listing 1 Userservie interface

package com.baobaotao.service;
import com.baobaotao.domain.User;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public interface UserService {
boolean hasMatchUser(String userName,String password);
User findUserByUserName(String userName);
void loginSuccess(User user);
void registerUser(User user);
}

We provide implementation via USERSERVICEIMPL to UserService:

Code Listing 2 Userserviceimpl Implementation UserService interface

Package com.baobaotao.service;
Import Com.baobaotao.dao.LoginLogDao;
Import Com.baobaotao.dao.UserDao;
Import Com.baobaotao.domain.LoginLog;
Import Com.baobaotao.domain.User;
public class Userserviceimpl implements UserService {
Private Userdao Userdao;
Private Loginlogdao Loginlogdao;
public boolean Hasmatchuser (string userName, string password) {
int MatchCount =userdao.getmatchcount (userName, password);
return matchcount > 0;
}
Public User finduserbyusername (String userName) {
Return Userdao.finduserbyusername (UserName);
}
public void loginsuccess (user user) {
User.setcredits (5 + user.getcredits ());
Loginlog Loginlog = new Loginlog ();
Loginlog.setuserid (User.getuserid ());
Loginlog.setip (User.getlastip ());
Loginlog.setlogindate (User.getlastvisit ());
Userdao.updatelogininfo (user);
Loginlogdao.insertloginlog (Loginlog);
}
public void Setloginlogdao (Loginlogdao Loginlogdao) {
This.loginlogdao = Loginlogdao;
}
public void Setuserdao (Userdao Userdao) {
This.userdao = Userdao;
}
}

Userserviceimpl references the two DAO layer classes (Userdao and Loginlogdao) to implement the UserService interface, which we have to integrate to test before Userserviceimpl is open for use. To ensure the correctness of the implementation logic.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.