You can look at the following samples:
- Terminate via api: samples-java/src/main/java/io/temporal/samples/terminateworkflow at master · temporalio/samples-j and associated test:
samples-java/src/test/java/io/temporal/samples/terminateworkflow at master · temporalio/samples-j - Cancellation: samples-java/HelloCancellationScope.java at master · temporalio/samples-java · GitHub and samples-java/HelloDetachedCancellationScope.java at master · temporalio/samples-java · GitHub
I would also consider reading this thread to learn more about cancellation.
Note that you can canel/terminate workflows also via tctl - Temporal CLI - tctl | Temporal documentation