48

I would like to know if a fake data generator exists for Java. In Perl exists Data::Faker and there's a port to Ruby called faker, for JavaScript faker.js. Someone know about a fake data generator for Java, that can provide random names, phone number, P.O. box number, etc...

4
  • 1
    I've never seen something like this (in the libraries java is shipping with). Maybe there is a 3rd party library that does that for you in Java. In this case you should use a search engine to find out :) Jun 15, 2011 at 19:25
  • Checkout ThinkUI
    – Bala R
    Jun 15, 2011 at 19:29
  • I've recently authored a library that can pass as faker-like: github.com/nomemory/mockneat Aug 22, 2017 at 11:13
  • You could try Datafaker, a JVM library to generate fake data. It's an up to date version (Disclaimer: maintained by me).
    – Erik Pragt
    Feb 1, 2022 at 12:44

4 Answers 4

51

Try jFairy. This is new project in early stage.

Fairy fairy = Fairy.create();
Person person = fairy.person();

System.out.println(person.fullName());            // Chloe Barker
System.out.println(person.email());               // barker@yahoo.com
System.out.println(person.telephoneNumber());     // 690-950-802

Company company = fairy.company();
System.out.println(company.name());          // Robuten Associates
System.out.println(company.url());           // http://www.robuteniaassociates.com

Person salesman = fairy.person(withCompany(company));
System.out.println(salesman.fullName());     // Juan Camacho
System.out.println(salesman.companyEmail()); // juan.camacho@robuteniaassociates.com

PS. I'm a contributor.

4
  • 2
    We are working on adding more functionalities right now and a new version will be released soon. Feb 10, 2015 at 12:12
  • 1
    How can i become a contributor too? im using it but it doesnt fit with my real life project. To be clear, it's language based, not locale based (lets say Kanada with french), or whatever you want. Language based is not fitting to a real life project. A phoneNumber is not based on language but on locale. I like the base concept and think its the best open thing by now, but its not fitting with the reality in a big project. By now i just modify the objects per reflection to fit, but i think i can provide something which is more usable. On githu i can not see how i can contribute. May 8, 2016 at 17:37
  • Any help is always welcome. To contribute use Pull Requests, more to read: guides.github.com/activities/contributing-to-open-source
    – MariuszS
    May 8, 2016 at 18:17
  • How to create test data which can start with word of my choice like "Test Chloe Barker" May 18, 2021 at 12:37
16

http://www.andygibson.net/blog/article/generate-test-data-with-datafactory/ How to use:

  1. Add it in your pom.xml

    <dependency>
        <groupId>org.fluttercode.datafactory</groupId>
        <artifactId>datafactory</artifactId>
        <version>0.8</version>
        <type>jar</type>
    </dependency>
    
  2. Test it...

    public class Main {
    
    public static void main(String[] args) {
        DataFactory df = new DataFactory();
        for (int i = 0; i < 100; i++) {          
            String name = df.getFirstName() + " "+ df.getLastName();
            System.out.println(name);
        }
      }
     }
    

Output :

Lindsey Craft
Erica Larsen
Ryan Levine
Erika Smith
Brooklyn Sloan
Karen Mayer
Eddie O'neill
Nancy Stevens
3
15

There is a Java port of the Perl Data::Faker - java-faker

3
  • It's worth to use, indeed. In my opinion it's got a good l10n support. Nov 6, 2012 at 21:28
  • 2
    I recently used java-faker, and creating an instance of Faker thrown a NPE. I proceeded to report an issue for this: github.com/DiUS/java-faker/issues/6 Apr 8, 2013 at 14:52
  • Most of built in faker-s are useless. Because there are plenty of them (pokemon, startrek etc) its hard to find useful one.
    – Krzysztof
    Aug 6, 2021 at 16:56
7

If you're using Hibernate, try HibernateMock.

Also:

2
  • 3
    HibernateMock is no more:-( But has been reborn as code.google.com/p/jpamock
    – enkor
    Mar 9, 2012 at 14:42
  • i tried Benerator.. it seems to be a good project ... but there is little or no gratis support on it.. activiy on this project is very low
    – arthur
    Jul 28, 2012 at 1:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.