泛微E9单元测试配置及使用教程

前言

编写目的:

为了帮助大家在不启动resin服务的前提下,进行一些二开内容的测试和验证,尽可能的利用单元测试等功能实现在IDEA中便可以进行后端代码的一些测试,从而节省掉反复重启resin服务的不必要的时间浪费。

1、前提条件

  • 将gitee中的项目拉取到本地IDEA中。

  • gitee地址:https://gitee.com/llhtrunk/ecology_dycs/tree/master

  • 配置好必要的ecology/classbean,WEB-INF/lib类库。

  • 保证可以正常引用系统中引用的类库和系统类进行开发和正常编译。

2、修改配置文件

2.1、修改数据库连接配置

修改为实际要连接的ecology数据库

image-20220809140556996

2.2、修改log4j

将log4j中的log日志文件路径修改为绝对路径的具体位置

image-20220809140933740

3、添加junit依赖

image-20220809141156987

4、测试类

测试查询数据库操作是否生效。

package com.weavernorth;

import org.junit.Before;
import org.junit.Test;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.hrm.User;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @Classname Test
 * @Description TODO
 * @Version 1.0.0
 * @Date 2022/7/21 0021 16:45
 * @Created by llh
 */
public class TestAction {
    
    

    @Before
    //1、设置好根路径
    public void before() throws Exception {
    
    
        GCONST.setServerName("ecology");
        GCONST.setRootPath("D:\\mywork\\Ecology9单元测试\\ecology\\");
        String hostname = "Unknown";
        try {
    
    
            InetAddress addr = InetAddress.getLocalHost();
            hostname = addr.getHostName();
        } catch (UnknownHostException ex) {
    
    
            System.out.println("Hostname can not be resolved");
        }
    }
    @Test
    //2、执行测试
    public void test() {
    
    
        BaseBean baseBean = new BaseBean();
      
        RecordSet rs = new RecordSet();
        //执行数据库查询
        rs.executeQuery("select *  from hrmresource ");
        while (rs.next()) {
    
    
            System.out.println("lastname = " + rs.getString("lastname"));
        }
        //读取配置文件
        String weaver = rs.getPropValue("weaver", "ecology.url");
        //打印日志
        baseBean.writeLog("weaver", weaver);
        //构建用户
        User user = new User(1);
        //读取用户信息
        String loginid = user.getLoginid();
        System.out.println("loginid = " + loginid);
    }
}

代码运行结果:
image-20220809141833879

5、利用系统的邮件配置发送邮件

邮件群发设置好发送服务

image-20220810092144123

@Test
public void test2() throws Exception {
    
    
    //收件人,邮箱地址,多地址时用英文逗号分隔
    String sendTo = "[email protected]";
    //邮件标题
    String emailTitle = "这是个标题";
    //邮件内容
    String emailInfo = "这是邮件内容";
    //邮件附件ids
    String emailFileIds = "";
    EmailWorkRunnable ewr = new EmailWorkRunnable(sendTo, emailTitle, emailInfo);
    ewr.setImagefileids(emailFileIds.toString());
    //result:true投递成功,false投递失败
    boolean emailSendResult = ewr.emailCommonRemind();
    System.out.println("emailSendResult = " + emailSendResult);
}

发送成功的截图

image-20220810092320013

6、发送消息

目前测试可以发送成功,但是在ecology对应用户中未看到对应消息,未找到原因。

@Test
public void testMessages() throws Exception {
    
    
    // 消息来源
    MessageType messageType = MessageType.newInstance(100);
    // 接收人id 必填
    Set<String> userIdList = new HashSet<>();
    userIdList.add("4");
    // 标题
    String title = "标题";
    // 内容
    String context = "内容";
    // PC端链接
    String linkUrl = "https://www.baidu.com";
    // 移动端链接
    String linkMobileUrl = "https://m.baidu.com";
    try {
    
    
        MessageBean messageBean = Util_Message.createMessage(messageType, userIdList, title, context, linkUrl, linkMobileUrl);
        // 创建人id
        messageBean.setCreater(1);
        // 需要修改消息为已处理等状态时传入,表示消息最初状态为待处理
        //message.setBizState("0");
        //消息来源code +“|”+业务id需要修改消息为已处理等状态时传入
        //messageBean.setTargetId("121|22");
        boolean store = Util_Message.store(messageBean);
        new BaseBean().writeLog("消息提醒结果=" + store);
    } catch (IOException e) {
    
    
        e.printStackTrace();
    }
}

猜你喜欢

转载自blog.csdn.net/u010048119/article/details/129579004