Every self respecting java programmer hates testing of pojo's, but it should / must be done. Beanunit makes it very easy to do an almost complete test of these simple objects with only one line of code. Just check out the code and look at the unit tests for a better explanation.
It has a dependency on junit.
<dependencies> ... <dependency> <groupId>nl.ivonet</groupId> <artifactId>beanunit</artifactId> <version>2.0</version> <scope>test</scope> </dependency> ... </dependencies>
@Test public void testAssertPojo() throws Exception { PojoContractAsserter.assertBean(Person.class); }
@Test public void testAssertPojo() throws Exception { ConstructedBeanAsserter.assertBean(Person.class); }
@Test public void testAssertPojo() throws Exception { BuilderBeanAsserter.assertBean(Person.class); }
@Before public void setUp() throws Exception { registerTypeAndDefaultArgument(AddressDto.class, new AddressDto.Builder().setCity("c").build()); registerTypeAndDefaultArgument(BigDecimal.class, new BigDecimal(42)); } @After public void tearDown() throws Exception { resetToDefaultTypes(); }
@Test public void testCreateObject() throws Exception { final Employee employee = createObject(Employee.class); assertNotNull(employee); assertEquals("Employee{id='String'}", employee.toString()); }
Copyright 2011 Ivo Woltring Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Ivo Woltring
Twitter: @ivonet
Version 2.0: - Major version because the interface has been changed - Now only the assertBean methods are public - Better names for the Asserter classes. - Easier to use as the above examples will demonstrate - added the createObject method as a convenience for unit testers that want to create an object the easy way for further testing of their own. - Some code cleanup. - More typos Version 1.0: - Fully working version - some textual improvements on fails - added the assertBean method as a replacement for all the other methods - more unit tests Version 0.3, 0.4 and 0.6 - trial versions (fully working of course) - first releases to maven central
You can download this project in either zip or tar formats.
You can also clone the project with Git by running:
$ git clone git://github.com/IvoNet/beanunit