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

How to implement a project test class in SpringBoot

PHPz
Release: 2023-05-16 21:22:04
forward
1168 people have browsed it

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);
 }

}
Copy after login

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);
    }
  }

}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template