Code Monkey home page Code Monkey logo

mock-localdate-now-java's Introduction

mock-LocalDate-now-java

How to mock the static LocalDate.now() call in java.

Example code

@RunWith(PowerMockRunner.class)
@PrepareForTest({LocalDate.class,PublicationValidator.class})
public class PublicationValidatorTest {

    private Validator validator;

    @Before
    public void setUp(){
        LocalDate date = LocalDate.of(2000, 1, 1);
        PowerMockito.stub(PowerMockito.method(LocalDate.class,"now")).toReturn(date);
        validator = new Validator();
    }

    @Test
    public void exampleTest() {
        result = validator.callWithCallToLocalDate()
        assertThat(result).isTrue();
    }
}

Explanation

@RunWith(PowerMockRunner.class) Will enable powermock for this test class

@PrepareForTest({LocalDate.class,Validator.class}) Include both the class to mock and the class using the mock

LocalDate date = LocalDate.of(2000, 1, 1); Decide which date should be used for the tests

PowerMockito.stub(PowerMockito.method(LocalDate.class,"now")).toReturn(date); This line will mock specifically the call to LocalDate.now() (other methods remain unaffected)

It is also possible to mock all calls by using PowerMockito.mockStatic(LocalDate.class) with a when().thenReturn() statement after.

Extra information

Remember to add PowerMock and the powermock mockito api to your classpath. Maven file:

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>2.0.4</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito2</artifactId>
    <version>2.0.4</version>
    <scope>test</scope>
</dependency>

mock-localdate-now-java's People

Contributors

aoriseth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

ig0revich

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.