单元测试

1 springboot单元测试

1.1 spring-boot-starter-test
我再真正需要单元测试的是什么呢,是否应该是service层呢?因为如果是controller,app启动通过yapi进行接口管理或者postman来做接口测试不是也可以吗?
但是,如果要登录呢?

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>
import com.dzmsoft.qingqiu.base.pojo.BaZi;
import com.dzmsoft.qingqiu.rule.dto.ShyGenYinDto;
import com.dzmsoft.qingqiu.rule.service.impl.QiangRuoService;
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;

@RunWith(SpringRunner.class)
@SpringBootTest
public class QiangRuoServiceTest {

    @Autowired
    private QiangRuoService qiangRuoService;

    @Test
    public void getShyDto(){
        BaZi bazi = new BaZi.Builder()
                .setYear("乙丑").setMonth("甲申").setDay("戊子").setHour("戊午")
                .build();
        ShyGenYinDto dto = qiangRuoService.getShyDto(bazi);
        System.out.println(dto.getYz());
    }
}

出现这个问题,在QingQiuApplication启动类增加@ComponentScan(basePackages = {"com.dzmsoft"})包的扫描即可。

 AopAutoConfiguration matched:
      - @ConditionalOnClass found required classes 'org.springframework.context.annotation.EnableAspectJAutoProxy', 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice', 'org.aspectj.weaver.AnnotatedElement'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
      - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)

下图是postman的调用
1
1.2 无需web环境
我只想测试一下组件中service的逻辑是否正确。

@RunWith(SpringRunner.class)
@SpringBootTest(classes =TestApplication.class, webEnvironment= SpringBootTest.WebEnvironment.NONE)
public class WhcaDemo {

    @Autowired
    private WhcaService whcaService;

    @Test
    public void test1(){
        System.out.println("hello world");
    }

这里还必须指定一个TestApplication.class,否则会报错

@SpringBootApplication
@ComponentScan(basePackages = {"com.whty"})
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class,args);
    }
}

放在test目录下即可
1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

warrah

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值