{"id":28135775,"url":"https://github.com/wesleyegberto/vscode-java-tests","last_synced_at":"2025-05-14T15:20:18.012Z","repository":{"id":39656855,"uuid":"286154507","full_name":"wesleyegberto/vscode-java-tests","owner":"wesleyegberto","description":"VS Extension to help write tests in Java using JUnit 4/5 with Hamcrest and Mockito","archived":false,"fork":false,"pushed_at":"2024-07-21T16:33:03.000Z","size":170,"stargazers_count":5,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-07-21T18:04:18.623Z","etag":null,"topics":["java","snippets","testing-tools","vscode-extension"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wesleyegberto.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-09T02:41:24.000Z","updated_at":"2023-02-01T13:01:07.000Z","dependencies_parsed_at":"2024-07-21T18:06:51.085Z","dependency_job_id":null,"html_url":"https://github.com/wesleyegberto/vscode-java-tests","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wesleyegberto%2Fvscode-java-tests","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wesleyegberto%2Fvscode-java-tests/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wesleyegberto%2Fvscode-java-tests/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wesleyegberto%2Fvscode-java-tests/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wesleyegberto","download_url":"https://codeload.github.com/wesleyegberto/vscode-java-tests/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254170122,"owners_count":22026223,"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","snippets","testing-tools","vscode-extension"],"created_at":"2025-05-14T15:19:52.057Z","updated_at":"2025-05-14T15:20:17.989Z","avatar_url":"https://github.com/wesleyegberto.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# VSCode Java Tests\n\n[![github-license-badge]][github-license]\n[![github-badge]][github]\n\nExtension to help write tests in Java using JUnit 4/5 with Hamcrest and Mockito.\n\nFeatures:\n\n* Snippets to add dependencies/plugins of test libs:\n  * JUnit 5 and Vintage\n  * [JsonPath](https://github.com/json-path/JsonPath): Java DSL to facilitate the creation of JSON matchers\n  * [ArchUnit](https://github.com/TNG/ArchUnit): Java DSL to test project architecture\n  * [JaCoCo](https://www.jacoco.org/jacoco/): Code coverage report\n  * [Pitest](https://github.com/hcoles/pitest): Mutation test\n  * [Testcontainers](https://www.testcontainers.org/)\n* Snippets to write tests structures, assertions, mocks, so on\n* Generate/open a test class for a given class\n* Generate/open a class using its qualified name (like IntelliJ IDEA)\n\nIt generates the test class with configuration to run with JUnit 4 or 5 (depending on the option `javaTests.template.junitDefaultVersion` in VS Code).\n\nI also recommend install Microsoft's [Java Test Runner](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-test)\n\n## How to Use\n\n### Commands\n\n| Command | Action | Description |\n| - | - | - |\n| `java.tests.createTestClass` | `Create/Open Test File` | Generate or open a test class |\n| `java.tests.createNewClass` | `Create new class` | Generate or open a class from its qualified name |\n\n### Snippets\n\n#### Java\n\n| Prefix | Description |\n| - | - |\n| `imports_junit4` | Insert the imports for JUnit 4 (Mockito, Hamcrast and JUnit) |\n| `imports_junit5` | Insert the imports for JUnit 5 (Mockito, Hamcrast and JUnit) |\n| `test_before` | Create setup method with `@Before` |\n| `test_after` | Create tear down method with `@After` |\n| `test_equals` | `assertEquals` |\n| `test_is` | `assertThat` with `is` |\n| `test_null` | `assertThat` with `nullValue` |\n| `test_not_null` | `assertThat` with `notNullValue` |\n| `test_nullorempty` | `assertThat` with `emptyOrNullString` |\n| `test_not_nullorempty` | `assertThat` with `not(emptyOrNullString)` |\n| `test_isOneOf` | `assertThat` with `isOneOf` |\n| `test_hasSize` | `assertThat` with `hasSize` |\n| `test_hasItem` | `assertThat` with `hasItem` |\n| `test_hasItems` | `assertThat` with `hasItems` |\n| `test_isIn` | `assertThat` with `isIn` |\n| `mock_class` | Create a mock object of a class |\n| `mock_method_return` | Mock a method's return |\n| `mock_method_throw` | Mock a method to throw exception |\n| `mock_verify_only` | Verify if a mocked method was the only one called |\n| `mock_verify_once` | Verify if a mocked method was called only once |\n| `mock_verify_times` | Verify if a mocked method was called `n` times |\n| `mock_verify_never` | Verify if a mocked method was never called |\n| `mock_arg_capture` | Capture an argument given to a mocked method using `ArgumentCaptor` |\n| `test_exception` | Assertion to verify if an exception was thrown (only JUnit 5) |\n| `test_parameterized` | Create a parameterized test (only JUnit 5) |\n\n#### POM.xml\n\nSnippets for Maven.\n\n| Prefix | Description |\n| - | - |\n| `junit5-props` | JUnit 5 verson properties |\n| `junit5-deps` | JUnit 5 dependencies |\n| `junit5-vintage` | JUnit 5 Vintage dependency to run tests from JUnit 3/4 |\n| `surefire-dep` | Surefire Maven dependency |\n| `failsafe-dep` | Failsafe Maven dependency |\n| `archunit-junit5-dep` | ArchUnit dependency to run with Junit 5 |\n| `jsonpath-dep` | JsonPath dependency to facilitate the creation of matchers for JSON |\n| `jacoco-plugin` | Maven plugin for JaCoCo Code Coverage Library |\n| `pitest-plugin` | Maven plugin for mutation test with Pitest |\n| `testcontainers-dep` | Testcontainer dependencies |\n| `testcontainers-dep-management` | Testcontainers Maven dependecy management |\n\n## Actions\n\n### Create Test File\n\nGenerate a test class in the folder `src/test/java` with code to construct the source class, if already there is a test\nclass then it will be opened.\n\nGenerate the test class with the following structure:\n\n* All the types that are used by target Java class are imported (improvements pending)\n* Define the instance to test using its **first** non-private constructor\n* Define an attribute annotated with `@Mock` for each parameter from the **first** non-private constructor\n* Define a test case for each public method that is not a setter (start with `set` and has only one parameter)\n* Declare the variables to be passed as argument to the public method and to store the result, if needed\n\n#### Example in JUnit 4\n\nGiven the file `/src/main/java/com/github/sample/ObjectService.java`:\n\n```java\npackage com.github.sample;\n\nimport java.util.logging.Logger;\n\nimport org.springframework.data.repository.CrudRepository;\nimport org.springframework.stereotype.Repository;\nimport org.springframework.stereotype.Service;\n\n@Service\npublic class ObjectService {\n    private Logger logger;\n    private ObjectRepository repository;\n\n    private ObjectService(ObjectRepository repository) {\n        this.repository = repository;\n    }\n\n    ObjectService(Logger logger, ObjectRepository repository) {\n        this.logger = logger;\n        this.repository = repository;\n    }\n\n    public void setLogger(Logger logger) {\n        this.logger = logger;\n    }\n\n    public void save(Object object) {\n        this.repository.save(object);\n    }\n\n    public Iterable\u003cObject\u003e fetchObjects() {\n        return this.repository.findAll();\n    }\n\n    public Object findById(long id) {\n        return this.repository.findById(id);\n    }\n}\n\n@Repository\n\ninterface ObjectRepository extends CrudRepository\u003cObject, Long\u003e {\n}\n\n```\n\nGenerate the test file `/src/test/java/com/github/sample/ObjectService.java`:\n\n```java\npackage com.github.sample;\n\nimport static org.hamcrest.Matchers.*;\nimport static org.junit.Assert.assertEquals;\nimport static org.junit.Assert.assertThat;\nimport static org.mockito.Mockito.*;\nimport org.hamcrest.CoreMatchers;\nimport org.junit.After;\nimport org.junit.Before;\nimport org.junit.Test;\nimport org.mockito.ArgumentCaptor;\nimport org.mockito.Mock;\n\n// To be improved: only import the types that are used in the class API\nimport java.util.logging.Logger;\nimport org.springframework.data.repository.CrudRepository;\nimport org.springframework.stereotype.Repository;\nimport org.springframework.stereotype.Service;\n\npublic class ObjectServiceTest {\n    @Mock\n    private Logger logger;\n    @Mock\n    private ObjectRepository repository;\n\n    private ObjectService objectService;\n\n    @Before\n    public void setup() {\n        this.objectService = new ObjectService(logger, repository);\n    }\n\n    @Test\n    public void shouldSave() {\n        // TODO: initialize args\n        Object object;\n\n        objectService.save(object);\n\n        // TODO: assert scenario\n    }\n\n    @Test\n    public void shouldFetchObjects() {\n        Iterable\u003cObject\u003e actualValue = objectService.fetchObjects();\n\n        // TODO: assert scenario\n    }\n\n    @Test\n    public void shouldFindById() {\n        // TODO: initialize args\n        long id;\n\n        Object actualValue = objectService.findById(id);\n\n        // TODO: assert scenario\n    }\n}\n```\n\n### Create New Class\n\nGenerate or open a class inside the folder `src/main/java` from a qualified class name (like IntelliJ IDEA).\n\n#### Example\n\nGive the input `com.github.sample.ObjectService`\n\nWill generate a file `/src/main/java/com/github/sample/ObjectService.java` with the following content:\n\n```java\npackage com.github.sample;\n\npublic class ObjectService {\n\n}\n```\n\n## Known Bugs\n\n- [ ] The generated test class tries to create a new instance even when there is only one private constructor\n\n## Roadmap\n\n- [x] Snippets to test\n- [x] Generate a test case for each method\n- [x] Create option to define if should mock the constructor's parameters\n- [x] Create option to define if should create a test case for each method\n- [x] Create option to ignore the static methods\n- [x] Generate template for JUnit 5 (imports, tests, checkers)\n- [x] Auto import the types used in arguments to constructor and methods\n  - [ ] Improve to *only* import the types used in arguments to constructor and methods\n- [ ] Snippets for Spring Boot tests (Rest Controller, Repository) - Doing\n- [ ] Remember JUnit version by project\n- [ ] Command to create the target class in `src/main/java` when doing TDD\n- [ ] Create tests for the extension\n\n## Links\n\n- [Github Repository](https://github.com/wesleyegberto/vscode-java-tests)\n- [JUnit 5 Docs](https://junit.org/junit5/docs/current/user-guide/)\n\n[github-license]: https://github.com/wesleyegberto/vscode-java-tests/blob/master/LICENSE\n[github-license-badge]: https://img.shields.io/github/license/wesleyegberto/vscode-java-tests.svg?style=flat \"License\"\n[github]: https://github.com/wesleyegberto/vscode-java-tests/actions?query=branch%3Amaster\n[github-badge]: https://github.com/wesleyegberto/vscode-java-tests/actions/workflows/ci.yml/badge.svg?branch=master\n[github-history-badge]: https://buildstats.info/github/chart/wesleyegberto/vscode-java-tests?includeBuildsFromPullRequest=false \"GitHub Actions History\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwesleyegberto%2Fvscode-java-tests","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwesleyegberto%2Fvscode-java-tests","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwesleyegberto%2Fvscode-java-tests/lists"}