若依SpringBoot单元测试类

        <!--测试类-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!--测试类-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--测试类-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>
import com.rjgf.influxdb.RjgfInfluxdbApplication;
import com.rjgf.influxdb.domain.OdsElectricConsume;
import com.rjgf.influxdb.service.IOdsElectricConsumeService;
import com.rjgf.influxdb.service.influxdbService;
import com.rjgf.influxdb.utils.DateUtil;
import com.rjgf.influxdb.utils.InfluxDBConnection;
import com.rjgf.influxdb.utils.SnowFlake;
import com.rjgf.influxdb.utils.factory.InfluxdbFactory;
import org.influxdb.dto.QueryResult;
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.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author 
 * @date 2022/9/19 8:25
 * @remark
 */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RjgfInfluxdbApplication.class)
public class TestTime {

    @Autowired
    private influxdbService infludbService;


    private final static String electricConsumeSql = "SELECT MAX(kwh) FROM \"electric_amount\" \n" +
            "WHERE  time >= '%s' \n" +
            "AND   time <= '%s' \n" +
            "GROUP BY buildId,deptId,energyCategory";

    @Test
    public void testhourTime(){
        infludbService.hourElectricityConsumption();
    }

    @Autowired
    private IOdsElectricConsumeService odsHourElectricConsumeService;

    @Test
    public void testdayTime(){


        Date d = new Date();
        String beforeOneDay = "2022-09-17 00:00:00";
        String beforeTwoDay = "2022-09-16 00:00:00";
        String nowDay = "2022-09-18 00:00:00";

        String nowDaySql = String.format(electricConsumeSql, beforeOneDay, nowDay);
        String beforeDaySql = String.format(electricConsumeSql, beforeTwoDay, beforeOneDay);

        System.out.println("nowDaySql"+nowDaySql);
        System.out.println("beforeDaySql"+beforeDaySql);

        List<OdsElectricConsume> odsElectricConsumeList = searchElectricityConsumption(
                nowDaySql, beforeDaySql, beforeOneDay);

        odsHourElectricConsumeService.insertOdsHourElectricConsumeList(odsElectricConsumeList, "ods_day_electric_consume");
    }

    @Test
    public void testdayTimetow(){

        infludbService.dayElectricityConsumption();
    }

    /**
     * 从时序数据库查询数据,返回清洗数据
     */
    public List<OdsElectricConsume> searchElectricityConsumption(String nowSql, String beforeSql, String influxdbTime) {

        InfluxDBConnection influxdbconnection = InfluxdbFactory.getInfluxdb("energy_manager_dev");

        Map<Map<String, String>, List<Object>> nowMap = influxdbconnection.query(nowSql)
                .getResults().get(0).getSeries()
                .stream().collect(Collectors.toMap(QueryResult.Series::getTags, o -> o.getValues().get(0)));

        Map<Map<String, String>, List<Object>> beforeMap = influxdbconnection.query(beforeSql)
                .getResults().get(0).getSeries()
                .stream().collect(Collectors.toMap(QueryResult.Series::getTags, o -> o.getValues().get(0)));

        List<OdsElectricConsume> odsElectricConsumeList = new ArrayList<>();
        for (Map.Entry<Map<String, String>, List<Object>> mapListEntry : nowMap.entrySet()) {
            if (beforeMap.containsKey(mapListEntry.getKey())) {
                double nowConsume = (double) nowMap.get(mapListEntry.getKey()).get(1);
                double beforeConsume = (double) beforeMap.get(mapListEntry.getKey()).get(1);
                //#.00 表示两位小数
                DecimalFormat df = new DecimalFormat("#0.00");
                double consume = Double.parseDouble(df.format(nowConsume - beforeConsume));
                Map<String, String> tagsMap = mapListEntry.getKey();
                OdsElectricConsume odsHourElectricConsume = new OdsElectricConsume(
                        SnowFlake.nextId(),
                        Integer.parseInt(tagsMap.get("buildId")),
                        Long.parseLong(tagsMap.get("deptId")),
                        tagsMap.get("energyCategory"),
                        DateUtil.StrToDate(influxdbTime),
                        consume);
                odsElectricConsumeList.add(odsHourElectricConsume);
            }
        }
        return odsElectricConsumeList;
    }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
若依Spring Boot提供了方便的工具来进行单元测试。首先,你需要在需要进行单元测试的服务模块中引入依赖spring-boot-starter-test。这可以通过在pom.xml文件中添加以下代码来实现: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> ``` 接下来,你需要创建一个测试。注意,测试的位置应该与Spring Boot的启动对应。如果你的测试位置改变了,可能会找不到启动。你可以通过在测试上添加`@SpringBootTest`注解来告诉Spring Boot这是一个测试,并且它需要启动整个Spring应用程序。 以下是一个简单的单元测试案例的示例代码: ```java import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class MyServiceTest { @Autowired private MyService myService; @Test public void testMyService() { // 测试代码 } } ``` 在这个例子中,我们使用了`@Autowired`注解来自动注入一个`MyService`实例,然后我们可以在`testMyService`方法中编写具体的测试代码。你可以根据需要添加更多的测试方法来测试不同的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [若依微服务版(SpringBoot/SpringCloudAlibaba)中在单个服务模块中进行单元测试](https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/128900845)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [若依框架如何进行单元测试](https://blog.csdn.net/qq_19309473/article/details/122147522)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值