{"id":17837079,"url":"https://github.com/orien/bean-matchers","last_synced_at":"2025-04-05T23:08:46.238Z","repository":{"id":3460081,"uuid":"4514181","full_name":"orien/bean-matchers","owner":"orien","description":"Hamcrest matchers for testing Java beans","archived":false,"fork":false,"pushed_at":"2025-03-01T08:26:25.000Z","size":3671,"stargazers_count":45,"open_issues_count":15,"forks_count":14,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-03-29T22:07:03.975Z","etag":null,"topics":["hamcrest-matchers"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/orien.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":"contributing.md","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":"2012-05-31T23:28:52.000Z","updated_at":"2025-03-01T08:26:28.000Z","dependencies_parsed_at":"2023-02-17T02:16:08.204Z","dependency_job_id":"a34f0768-639c-44ed-88e9-9c70fa230dc4","html_url":"https://github.com/orien/bean-matchers","commit_stats":{"total_commits":456,"total_committers":5,"mean_commits":91.2,"dds":"0.48026315789473684","last_synced_commit":"0ad0980c7df786766a0e4a17a79af7a9ed29ace7"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orien%2Fbean-matchers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orien%2Fbean-matchers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orien%2Fbean-matchers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/orien%2Fbean-matchers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/orien","download_url":"https://codeload.github.com/orien/bean-matchers/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247411234,"owners_count":20934653,"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":["hamcrest-matchers"],"created_at":"2024-10-27T20:45:20.439Z","updated_at":"2025-04-05T23:08:46.216Z","avatar_url":"https://github.com/orien.png","language":"Java","funding_links":[],"categories":["测试"],"sub_categories":[],"readme":"Bean Matchers\n=============\n\n[![Maven Central](https://img.shields.io/maven-central/v/com.google.code.bean-matchers/bean-matchers.svg)](https://search.maven.org/search?q=g:com.google.code.bean-matchers%20AND%20a:bean-matchers)\n[![License](https://img.shields.io/github/license/orien/bean-matchers.svg)](https://github.com/orien/bean-matchers/blob/main/LICENSE)\n[![Build Status](https://github.com/orien/bean-matchers/workflows/build/badge.svg?branch=main)](https://github.com/orien/bean-matchers/actions?query=workflow%3Abuild+branch%3Amain)\n[![Codecov](https://codecov.io/gh/orien/bean-matchers/branch/main/graph/badge.svg?token=8s9HyDeakh)](https://codecov.io/gh/orien/bean-matchers)\n\n* [JavaBeans Spec](https://www.oracle.com/java/technologies/javase/javabeans-spec.html)\n* [JavaBeans Tutorial](https://docs.oracle.com/javase/tutorial/javabeans/)\n* [JavaBeans Wikipedia Entry](https://en.wikipedia.org/wiki/JavaBeans)\n* [Project Documentation](https://orien.io/bean-matchers/)\n* [Google Code project page (archived)](https://code.google.com/archive/p/bean-matchers/)\n\nBeans are prevalent in our Java world, however these simple structures are commonly\nuntested. Over the course of a project they can be inflicted with copy and paste errors,\n`hashCode`, `equals` and `toString` methods fall out of sync as new properties are added.\n\nEnter Bean Matchers. The Bean Matchers project provides a series of Hamcrest matchers for\ntesting Java beans. This provides an easy way to gain confidence that your beans are\ncorrect without needing to consume time writing explicit tests for each property. Ensure\na no-args constructor exist. Getters and setters are correctly defined for each property.\nProperties are compared in `equals`, influence the generated `hashCode`, and are present\nin the output of the `toString` method.\n\nSpecial attention has been given to the readability of these matchers. It is important\nthat our tests describe the intent of the code being tested.\n\nBean Matchers is released under the [3-Clause BSD License](https://opensource.org/licenses/BSD-3-Clause).\n\nQuick Start\n-----------\n\n1. Add the Bean Matchers dependency to your Maven project\n   (Check the [Central Repository](https://search.maven.org/search?q=g:com.google.code.bean-matchers%20AND%20a:bean-matchers)\n   for the latest version):\n\n   ```xml\n   \u003cdependency\u003e\n      \u003cgroupId\u003ecom.google.code.bean-matchers\u003c/groupId\u003e\n      \u003cartifactId\u003ebean-matchers\u003c/artifactId\u003e\n      \u003cversion\u003e0.14\u003c/version\u003e\n      \u003cscope\u003etest\u003c/scope\u003e\n   \u003c/dependency\u003e\n   ```\n\n2. Test your bean:\n\n   ```java\n   import static com.google.code.beanmatchers.BeanMatchers.*;\n   import static org.hamcrest.CoreMatchers.allOf;\n   import static org.hamcrest.MatcherAssert.assertThat;\n\n   public class MyBeanTest {\n\n       // One big test\n       @Test\n       public void testBean() {\n           assertThat(MyBean.class, allOf(\n                   hasValidBeanConstructor(),\n                   hasValidGettersAndSetters(),\n                   hasValidBeanHashCode(),\n                   hasValidBeanEquals(),\n                   hasValidBeanToString()\n           ));\n       }\n\n       // Individual, well named tests\n\n       @Test\n       public void shouldHaveANoArgsConstructor() {\n           assertThat(MyBean.class, hasValidBeanConstructor());\n       }\n\n       @Test\n       public void gettersAndSettersShouldWorkForEachProperty() {\n           assertThat(MyBean.class, hasValidGettersAndSetters());\n       }\n\n       @Test\n       public void allPropertiesShouldInfluenceHashCode() {\n           assertThat(MyBean.class, hasValidBeanHashCode());\n       }\n\n       @Test\n       public void allPropertiesShouldBeComparedDuringEquals() {\n           assertThat(MyBean.class, hasValidBeanEquals());\n       }\n\n       @Test\n       public void allPropertiesShouldBeRepresentedInToStringOutput() {\n           assertThat(MyBean.class, hasValidBeanToString());\n       }\n\n   }\n   ```\n\nTesting the No-Args Constructor\n-------------------------------\n\nEnsures a bean has a working no-args constructor.\n```java\nassertThat(BeanToTest.class, hasValidBeanConstructor());\n```\n\nTesting Getters and Setters\n---------------------------\n\nMatchers for ensuring what is stored using a setter is what is obtained using the related getter.\n\nEnsure all properties on the bean have working getters and setters.\n```java\nassertThat(BeanToTest.class, hasValidGettersAndSetters());\n```\n\nEnsure that the property named `testOnlyThisProperty` has a working getter and setter.\n```java\nassertThat(BeanToTest.class, hasValidGettersAndSettersFor(\"testOnlyThisProperty\"));\n```\n\nEnsure that all properties on the bean except the property named `dontTestPropertyWithThisName` has working getters and setters.\n```java\nassertThat(BeanToTest.class, hasValidGettersAndSettersExcluding(\"dontTestPropertyWithThisName\"));\n```\n\nThese matchers test a bean instance.\n```java\nassertThat(new BeanToTest(), isABeanWithValidGettersAndSetters());\n\nassertThat(new BeanToTest(), isABeanWithValidGettersAndSettersFor(\"testOnlyThisProperty\"));\n\nassertThat(new BeanToTest(), isABeanWithValidGettersAndSettersExcluding(\"dontTestPropertyWithThisName\"));\n```\n\nTesting the hashCode Method\n---------------------------\n\nMatchers for ensuring properties influence the generated hash code. This is tested by setting the property with one value and comparing the generated hash code with that produced when the property is set with a different value. If the values differ we infer the hash code is correctly influenced by the property.\n\nEnsure that all properties on the bean influence the produced hash code.\n```java\nassertThat(BeanToTest.class, hasValidBeanHashCode());\n```\n\nEnsure that the property named `propertyInfluencingHashCode` influences the produced hash code.\n```java\nassertThat(BeanToTest.class, hasValidBeanHashCodeFor(\"propertyInfluencingHashCode\"));\n```\n\nEnsure that all properties on the bean except the property named `propertyNotInfluencingHashCode` influences the produced hash code.\n```java\nassertThat(BeanToTest.class, hasValidBeanHashCodeExcluding(\"propertyNotInfluencingHashCode\"));\n```\n\nTesting the equals Method\n-------------------------\n\nMatchers for ensuring properties are compared during the equals operation. Ensure the method accounts for the same instance, a null instance, instance of differing type and null properties.\n\nEnsure all properties on the bean are compared during the equals method.\n```java\nassertThat(BeanToTest.class, hasValidBeanEquals());\n```\n\nEnsure the property named `testOnlyThisProperty` on the bean is compared during the equals method.\n```java\nassertThat(BeanToTest.class, hasValidBeanEqualsFor(\"testOnlyThisProperty\"));\n```\n\nEnsure all the properties on the bean is compared during the equals method except the property named `dontTestPropertyWithThisName`.\n```java\nassertThat(BeanToTest.class, hasValidBeanEqualsExcluding(\"dontTestPropertyWithThisName\"));\n```\n\nTesting the toString Method\n---------------------------\n\nMatchers for ensuring the bean class name and properties are included in toString output.\n\nEnsure all properties on the bean are included in the string value.\n```java\nassertThat(BeanToTest.class, hasValidBeanToString());\n```\n\nEnsure the string value includes includes the property named `propertyInToString`. Any other properties on the bean are not tested.\n```java\nassertThat(BeanToTest.class, hasValidBeanToStringFor(\"propertyInToString\"));\n```\n\nEnsure the string value includes all properties on the bean except the property named `propertyNotInToString`.\n```java\nassertThat(BeanToTest.class, hasValidBeanToStringExcluding(\"propertyNotInToString\"));\n```\n\nGenerating Property Values\n--------------------------\n\nThe Bean Matchers library generates values to populate beans while performing various tests. Out of the box Bean Matchers will generate random values for properties of primitive, array and enum type. It will delegate to [Mockito](https://site.mockito.org) to create a mock for non-final types. For final types, one can implement and register a [ValueGenerator](https://github.com/orien/bean-matchers/blob/main/src/main/java/com/google/code/beanmatchers/ValueGenerator.java) to generate random values.\n\nFor example say we have defined a value type:\n```java\npublic final class MyCustomValueType {\n    private final String value;\n\n    public MyCustomValueType(String value) {\n        this.value = value;\n    }\n    ...\n}\n```\n\nWe have a bean with a property of our value type:\n```java\npublic class MyBean {\n    private MyCustomValueType property;\n\n    public MyCustomValueType getProperty() {\n        return property;\n    }\n\n    public void setProperty(MyCustomValueType property) {\n        this.property = property;\n    }\n    ...\n}\n```\n\nTo test this bean we need to register a ValueGenerator that can generate random values to populate the bean with:\n```java\nBeanMatchers.registerValueGenerator(new ValueGenerator\u003cMyCustomValueType\u003e() {\n        public MyCustomValueType generate() {\n            return new MyCustomValueType(StringUtils.randomString());\n        }\n    }, MyCustomValueType.class);\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forien%2Fbean-matchers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Forien%2Fbean-matchers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Forien%2Fbean-matchers/lists"}