44import com .baeldung .temporal .workflows .hello .HelloWorkflowRegistrar ;
55import io .temporal .client .WorkflowClient ;
66import io .temporal .client .WorkflowOptions ;
7+ import io .temporal .serviceclient .WorkflowServiceStubs ;
8+ import io .temporal .serviceclient .WorkflowServiceStubsOptions ;
79import io .temporal .testing .TestWorkflowEnvironment ;
810import io .temporal .worker .Worker ;
11+ import io .temporal .workflow .Workflow ;
912import org .junit .jupiter .api .AfterEach ;
1013import org .junit .jupiter .api .BeforeEach ;
1114import org .junit .jupiter .api .Test ;
1215import org .slf4j .Logger ;
1316import org .slf4j .LoggerFactory ;
1417
18+ import java .util .Optional ;
1519import java .util .UUID ;
1620
1721import static org .junit .jupiter .api .Assertions .assertEquals ;
@@ -34,6 +38,17 @@ public void startWorker() {
3438 worker = testEnv .newWorker (QUEUE_NAME );
3539 HelloWorkflowRegistrar .newInstance ().register (worker );
3640 client = testEnv .getWorkflowClient ();
41+
42+ // var serviceStubs = WorkflowServiceStubs.newServiceStubs(WorkflowServiceStubsOptions
43+ // .newBuilder()
44+ // .setTarget("localhost:7233")
45+ // .setEnableKeepAlive(true)
46+ // .setEnableHttps(false)
47+ // .build());
48+ //
49+ // client = WorkflowClient.newInstance(serviceStubs);
50+
51+ testEnv .start ();
3752 }
3853
3954 @ AfterEach
@@ -42,10 +57,7 @@ public void stopWorker() {
4257 }
4358
4459 @ Test
45- void givenPerson_whenSayHello_thenSuccess () throws Exception {
46-
47- // We must register all activities/worklows before starting the test environment
48- testEnv .start ();
60+ void givenPerson_whenSayHelloAsync_thenSuccess () throws Exception {
4961
5062 // Create workflow stub wich allow us to create workflow instances
5163 var wfid = UUID .randomUUID ().toString ();
@@ -59,12 +71,33 @@ void givenPerson_whenSayHello_thenSuccess() throws Exception {
5971
6072 // Invoke workflow asynchronously.
6173 var execution = WorkflowClient .start (workflow ::hello ,"Baeldung" );
74+ var workflowStub = client .newUntypedWorkflowStub (execution .getWorkflowId ());
75+
76+ // Retrieve a CompletableFuture we can use to wait for the result.
77+ var future = workflowStub .getResultAsync (String .class );
78+ log .info ("Waiting for workflow to complete..." );
79+ var result = future .get ();
80+ log .info ("Workflow completed with result: {}" , result );
81+ assertEquals ("Hello, Baeldung" , result );
82+ }
83+
84+ @ Test
85+ void givenPerson_whenSayHelloSync_thenSuccess () throws Exception {
6286
63- // Create a blocking workflow using tbe execution's workflow id
64- var syncWorkflow = client .newWorkflowStub (HelloWorkflow .class ,execution .getWorkflowId ());
87+ // Create workflow stub wich allow us to create workflow instances
88+ var wfid = UUID .randomUUID ().toString ();
89+ var workflow = client .newWorkflowStub (
90+ HelloWorkflow .class ,
91+ WorkflowOptions .newBuilder ()
92+ .setTaskQueue (QUEUE_NAME )
93+ .setWorkflowId (wfid )
94+ .build ()
95+ );
6596
97+ // Invoke workflow synchronously.
98+ var result = workflow .hello ("Baeldung" );
6699
67100 // The sync workflow stub will block until it completes. Notice that the call argumento here is ignored!
68- assertEquals ("Hello, Baeldung" , syncWorkflow . hello ( "ignored" ) );
101+ assertEquals ("Hello, Baeldung" , result );
69102 }
70103}
0 commit comments