测试类编写

This commit is contained in:
2022-01-24 23:52:29 +08:00
parent ca65b5e8fc
commit 0c074c046f
11 changed files with 215 additions and 24 deletions

View File

@@ -12,6 +12,15 @@
<artifactId>test</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.hxuanyu</groupId>
<artifactId>notify-spring-boot-starter</artifactId>
@@ -20,13 +29,19 @@
<groupId>com.hxuanyu</groupId>
<artifactId>common-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<groupId>com.hxuanyu</groupId>
<artifactId>monitor-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<groupId>com.hxuanyu</groupId>
<artifactId>network-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

@@ -4,11 +4,10 @@ import com.hxuanyu.common.message.Msg;
import com.hxuanyu.notify.model.Mail;
import com.hxuanyu.notify.service.MailService;
import com.hxuanyu.notify.service.NotifyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@@ -22,12 +21,12 @@ import javax.annotation.Resource;
@RestController
public class TestController {
private final Logger logger = LoggerFactory.getLogger(TestController.class);
@Resource
MailService mailService;
@RequestMapping("/testMail")
public Msg<Mail> testMail(){
public Msg<Mail> testMail() {
Mail mail = new Mail();
mail.setContent("邮件内容");
mail.setSubject("邮件主题");
@@ -41,4 +40,33 @@ public class TestController {
return Msg.success("发送邮件成功", mail);
}
@ResponseBody
@GetMapping("/")
public Msg<String> testGet() {
logger.info("收到GET请求");
return Msg.success("GET 测试");
}
@ResponseBody
@PostMapping("/")
public Msg<String> testPost() {
logger.info("收到POST请求");
return Msg.success("POST 测试");
}
@ResponseBody
@PutMapping("/")
public Msg<String> testPut() {
logger.info("收到PUT请求");
return Msg.success("PUT 测试");
}
@ResponseBody
@DeleteMapping("/")
public Msg<String> testDelete() {
logger.info("收到DELETE请求");
return Msg.success("DELETE 测试");
}
}

View File

@@ -0,0 +1,20 @@
package com.hxuanyu.test.monitor;
import com.hxuanyu.monitor.annotation.MonitorItem;
import com.hxuanyu.monitor.base.BaseMonitorItem;
import com.hxuanyu.monitor.common.CheckResult;
import com.hxuanyu.notify.enums.NotifyType;
/**
* 定时任务监控测试
*
* @author hxuanyu
*/
@MonitorItem(cron = "0/5 * * * * *")
public class TestMonitorItem extends BaseMonitorItem {
@Override
public CheckResult check() {
return CheckResult.triggered("Hello", NotifyType.TYPE_LOG);
}
}

View File

@@ -0,0 +1,22 @@
package com.hxuanyu.starter.test.common;
import com.hxuanyu.common.message.Msg;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MsgTest {
private final Logger logger = LoggerFactory.getLogger(MsgTest.class);
@Test
public void test() {
Msg<String> msg = Msg.success("测试内容", "测试体");
logger.info(msg.toString());
assert msg.isSuccess();
msg = Msg.failed("失败消息");
logger.info(msg.toString());
assert msg.isFailed();
}
}

View File

@@ -0,0 +1,25 @@
package com.hxuanyu.starter.test.monitor;
import com.hxuanyu.monitor.base.BaseMonitorItem;
import com.hxuanyu.monitor.manager.MonitorItemBeanManager;
import com.hxuanyu.test.MainApplication;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = MainApplication.class)
public class MonitorTest {
private final Logger logger = LoggerFactory.getLogger(MonitorTest.class);
@Autowired
MonitorItemBeanManager monitorItemBeanManager;
@Test
public void testMonitorItemScan() {
BaseMonitorItem testMonitorItem = monitorItemBeanManager.getMonitorItemMap().get("ScheduledTask-TestMonitorItem");
assert testMonitorItem.getMonitorItemName() != null;
logger.info("获取到的Bean{}", testMonitorItem.toString());
}
}

View File

@@ -0,0 +1,56 @@
package com.hxuanyu.starter.test.network;
import com.hxuanyu.common.message.Msg;
import com.hxuanyu.network.service.HttpService;
import com.hxuanyu.test.MainApplication;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.web.WebAppConfiguration;
import static java.lang.Thread.sleep;
@SpringBootTest(classes = MainApplication.class)
public class NetworkTest {
@Autowired
HttpService httpService;
private final Logger logger = LoggerFactory.getLogger(NetworkTest.class);
@Test
@Timeout(3000)
public void testGet() throws InterruptedException {
Msg<String> msg = httpService.doGet("https://baidu.com");
logger.info("GET测试{}", msg);
assert msg.isSuccess();
}
@Test
@Timeout(3000)
public void testPost() {
Msg<String> msg = httpService.doPost("https://baidu.com");
logger.info("POST测试{}", msg);
assert msg.isSuccess();
}
@Test
@Timeout(3000)
public void testDelete() {
Msg<String> msg = httpService.doDelete("https://baidu.com");
logger.info("DELETE测试{}", msg);
assert msg.isSuccess();
}
@Test
@Timeout(3000)
public void testPut() {
Msg<String> msg = httpService.doPut("https://baidu.com");
logger.info("PUT测试{}", msg);
assert msg.isSuccess();
}
}

View File

@@ -0,0 +1,27 @@
package com.hxuanyu.starter.test.notify;
import com.hxuanyu.notify.enums.NotifyType;
import com.hxuanyu.notify.model.Mail;
import com.hxuanyu.notify.service.NotifyService;
import com.hxuanyu.test.MainApplication;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static java.lang.Thread.sleep;
@SpringBootTest(classes = MainApplication.class)
public class NotifyTest {
@Autowired
NotifyService notifyService;
@Test
public void testNotify() throws InterruptedException {
notifyService.notify(new Mail("2252193204@qq.com", "test subject", "test success"), NotifyType.TYPE_MAIL);
sleep(2000);
notifyService.notify("短信通知方式", NotifyType.TYPE_MSG);
notifyService.notify("日志输出方式", NotifyType.TYPE_LOG);
}
}