SpringBoot中怎么实现一个项目测试类

这篇文章给大家介绍SpringBoot中怎么实现一个项目测试类,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

创新互联于2013年创立,是专业互联网技术服务公司,拥有项目成都网站设计、做网站、成都外贸网站建设公司网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元凤台做网站,已为上家服务,为凤台各地企业和个人服务,联系电话:18982081108

1.

package soundsystem;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest {

 @Autowired
 private CompactDisc cd;

 @Test
 public void cdShouldNotBeNull() {
  assertNotNull(cd);
 }

}

CDPlayerTest使用了Spring的SpringJUnit4ClassRunner,以便在测试开始的时候自动创建Spring的应用上下文。

注解@ContextConfiguration会告诉它需要在CDPlayerConfig中加载配置。

因为CDPlayerConfig类中包含了@ComponentScan,因此最终的应用上下文中应该包含CompactDiscbean。

2.

package com.baizhi.cmfz;


import com.baizhi.cmfz.dao.BannerDao;
import com.baizhi.cmfz.entiy.Banner;
import com.baizhi.cmfz.service.BannerService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BannerTest {


  @Autowired
  private BannerDao dao;
  @Autowired
  private BannerService service;


  @Test
  public void test1(){
    List list = dao.selectAllBanner(1,10,null);
    for (Banner banner : list) {
      System.out.println(banner);
    }
  }

}

关于SpringBoot中怎么实现一个项目测试类就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网页标题:SpringBoot中怎么实现一个项目测试类
文章起源:http://bzwzjz.com/article/jdihoi.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站建设 H5网站制作 营销网站建设 成都网站设计 定制级高端网站建设 成都网站建设公司 网站设计 教育网站设计方案 企业网站设计 重庆网站建设 营销型网站建设 成都网站建设公司 重庆企业网站建设 成都网站建设公司 网站建设方案 成都网站设计 响应式网站设计 成都网站设计公司 自适应网站设计 成都网站制作 成都网站制作公司 成都网站制作