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

  • Home
  • SEARCH
  • 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 3306502
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:17:45+00:00 2026-05-17T21:17:45+00:00

I have a JUnit test case where I’m expecting a particular method call to

  • 0

I have a JUnit test case where I’m expecting a particular method call to take a long time (over a minute). I want to

  1. Make the method call.
  2. Make sure the method call takes at least a minute and have a JUnit assertion fail if it doesn’t.
  3. Then kill the method call (assuming it took more than a minute as it should) because it could take a really long time.

How do I do this?

  • 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-05-17T21:17:46+00:00Added an answer on May 17, 2026 at 9:17 pm

    You can write a class implementing runnable that wraps around the method of interest; assuming spawning threads is allowed.

    public class CallMethod implements Runnable
    {
       //time in milli
       public long getStartTime()
       {
         return startTime;
       }
    
       //time in milli
       public long getEndTime()
       {
         return endTime;
       }
    
       public void run()
       {
           startTime = ...;
           obj.longRunningMethod();
           endTime = ...;
       }
    }
    

    Then in your JUnit you can do something like:

    public void testCase1()
    {
      CallMethod call = new CallMethod();
      Thread t = new Thread(call);
      t.start();
      t.join(60000); // wait one minute
    
      assertTrue(t.alive() || call.getEndTime() - call.getStartTime() >= 60000);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JUnit 4 test case with the Spring @Transactional annotation that saves
Time elapsed (time taken?) to run(and pass) my Junit test case is shown as
I have the following JUnit test. The method that I'm testing is quite simple,
I have a JUnit test that tests adding Strings to a Dictionary custom type.
I have a JUnit test that is checking to make sure that a customized
Class under test MyClass.java JUnit test case name alternatives: TestMyClass.java MyClassTest.java http://moreunit.sourceforge.net seems to
Can we have Junit Test cases for testing Proxy, Business Services in Oracle Service
I have a JUnit 3.x TestCase which I would like to be able to
I am using Eclipse IDE and JUnit for unit testing. I have given 12
I have a method that is going to the DB so all our JDBC

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.