{"id":16702621,"url":"https://github.com/szpak/mockito-java8","last_synced_at":"2025-03-17T01:30:52.541Z","repository":{"id":34295798,"uuid":"38196344","full_name":"szpak/mockito-java8","owner":"szpak","description":"Mockito add-ons leveraging Java 8 and lambda expressions to make mocking even more compact","archived":false,"fork":false,"pushed_at":"2021-06-21T05:16:25.000Z","size":375,"stargazers_count":79,"open_issues_count":9,"forks_count":11,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-02-27T15:38:11.472Z","etag":null,"topics":["java","java-8","mocking","mockito","testing"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/szpak.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-28T11:25:04.000Z","updated_at":"2024-03-31T14:16:22.000Z","dependencies_parsed_at":"2022-09-03T00:33:06.021Z","dependency_job_id":null,"html_url":"https://github.com/szpak/mockito-java8","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szpak%2Fmockito-java8","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szpak%2Fmockito-java8/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szpak%2Fmockito-java8/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/szpak%2Fmockito-java8/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/szpak","download_url":"https://codeload.github.com/szpak/mockito-java8/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243835942,"owners_count":20355611,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["java","java-8","mocking","mockito","testing"],"created_at":"2024-10-12T19:05:34.882Z","updated_at":"2025-03-17T01:30:51.931Z","avatar_url":"https://github.com/szpak.png","language":"Java","readme":"# Mockito-Java8 [![Build Status](https://travis-ci.org/szpak/mockito-java8.svg?branch=master)](https://travis-ci.org/szpak/mockito-java8) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/info.solidsoft.mockito/mockito-java8/badge.svg)](https://maven-badges.herokuapp.com/maven-central/info.solidsoft.mockito/mockito-java8)\n\nMockito add-ons leveraging Java 8 and lambda expressions to make mocking even more compact.\n\n## Quick start\n\n### Lambda matcher\n\nAllows for stubbing with matcher logic defined within a lambda expression. Useful when working with complex classes pass as an argument.\n\n```\ngiven(ts.findNumberOfShipsInRangeByCriteria(\n    argLambda(c -\u003e c.getMinimumRange() \u003e 1000))).willReturn(4);\n```\n\n### Argument Captor - Java 8 edition\n\nAllows to use ArgumentCaptor in one line (here with AssertJ):\n\n```\nverify(ts).findNumberOfShipsInRangeByCriteria(\n    assertArg(sc -\u003e assertThat(sc.getMinimumRange()).isLessThan(2000)));\n```\n\n### Mockito API methods available via interfaces (without static imports)\n\nAllows to use methods from Mockito API without the need to use static imports. It is enough to make your test class implement `WithBDDMockito`\ninterface to have all methods from Mockito API available directly (especially useful for Eclipse users).\n\n```\nclass SpaceShipTest implements WithBDDMockito {\n    //stub and verify as usual without static imports\n}\n```\n\nAvailable interfaces: [`WithBDDMockito`](https://github.com/szpak/mockito-java8/blob/master/src/main/java/info/solidsoft/mockito/java8/api/WithBDDMockito.java),\n[`WithMockito`](https://github.com/szpak/mockito-java8/blob/master/src/main/java/info/solidsoft/mockito/java8/api/WithMockito.java) and\n[`WithAdditionalMatchers`](https://github.com/szpak/mockito-java8/blob/master/src/main/java/info/solidsoft/mockito/java8/api/WithAdditionalMatchers.java).\n\n\n## Configuration in a project\n\nmockito-java8 jars are available in Maven Central.\n\n### Gradle\n\n```\ntestCompile 'info.solidsoft.mockito:mockito-java8:2.5.0'\n```\n\n### Maven\n\n```\n\u003cdependency\u003e\n    \u003cgroupId\u003einfo.solidsoft.mockito\u003c/groupId\u003e\n    \u003cartifactId\u003emockito-java8\u003c/artifactId\u003e\n    \u003cversion\u003e2.5.0\u003c/version\u003e\n    \u003cscope\u003etest\u003c/scope\u003e\n\u003c/dependency\u003e\n```\n\n### Other\n\nClick Maven Central badge [![Maven Central](https://maven-badges.herokuapp.com/maven-central/info.solidsoft.mockito/mockito-java8/badge.svg)](https://maven-badges.herokuapp.com/maven-central/info.solidsoft.mockito/mockito-java8) to get configuration snippets for SBT, Ivy and more.\n\n## Mockito compatibility\n\nMockito-Java8 has two development lines. Versions 1.x (and 0.3.x) should be compatible with Mockito 1.10.12+ and 2.0.x-beta up to 2.0.21-beta.\nVersions 2.x supports the new matchers API and should be compatible with Mockito 2.0.22-beta and newer versions.\n\nDocumentation for versions 1.x can be found in a separate [branch](https://github.com/szpak/mockito-java8/tree/1.x).\n\n## Provided add-ons\n\n### Lambda matcher\n\nAllows to define matcher logic within a lambda expression. Useful when working with complex classes pass as an argument.\n\n```\n@Immutable\nclass ShipSearchCriteria {\n    int minimumRange;\n    int numberOfPhasers;\n}\n```\n\n```\n@Test\npublic void shouldAllowToUseLambdaInStubbing() {\n    //given\n    given(ts.findNumberOfShipsInRangeByCriteria(\n        argLambda(c -\u003e c.getMinimumRange() \u003e 1000))).willReturn(4);\n    //expect\n    assertThat(ts.findNumberOfShipsInRangeByCriteria(new ShipSearchCriteria(1500, 2)))\n        .isEqualTo(4);\n    //expect\n    assertThat(ts.findNumberOfShipsInRangeByCriteria(new ShipSearchCriteria(700, 2)))\n        .isEqualTo(0);\n}\n```\n\nIn comparison the same logic implemented with a custom Answer in Java 7:\n\n```\n@Test\npublic void stubbingWithCustomAnswerShouldBeLonger() {  //old way\n    //given\n    given(ts.findNumberOfShipsInRangeByCriteria(any())).willAnswer(new Answer\u003cInteger\u003e() {\n        @Override\n        public Integer answer(InvocationOnMock invocation) throws Throwable {\n            Object[] args = invocation.getArguments();\n            ShipSearchCriteria criteria = (ShipSearchCriteria) args[0];\n            if (criteria.getMinimumRange() \u003e 1000) {\n                return 4;\n            } else {\n                return 0;\n            }\n        }\n    });\n    //expect\n    assertThat(ts.findNumberOfShipsInRangeByCriteria(new ShipSearchCriteria(1500, 2)))\n        .isEqualTo(4);\n    //expect\n    assertThat(ts.findNumberOfShipsInRangeByCriteria(new ShipSearchCriteria(700, 2)))\n        .isEqualTo(0);\n}\n```\n\nEven Java 8 and less readable constructions don't help too much:\n\n```\n@Test\npublic void stubbingWithCustomAnswerShouldBeLongerEvenAsLambda() {  //old way\n    //given\n    given(ts.findNumberOfShipsInRangeByCriteria(any())).willAnswer(invocation -\u003e {\n        ShipSearchCriteria criteria = (ShipSearchCriteria) invocation.getArguments()[0];\n        return criteria.getMinimumRange() \u003e 1000 ? 4 : 0;\n    });\n    //expect\n    assertThat(ts.findNumberOfShipsInRangeByCriteria(new ShipSearchCriteria(1500, 2)))\n        .isEqualTo(4);\n    //expect\n    assertThat(ts.findNumberOfShipsInRangeByCriteria(new ShipSearchCriteria(700, 2)))\n        .isEqualTo(0);\n}\n```\n\n### Argument Captor - Java 8 edition\n\nAllows to use ArgumentCaptor in one line:\n\n```\n@Test\npublic void shouldAllowToUseAssertionInLambda() {\n    //when\n    ts.findNumberOfShipsInRangeByCriteria(searchCriteria);\n    //then\n    verify(ts).findNumberOfShipsInRangeByCriteria(\n        assertArg(sc -\u003e assertThat(sc.getMinimumRange()).isLessThan(2000)));\n}\n```\n\nIn comparison to 3 lines in the classic way:\n\n```\n@Test\npublic void shouldAllowToUseArgumentCaptorInClassicWay() {  //old way\n    //when\n    ts.findNumberOfShipsInRangeByCriteria(searchCriteria);\n    //then\n    ArgumentCaptor\u003cShipSearchCriteria\u003e captor = \n        ArgumentCaptor.forClass(ShipSearchCriteria.class);\n    verify(ts).findNumberOfShipsInRangeByCriteria(captor.capture());\n    assertThat(captor.getValue().getMinimumRange()).isLessThan(2000);\n}\n```\n\n### Mockito API methods available via interfaces (without static imports)\n\nAllows to use methods from Mockito API without the need to use static imports. It is enough to make your test class implement `WithBDDMockito`\ninterface to have all methods from stubbing/mockito Mockito API available directly.\n\n```\n//no need to use static imports!\n\npublic class SpaceShipTest implements WithBDDMockito {\n\n    @Test\n    public void shouldVerifyMethodExecution() {\n        //given\n        TacticalStation tsSpy = spy(TacticalStation.class);\n        willDoNothing().given(tsSpy).fireTorpedo(2);\n        //when\n        tsSpy.fireTorpedo(2);\n        tsSpy.fireTorpedo(2);\n        //then\n        then(tsSpy).should(times(2)).fireTorpedo(2);\n    }\n}\n```\n\nThe same code would work fine with a bunch of static imports. Of course they can be hidden in IDE and usually do not disturb much. Nevertheless\nto be able to write just a method name (e.g. `mock(TacticalStation.class)`) without a class is it required to press ALT-ENTER (in IntelliJ IDEA)\nto add each static import on the first usage of a given method in a test class. The situation is even worse in Eclipse where it is\n[required](http://stackoverflow.com/a/290756/313516) to earlier add `BDDMockito` to \"Favorites\" in \"Content Assist\" to make it suggested by IDE.\n\nMockito methods are provided by 3 base interfaces, being an entry point for given set of methods:\n - [`WithBDDMockito`](https://github.com/szpak/mockito-java8/blob/master/src/main/java/info/solidsoft/mockito/java8/api/WithBDDMockito.java) - stubbing/mocking API in BDD style\n - [`WithMockito`](https://github.com/szpak/mockito-java8/blob/master/src/main/java/info/solidsoft/mockito/java8/api/WithMockito.java) - classic stubbing/mocking API\n - [`WithAdditionalMatchers`](https://github.com/szpak/mockito-java8/blob/master/src/main/java/info/solidsoft/mockito/java8/api/WithAdditionalMatchers.java) - additional matchers\n\n\n## Rationale\n\nMockito-Java8 is a side effect of my short presentation [Java 8 brings power to testing!](https://speakerdeck.com/szpak/java-8-brings-power-to-testing)\nwhich I gave at GeeCON TDD 2015 and DevConf.cz 2015. In my speech, using 4 examples, I showed how Java 8 - namely lambda expressions - can simplify testing tools.\n\n\n## Limitations\n\nUnfortunately there is a set of nice Java 8 related features which cannot be implemented without a non backward compatible changes in Mockito core and\ntherefore cannot be implemented as an add-on. A good news is that [Mockito 3.0](https://github.com/mockito/mockito/issues/297) is planned to require\nJava 8 making it all possible. \n\n\n## Java 9 compatibility\n\nThe project is automatically tested with Java 9 (and Java 10) in the CI environment. At least the base scenarios should work with Java 9. Feel free to report an [issue](https://github.com/szpak/mockito-java8/issues/) if you encounter any mockito-java8 specific problem.\n\nThe project's JAR artifact contains an Automatic-Module-Name manifest attribute. It's value - `info.solidsoft.mockito.mockito-java8` - is used\nas the name of the automatic module defined by that JAR file when it is placed on the Java 9 module path. This allows to explicitly require\nmockito-java8 in other projects.\n\nThe mockito-java8 dependencies itself will be declared explicitly once available in required projects.\n\n\n## Additional information \n\nmockito-java8 has been written by Marcin Zajączkowski. The author can be contacted directly via email: mszpak ATT wp DOTT pl.\nThere is also Marcin's blog available: [Solid Soft](http://blog.solidsoft.info/) - working code is not enough.\n\nmockito-java8 is a separate project and is NOT supported by The Mockito Core Team.\n\nThe library is licensed under the terms of [the Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0.txt).\n","funding_links":[],"categories":["Testing"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fszpak%2Fmockito-java8","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fszpak%2Fmockito-java8","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fszpak%2Fmockito-java8/lists"}