반응형

Spring boot 이용해서 Scheduler 를 작업할 때, 부분적인 단위 테스트를 하기가 어렵습니다.


내가 원하는 Job만 선택적으로 실행할 수 있는 테스트 코드입니다.


도움이 되시길 바랍니다.


-- SearchScheduler --

1
2
3
4
5
6
7
8
@Component
public class SearchScheduler {
 
  @Scheduled(fixedDelay = 3000)
  public void searchTest() {
 
  }
}


-- StatScheduler --

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Component
public class StatScheduler {
  private static Logger prettyLogger = LoggerFactory.getLogger(Constants.PRETTY_LOGGER_NAME);
 
  // cron 문법 참조
  // http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger
  @Scheduled(cron = "*/3 * * * * *")
  public void test3() {
    PrettyLog prettyLog = new PrettyLogImpl("StatScheduler.test3-클래스명.메서드명");
    // TODO
    prettyLog.stop();
    prettyLogger.info(prettyLog.prettyPrint());
  }
 
  @Scheduled(cron = "* * * * * *")
  public void test1() {
    PrettyLog prettyLog = new PrettyLogImpl("StatScheduler.test1");
    prettyLog.append("When""1초마다 돌지요");
    prettyLog.stop();
    prettyLogger.info(prettyLog.prettyPrint());
  }
}



-- 테스트 코드 --

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.IntervalTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.ScheduledMethodRunnable;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
 
 
@RunWith(SpringRunner.class)
@SpringBootTest
public class StatSchedulerTest {
 
  @Test
  public void test() {
    PrettyLog prettyLog = new PrettyLogImpl("test");
    try {
      TimeUnit.SECONDS.sleep(10);
    } catch (Exception e) {
      if (e != null) {
        prettyLog.append("ERROR", e.toString());
      }
      e.printStackTrace();
    } finally {
      prettyLog.stop();
      System.out.println(prettyLog.prettyPrint());
    }
  }
 
  @TestConfiguration
  static class SchedulingTestConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
      PrettyLog prettyLog = new PrettyLogImpl("SchedulingTestConfig");
      List<IntervalTask> taskList = taskRegistrar.getFixedDelayTaskList();
      List<CronTask> cronTaskList = taskRegistrar.getCronTaskList();
      List<IntervalTask> newTaskList = new ArrayList<>();
      List<CronTask> newCronTaskList = new ArrayList<>();
 
      prettyLog.start("IntervalTask");
      for (IntervalTask task : taskList) {
        ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
        String methodName = runnable.getMethod().getName();
        Object target = runnable.getTarget();
 
        if (target instanceof SearchScheduler) {
          if (methodName.equals("searchTest")) {
            prettyLog.append("method",
                target.getClass().getSimpleName() + "." + methodName + "(" + task.getInterval() + "ms)");
            newTaskList.add(new IntervalTask(runnable, task.getInterval(), task.getInitialDelay()));
          }
        }
      }
      prettyLog.stop();
 
      prettyLog.start("CronTask");
      for (CronTask task : cronTaskList) {
        ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
        Method method = runnable.getMethod();
        String methodName = method.getName();
        Object target = runnable.getTarget();
        if (target instanceof StatScheduler) {
          if (methodName.equals("test1")) {
            prettyLog.append("method",
                target.getClass().getSimpleName() + "." + methodName + "(" + task.getExpression() + ")");
            newCronTaskList.add(new CronTask(runnable, task.getExpression()));
          }
        }
      }
      prettyLog.stop();
 
      taskRegistrar.setFixedDelayTasksList(newTaskList);
      taskRegistrar.setCronTasksList(newCronTaskList);
      prettyLog.stop();
      System.out.println(prettyLog.prettyPrint());
    }
  }
}




-- 설정 부분 로그 --


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
+---------------------------------------------------
|START                : 2037-09-19 04:54:33,195
|ID                   : SchedulingTestConfig
| +---------------------------------------------------
| |START                : 2037-09-19 04:54:33,196
| |ID                   : IntervalTask
| |method               : SearchScheduler.searchTest(3000ms)
| |ELAPSED              : 0 ms
| +---------------------------------------------------
| +---------------------------------------------------
| |START                : 2037-09-19 04:54:33,201
| |ID                   : CronTask
| |method               : StatScheduler.test1(* * * * * *)
| |ELAPSED              : 0 ms
| +---------------------------------------------------
|ELAPSED              : 13 ms
+---------------------------------------------------
cs


반응형

+ Recent posts