ソースを参照

chore[litemall-core]: 采用SyncTaskExecutor调度器来测试异步方法

Junling Bu 7 年 前
コミット
db0995d1d8

+ 17 - 6
litemall-core/src/test/java/org/linlinjava/litemall/core/MailTest.java

@@ -5,9 +5,16 @@ import org.junit.runner.RunWith;
 import org.linlinjava.litemall.core.notify.NotifyService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+import org.springframework.context.annotation.Primary;
+import org.springframework.core.task.SyncTaskExecutor;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.test.context.web.WebAppConfiguration;
 
+import java.util.concurrent.Executor;
+
 /**
  * 测试邮件发送服务
  *
@@ -23,18 +30,22 @@ import org.springframework.test.context.web.WebAppConfiguration;
 @SpringBootTest
 public class MailTest {
 
+    @Configuration
+    @Import(Application.class)
+    static class ContextConfiguration {
+        @Bean
+        @Primary
+        public Executor executor() {
+            return new SyncTaskExecutor();
+        }
+    }
+
     @Autowired
     private NotifyService notifyService;
 
     @Test
     public void testMail() {
         notifyService.notifyMail("订单信息", "订单1111111已付款,请发货");
-
-        try {
-            Thread.sleep(5000);
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-        }
     }
 
 

+ 17 - 24
litemall-core/src/test/java/org/linlinjava/litemall/core/SmsTest.java

@@ -6,9 +6,16 @@ import org.linlinjava.litemall.core.notify.NotifyService;
 import org.linlinjava.litemall.core.notify.NotifyType;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+import org.springframework.context.annotation.Primary;
+import org.springframework.core.task.SyncTaskExecutor;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.test.context.web.WebAppConfiguration;
 
+import java.util.concurrent.Executor;
+
 /**
  * 测试短信发送服务
  *
@@ -25,6 +32,16 @@ import org.springframework.test.context.web.WebAppConfiguration;
 @SpringBootTest
 public class SmsTest {
 
+    @Configuration
+    @Import(Application.class)
+    static class ContextConfiguration {
+        @Bean
+        @Primary
+        public Executor executor() {
+            return new SyncTaskExecutor();
+        }
+    }
+
     @Autowired
     private NotifyService notifyService;
 
@@ -34,12 +51,6 @@ public class SmsTest {
         String[] params = new String[] {"123456"};
 
         notifyService.notifySmsTemplate(phone, NotifyType.CAPTCHA, params);
-
-        try {
-            Thread.sleep(5000);
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-        }
     }
 
     @Test
@@ -48,12 +59,6 @@ public class SmsTest {
         String[] params = new String[] {"123456"};
 
         notifyService.notifySmsTemplate(phone, NotifyType.PAY_SUCCEED, params);
-
-        try {
-            Thread.sleep(5000);
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-        }
     }
 
     @Test
@@ -62,12 +67,6 @@ public class SmsTest {
         String[] params = new String[] {"123456"};
 
         notifyService.notifySmsTemplate(phone, NotifyType.SHIP, params);
-
-        try {
-            Thread.sleep(5000);
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-        }
     }
 
     @Test
@@ -76,11 +75,5 @@ public class SmsTest {
         String[] params = new String[] {"123456"};
 
         notifyService.notifySmsTemplate(phone, NotifyType.REFUND, params);
-
-        try {
-            Thread.sleep(5000);
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-        }
     }
 }