springboot测试用例

satuo20 1年前 ⋅ 256 阅读
  • 引入依赖版本
  <!--spring-boot-starter-parent的版本号[2.2.0.RELEASE]要和[Hoxton.RELEASE]的版本对应 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.RELEASE</version>
  </parent>
  • 加入测试模块
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
  • 编写测试代码
package com.satuo20;

import com.satuo20.entity.Book;
import com.satuo20.repository.BookRepository;
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.Optional;

@SpringBootTest(classes = ElasticApp.class)
@RunWith(SpringRunner.class)
public class AppTest {

    @Autowired
    private BookRepository bookRepository;

    @Test
    public void testSave() {
        Book book = new Book("998","saas","this is content","zhangsan",200d,new Date());
        bookRepository.save(book);
    }


    @Test
    public void testFindByName() {
        Optional<Book> book = bookRepository.findByName("saas");
        System.err.println(book.get());
    }
}

更多用法参考

https://www.cnblogs.com/myitnews/p/12330297.html


全部评论: 0

    我有话说: