Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8282955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:33:51+00:00 2026-06-08T10:33:51+00:00

I have an async method I am converting to a sync method using a

  • 0

I have an async method I am converting to a sync method using a countdown latch. I’m struggling with writing a unit test without using mockito’s timeout function. I can’t work out how to get the verify method to wait for the async method call:

public interface SyncExchangeService {
    boolean placeOrder(Order order);
}
public interface ExchangeService {
    void placeOrder(Order order, OrderCallback orderResponseCallback);
}

public interface OrderCallback {
    public void onSuccess();
    public void onFailure();
}



public class SyncExchangeServiceAdapter implements SyncExchangeService {
    private ExchangeService exchangeService;

    public SyncExchangeServiceAdapter(ExchangeService exchangeService) {
        this.exchangeService = exchangeService;
    }

    @Override
    public boolean placeOrder(Order order) {

        final CountDownLatch countdownLatch=new CountDownLatch(1);
        final AtomicBoolean result=new AtomicBoolean();
        exchangeService.placeOrder(order, new OrderCallback() {

            @Override
            public void onSuccess() {
                result.set(true);
                countdownLatch.countDown();
            }

            @Override
            public void onFailure(String rejectReason) {
                result.set(false);
                countdownLatch.countDown();
            }
        });
        try {
            countdownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return result.get();
    }
}


public class SyncExchangeServiceAdapterTest {
    private ExchangeService mockExchange=mock(ExchangeService.class);
    private SyncExchangeServiceAdapter adapter=new SyncExchangeServiceAdapter(mockExchange);
    private Boolean response;
    private ArgumentCaptor<Boolean> callback=CaptorArgumentCaptor.forClass(OrderCallback.class);
    private CountDownLatch latch=new CountDownLatch(1);


    @Test
    public void testPlaceOrderWithSuccess() throws Exception {
        final Order order=mock(Order.class);
         Executors.newSingleThreadExecutor().submit(new Runnable() {
            @Override
            public void run() {
                response=adapter.placeOrder(order);
                latch.countDown();
            }
        });
            verify(mockExchange,timeout(10) ).placeOrder(eq(order), callbackCaptor.capture());
//the timeout method is not really recommended and could also fail randomly if the thread takes more than 10ms


        callbackCaptor.getValue().onSuccess();
        latch.await(1000,TimeUnit.MILLISECONDS);
            assertEquals(true,response);
    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:33:52+00:00Added an answer on June 8, 2026 at 10:33 am

    For these kinds of tests I like to use a little library called awaitility. You can do it yourself with a countdown latch, but as you have seen you have to hack up your test with a machete to make that work.

    In this test you should call verify after you have awaited the latch.

    Another problem in your code is the private Boolean response. Since you’re changing it in another thread you should make that an AtomicBoolean or at least declare it volatile.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When writing async method implementations using the BeginInvoke/EndInvoke pattern the code might look something
I have some async method public static Task<JObject> GetUser(NameValueCollection parameters) { return CallMethodApi(users.get, parameters,
I have an Async method returning a Task. I also wish to offer a
Why have the calling thread walk into the async method until the inner 'await'?
I have a receive callback from an async client method that is supposed to
I have a public async Task Foo() method that I want to call from
So I have a async Web Request method with a callback: private void DoWebRequest(string
I have a service hosted in a WPF application with an async method with
I know how to make Async methods but say I have a method that
I have a tcp server that uses TCPListener and the async method BeginAcceptTCPClient: Imports

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.