{"id":20171601,"url":"https://github.com/pascalw/jfactory","last_synced_at":"2026-02-15T13:33:28.086Z","repository":{"id":3696957,"uuid":"4767764","full_name":"pascalw/jFactory","owner":"pascalw","description":"jFactory - easy object factories for Java","archived":false,"fork":false,"pushed_at":"2013-04-12T17:54:43.000Z","size":196,"stargazers_count":1,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-24T07:43:44.170Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://jfactory.pwiddershoven.nl","language":"Java","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/pascalw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-06-23T21:37:17.000Z","updated_at":"2024-03-31T14:12:53.000Z","dependencies_parsed_at":"2022-09-10T20:27:25.416Z","dependency_job_id":null,"html_url":"https://github.com/pascalw/jFactory","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pascalw%2FjFactory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pascalw%2FjFactory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pascalw%2FjFactory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pascalw%2FjFactory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pascalw","download_url":"https://codeload.github.com/pascalw/jFactory/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244110086,"owners_count":20399561,"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":[],"created_at":"2024-11-14T01:25:42.492Z","updated_at":"2025-10-05T21:43:34.236Z","avatar_url":"https://github.com/pascalw.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jFactory [![Build Status](https://travis-ci.org/PascalW/jFactory.png)](https://travis-ci.org/PascalW/jFactory)\n\njFactory is a factory library for Java, inspired by the great [factory_girl](https://github.com/thoughtbot/factory_girl) by Thoughtbot.\n\njFactory can be used to easily define factories for your model objects.\nIt has a notion of building and creating objects, where creating objects means saving objects to a database after building them.\n\njFactory is database/persistence layer agnostic, support for databases can easily provided by subclassing the PersistableObjectFactory class.\nCurrently only MongoDB is supported through the [Morphia](http://code.google.com/p/morphia/) POJO mapper.\n\n# Usage\n\n## Defining factories\n\n```java\npublic class ArticleFactory extends ObjectFactory\u003cArticle\u003e {\n\n    public ArticleFactory() {\n        super(Article.class);\n    }\n\n    @Override\n    protected void define() {\n        property(\"guid\", \"http://pwiddershoven.nl/blog/2011/01/05/airplayer.html\");\n        property(\"title\", \"Airplayer\");\n\n        trait(new Trait(\"unpublished\") {\n            public void apply() {\n                property(\"state\", Article.State.UNPUBLISHED);\n            }\n        });\n\n        sequence(\"id\", new Sequence() {\n            public Object apply(int n) {\n                return n;\n            }\n        });\n    }\n    \n    @AfterFactoryBuild\n    public void afterBuild(Article article) {\n        System.out.println(article);\n    }\n}\n```\n\n```java\npublic class BookFactory extends ObjectFactory\u003cBook\u003e {\n\n    public BookFactory() {\n        super(Book.class);\n    }\n\n    @Override\n    protected void define() {\n        constructWith(\"Enterprise Integration Patterns\", \"Gregor Hohpe\");\n        \n        // lazy values are evaluated when an object is created through the factory\n        // regular values are evaluated when the factory is constructed\n        property(\"createdAt\", new LazyValue() {\n            @Override\n            public Object evaluate() {\n                return new Date();\n            }\n        });\n    }\n}\n```\n\n## Using factories\n\nFactories can be either called directly:\n\n```java\nBook book = new BookFactory().build(\"title\", \"My Book\");\nArticle article = new ArticleFactory(\"unpublished\").build();\n```\n\nOr through the provided Factory class. In this case you have to configure which packages to scan for Factory classes.\n\n```java\n// add package to scan for factory classes, so jFactory can find the factories\nFactory.addFactoryScanPackage(\"nl.pwiddershoven.jfactory.factories\");\n\nArticle article1 = build(Article.class);\nArticle article2 = build(Article.class, \"unpublished\", \"title\", \"Some title\");\nArticle article2 = build(Article.class, \"author\", \"Pascal\");\n````\n\nFor more examples, see [FactoryTest.java](https://github.com/PascalW/jFactory/blob/master/core/src/test/java/nl/pwiddershoven/jfactory/FactoryTest.java).\n\n## Persistence\n\njFactory has the notion of \"persistable objects\", meaning you can create factories which will automatically persist the created objects through some persistence layer.\njFactory has a flexible architecture, allowing you to easily define custom abstract factories for your favorite persistence layer.\njFactory comes with support for persitence through the [Morphia](http://code.google.com/p/morphia/) Object Mapper.\n\nDefining a factory for a Morphia entity is easy and just like defining a regular Factory:\n\n```java\npublic class ArticleFactory extends MorphiaBackedPersistableObjectFactory\u003cPersistableArticle\u003e {\n\n    public ArticleFactory() {\n        super(PersistableArticle.class);\n    }\n\n    @Override\n    protected void define() {\n        property(\"id\", new ObjectId());\n        property(\"guid\", \"http://pwiddershoven.nl\");\n        property(\"title\", \"Test\");\n    }\n    \n    @AfterFactoryCreate\n    public void afterCreate(PersistableArticle article) {\n        //do something with the article\n    }\n}\n```\n\nThe only difference is the factory class you inherit from must be a `PersistableObjectFactory` like the `MorphiaBackedPersistableObjectFactory`.\nPeristableObjectFactories behave like regular factories, but add a `create` method allowing you the build and persist an object.\n\n```java\n//first, set connection details so the object factory know where to persist your objects\nMorphiaBackedPersistableObjectFactory.setDatastore(datastore);\n\n//using `build` an object is only constructed like when using a regular factory\nPersistableArticle article1 = build(PersistableArticle.class, \"title\", \"test\");\n\n//using `create` to create an object will cause the object to be persisted too\nPersistableArticle article2 = create(PersistableArticle.class, \"title\", \"test\");\n```\n\n### Custom abstract persistable factories\n\nDefinign a custom abstract factory for some persistence layer is easy, see for example the `MorphiaBackedPersistableObjectFactory` implementation:\n\n```java\npublic abstract class MorphiaBackedPersistableObjectFactory\u003cT\u003e extends PersistableObjectFactory\u003cT\u003e {\n\n    private static Datastore datastore;\n\n    public static void setDatastore(Datastore datastore) {\n        MorphiaBackedPersistableObjectFactory.datastore = datastore;\n    }\n\n    public MorphiaBackedPersistableObjectFactory(Class\u003cT\u003e factoryClass) {\n        super(factoryClass);\n    }\n\n    @Override\n    protected void persist(T object) {\n        datastore.save(object);\n    }\n}\n```\n\nBasically, you only have to define a method that can persist built objects.\nPersistableObjectFactories should also allow to set some connection details, like the `setDatastore` method in the example above.\n\n# Availability\n\njFactory releases are available in [Maven Central](http://search.maven.org/#search%7Cga%7C1%7Cjfactory).  \nSnapshots of master are available at the [Sonatype OSS snapshot repository](https://oss.sonatype.org/content/repositories/snapshots/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpascalw%2Fjfactory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpascalw%2Fjfactory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpascalw%2Fjfactory/lists"}