22
33import com .baeldung .temporal .workflows .hello .HelloWorkflow ;
44import com .baeldung .temporal .workflows .hello .HelloWorkflowRegistrar ;
5- import com .baeldung .temporal .workflows .hellov2 .HelloWorkflowV2 ;
65import io .temporal .client .WorkflowClient ;
76import io .temporal .client .WorkflowOptions ;
87import io .temporal .serviceclient .WorkflowServiceStubs ;
98import io .temporal .worker .Worker ;
109import io .temporal .worker .WorkerFactory ;
11- import io .temporal .worker .WorkerOptions ;
1210import org .junit .jupiter .api .AfterEach ;
1311import org .junit .jupiter .api .BeforeEach ;
1412import org .junit .jupiter .api .Test ;
1513import org .slf4j .Logger ;
1614import org .slf4j .LoggerFactory ;
1715
1816import java .util .UUID ;
17+ import java .util .concurrent .ExecutionException ;
1918
2019import static org .junit .jupiter .api .Assertions .assertEquals ;
2120
@@ -52,8 +51,8 @@ public void stopWorker() {
5251 @ Test
5352 void givenPerson_whenSayHello_thenSuccess () {
5453
55- WorkflowServiceStubs service = WorkflowServiceStubs .newLocalServiceStubs ();
56- WorkflowClient client = WorkflowClient .newInstance (service );
54+ var service = WorkflowServiceStubs .newLocalServiceStubs ();
55+ var client = WorkflowClient .newInstance (service );
5756
5857 var wfid = UUID .randomUUID ().toString ();
5958
@@ -68,9 +67,35 @@ void givenPerson_whenSayHello_thenSuccess() {
6867 // Run the workflow synchronously
6968 var result = workflow .hello ("Baeldung" );
7069 assertEquals ("Hello, Baeldung" , result );
71-
7270 }
7371
72+ @ Test
73+ void givenPerson_whenSayHelloAsync_thenSuccess () throws ExecutionException , InterruptedException {
74+
75+ var service = WorkflowServiceStubs .newLocalServiceStubs ();
76+ var client = WorkflowClient .newInstance (service );
77+
78+ var wfid = UUID .randomUUID ().toString ();
79+
80+ var workflow = client .newWorkflowStub (
81+ HelloWorkflow .class ,
82+ WorkflowOptions .newBuilder ()
83+ .setTaskQueue (QUEUE_NAME )
84+ .setWorkflowId (wfid )
85+ .build ()
86+ );
87+
88+ var execution = WorkflowClient .start (workflow ::hello ,"Baeldung" );
89+
90+ var workflowStub = client .newUntypedWorkflowStub (execution .getWorkflowId ());
7491
92+ // Retrieve a CompletableFuture we can use to wait for the result.
93+ var future = workflowStub .getResultAsync (String .class );
94+ log .info ("Waiting for workflow to complete..." );
95+ var result = future .get ();
96+ log .info ("Workflow completed with result: {}" , result );
97+ assertEquals ("Hello, Baeldung" , result );
98+
99+ }
75100
76101}
0 commit comments