SpringBoot项目的测试类

JackYin 2019-12-22

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<Banner> list = dao.selectAllBanner(1,10,null);
        for (Banner banner : list) {
            System.out.println(banner);
        }
    }

}

其中类SpringRunner继承了了SpringJUnit4ClassRunner

相关推荐