第一步:引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
第二步:编写test类
package com.lpf;
import com.lpf.grainmall.product.entity.BrandEntity;
import com.lpf.grainmall.product.service.BrandService;
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.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
//扫描指定包下面的类加入到Spring容器
@ComponentScan(basePackages = "com.lpf")
@SpringBootTest(classes = GrainmallProductApplicationTests.class)
public class GrainmallProductApplicationTests {
@Autowired
private BrandService brandService;
/**
* 注意:引入 org.junit包下面的test,不要搞错包!!!
*/
@Test
public void contextLoads() {
BrandEntity brandEntity = new BrandEntity();
brandEntity.setName("小米");
brandEntity.setDescript("性价比超高");
boolean save = brandService.save(brandEntity);
System.out.println(save);
}
}