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 8109847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:32:50+00:00 2026-06-06T01:32:50+00:00

What would be the best way to something like this where a timestamp is

  • 0

What would be the best way to something like this where a timestamp is updated automatically before making the mocked call?

Here is some dummy code of what I am trying to test:

public class ThingWithATimestamp {
    public Long timestamp;
    public String name;

    public ThingWithATimestamp(String name) {
        this.name = name;
    }
}

public class TheClassThatDoesStuff {
    private ThingConnector connector;

    public TheClassThatDoesStuff(ThingConnector connector) {
        this.connector = connector;
    }

    public void updateTheThing(MyThingWithATimestamp thing) {
        thing.timestamp = currentTimestamp();
        connector.update(thing);            
    }
}

Here is what I want to test:

public class TheClassThatDoesStuffTests {
    @Test
    public void canUpdateTheThing() {
        ThingConnector connector = mock(ThingConnector.class);
        TheClassThatDoesStuff doer = new ThisClassThatDoesStuff(connector);

        doer.updateTheThing(new ThingWithATimestamp("the name"));

        verify(connector, times(1)).update(SomeMatcherThatICantFigureOut);
    }

I know this code is pretty dumbed down but I think it accurately portrays what I am trying to verify. I basically need a matcher to fill in the test to verify that the timestamp is within X of the current time so I know it got updated correctly and that connector.update was called with the proper timestamp for the object.

  • 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-06T01:32:54+00:00Added an answer on June 6, 2026 at 1:32 am

    I find the most robust way to deal with time-critical code is to wrap up all of your time-critical functions in their own class. I usually call it TimeHelper. So this class might look like the following.

    import java.util.Date;
    public class TimeHelper{
        public long currentTimeMillis(){
            return System.currentTimeMillis();
        }
        public Date makeDate(){
            return new Date();
        }
    }
    

    and it might have more methods of the same type. Now, any class that uses such functions should have (at least) two constructors – the normal one that you’ll use in your application, plus a package-private one in which a TimeHelper is a parameter. This TimeHelper needs to be stored away for later use.

    public class ClassThatDoesStuff {
        private ThingConnector connector;
        private TimeHelper timeHelper;
    
        public ClassThatDoesStuff(ThingConnector connector) {
            this(connector, new TimeHelper());
        }
    
        ClassThatDoesStuff(ThingConnector connector, TimeHelper timeHelper) {
            this.connector = connector;
            this.timeHelper = timeHelper;
        } 
    }
    

    Now, within your class, instead of writing System.currentTimeMillis(), write timeHelper.currentTimeMillis(). This will, of course, have exactly the same effect; except now, your class has magically become much more testable.

    When you test your class, make a mock of TimeHelper. Configure this mock (using Mockito’s when and thenReturn, or alternatively doReturn) to return any time values you like – whatever you need for your test. You can even return multiple values here, if you’re going to have multiple calls to currentTimeMillis() in the course of the test.

    Now use the second constructor to make the object that you’re going to test, and pass in the mock. This gives you perfect control of what time values will be used in the test; and you can make your assertions or verifications assert that precisely the right value has been used.

    public class ClassThatDoesStuffTest{
        @Mock private TimeHelper mockTime;
        @Mock private ThingConnector mockConnector;
        private ClassThatDoesStuff toTest;
    
        @Test
        public void doesSomething(){
            // Arrange
            initMocks(this);
            when(mockTime.currentTimeMillis()).thenReturn(1000L, 2000L, 5000L);
            toTest = new ClassThatDoesStuff(mockConnector, mockTime);
    
            // Act
            toTest.doSomething();
    
            // Assert
            // ... ???
        }
    }        
    

    If you do this, you know that your test will always work, and never be dependent on the time slicing policies of your operating system. You also have the power to verify the exact values of your timestamps, rather than asserting that they fall within some approximate interval.

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

Sidebar

Related Questions

I was wondering what would be the best way to accomplish something like this...
What would be the best way to accomplish something like this? Suppose I have
I'm trying to figure out the best way to do something like this. Basically
I would like to know the best way to set the values for multiple
postgres 9.2 supports json columns. what would be best way to extend postgres to
I want to make a code snippet database web application. Would the best way
What would be the best way to display a dialog whenever my app receives
What would be the best way to version a rails application? We want to
What would be the best way to properly concatenate string with an url inside?
What would be the best way and more idiomatic to break a string into

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.