集成了SSM框架的系统怎么做测试?
1.首先在测试文件夹下新建一个测试基类BaseTest
BaseTest中的代码如下:
package wbl_ssm_blog.mapper; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; // 加载测试运行基础环境 JUnit4 @RunWith(SpringJUnit4ClassRunner.class) // 加载resources下面的所有源代码 @WebAppConfiguration("src/main/resouces") // 加载mybatis所有的恶配置文件 @ContextConfiguration(locations={"classpath:mybatis/mybatis-config.xml", "classpath:spring/spring-mybatis.xml"}) public class BaseTest{ protected Logger log = LoggerFactory.getLogger(getClass()); @Test public void test(){ System.out.println("aaa"); } }
然后再新建一个我们实际需要使用的测试类,这个测试类需要继承我们刚才新建的BaseTest。
package wbl_ssm_blog.mapper; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import wbl_ssm_blog.entity.Article; import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import static org.junit.Assert.*; public class ArticleMapperTest extends BaseTest{ @Autowired public ArticleMapper articleMapper ; @Test public void findArticleBySome() { List<Article> articleList = articleMapper.findArticleBySome("的",10); System.out.println(articleList.size()); for(Article article : articleList) { System.out.println(article); } } @Test public void findArticleBySome01() { Article criteria = new Article(); criteria.setArticleTitle("操作"); criteria.setArticleViewCount(2); List<Article> articleList = articleMapper.findArticleBySome01(criteria); System.out.println(articleList.size()); for(Article article : articleList) { System.out.println(article); } } @Test public void deleteById() { int returnResult = articleMapper.deleteById(36); System.out.println(returnResult); } @Test public void insert() { Article article = new Article(); article.setArticleViewCount(10); article.setArticleTitle("你好,这是测试文章的标题"); article.setArticleCommentCount(20); article.setArticleContent("你好,这是测试文章的内容"); article.setArticleCreateTime(new Date()); article.setArticleUpdateTime(new Date()); article.setArticleIsComment(1); article.setArticleLikeCount(20); article.setArticleOrder(1); article.setArticleStatus(1); article.setArticleSummary("你好,这是测试文章的总结"); article.setArticleUserId(1); articleMapper.insert(article); System.out.println(article.getArticleId()); } @Test public void update() { Article article = new Article(); article.setArticleId(39); article.setArticleTitle("你好,39号文章的标题被更新"); article.setArticleContent("你好,39号文章的内容被更新"); article.setArticleSummary("你好,39号文章的总结被更新"); articleMapper.update(article); System.out.println(article); } @Test public void findAll() { HashMap<String,Object> cateria = new HashMap<String,Object>(); cateria.put("status",1); cateria.put("userId",1); // cateria.put("keywords","操作"); // cateria.put("categoryId",1); cateria.put("tagId",1); List<Article> articleList = articleMapper.findAll(cateria); System.out.println(); // order by `article`.`article_order` DESC ,`article`.`article_id` DESC } @Test public void listAllNotWithContent() { List<Article> articleList = articleMapper.listAllNotWithContent(); System.out.println(); } @Test public void countArticle() { int rows = articleMapper.countArticle(1); System.out.println(); } @Test public void countArticleComment() { int commentCount = articleMapper.countArticleComment(); System.out.println(); } @Test public void countArticleView() { int viewCount = articleMapper.countArticleView(); System.out.println(); } @Test public void getArticleByStatusAndId() { Article article = articleMapper.getArticleByStatusAndId(1,1); System.out.println(); } @Test public void pageArtilce() { List<Article> articleList = articleMapper.pageArtilce(1,5,10); System.out.println(); } @Test public void deleteBatch() { List<Integer> ids = new ArrayList<Integer>(); ids.add(40); ids.add(41); articleMapper.deleteBatch(ids); } @Test public void listArticleByViewCount() { List<Article> articleList = articleMapper.listArticleByViewCount(3); System.out.println(); } @Test public void getAfterArticle() { Article article = articleMapper.getAfterArticle(1); System.out.println(); } @Test public void getPreArticle() { Article article = articleMapper.getPreArticle(2); System.out.println(); } @Test public void listRadomArticle() { List<Article> articleList = articleMapper.listRadomArticle(3); System.out.println(); } @Test public void listArticleByCommentCount() { List<Article> articleList = articleMapper.listArticleByCommentCount(3); System.out.println(); } @Test public void updateCommentCount() { articleMapper.updateCommentCount(3); System.out.println(); } @Test public void getLastUpdateArticle() { Article article = articleMapper.getLastUpdateArticle(); System.out.println(); } @Test public void countArticleByUser() { int articleCount = articleMapper.countArticleByUser(1); System.out.println(); } @Test public void findArticleByCategoryId() { List<Article> articleList = articleMapper.findArticleByCategoryId(1,3); System.out.println(); } @Test public void findArticleByCategoryIds() { List<Integer> ids = new ArrayList<>(); ids.add(1); ids.add(2); ids.add(3); List<Article> articleList = articleMapper.findArticleByCategoryIds(ids,5); System.out.println(); } @Test public void listArticleByLimit() { List<Article> articleList = articleMapper.listArticleByLimit(3); System.out.println(); } }
然后就可以对mapper.xml文件中的方法进行测试了。