Home  >  Article  >  Java  >  How to implement a project test class in SpringBoot

How to implement a project test class in SpringBoot

PHPz
PHPzforward
2023-05-16 21:22:041075browse

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 uses Spring's SpringJUnit4ClassRunner to automatically create the Spring application context when the test starts.

The annotation @ContextConfiguration will tell it that it needs to load the configuration in CDPlayerConfig.

Because the CDPlayerConfig class contains @ComponentScan, the final application context should contain 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);
    }
  }

}

The above is the detailed content of How to implement a project test class in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete