项目搭建就不重复说了,有不明白的请参考 Maven+Eclipse(STS)搭建SSM项目笔记: https://www.jianshu.com/p/ca0e929c180a。
使用SSM项目测试问题,每次启动容器测试数据层,业务层代码有点麻烦。使用Junit单元测试可以解决每次测试数据层,业务层,控制层的代码测试。直接上代码。
package com.ssm.test;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.trans