What is Java Faker Library?

Java Faker is a library used to generate fake data. It provides different classes and methods in order to generate real-looking data that ranges from mobile number, address, music, nation and many more. This is really helpful when we want to use some placeholder but don’t have actual data. For example, you want to generate debit card data and want to achieve validation against it. This is possible using Faker library.

In this article, we will be looking at how to generate dummy data with the help of JavaFaker’s classes.

Dependencies

Below is the single dependency we will need for maven based projects.

Faker Class

Let us see how to instantiate a Faker object and use it to call some fake data.

Faker faker = new Faker();

To generate data with different locales:

We use locales to make the generated data more specific to a single location.

Faker faker = new Faker(new Locale(“LOCALE_NAME”));

For example :

Let us retrieve some fake data with the help of the Faker class instance.

Faker faker = new Faker();

String firstName=faker.name().firstName();

String lastName=faker.name().lastName();

String city=faker.address().cityName();

String country=faker.address().country();

Similarly we can use this Faker object to create data relating to many more objects such as:

  • Food
  • Music
  • Company
  • Finance
  • HarryPotter

You can find the full list here.

FakeValueService

This class provides methods for generating random sequences. Let us see some of the useful methods provided by the FakeValueService class.

  1. Letterify :

This method helps to generate random sequences of alphabetic characters.

  1. Numerify :

This method helps to generate numeric sequences.

  1. Bothify :

This method is a combination of the two and can create random alphanumeric sequences – useful for mocking contents like ID strings.

  1. Regexify :

This method generates random sequence based on a given regex pattern.

FakeValueService requires a valid Locale, as well as a RandomService:

In the below test, we create a FakeValueService instance with locale ‘en-GB’ and generate a unique fake gmail address with the help of Bothify method.

Here, we replace ‘?’ by random letters and ‘#’ by random numbers and then validate the generated gmail address with the help of Matcher.

In the below code snippet, we will use FakeValueService to create a random sequence on the basis of a specified regex using the Regexify method.

In this tutorial, we explored the JavaFaker library to generate real-looking fake data along with the useful classes Faker class and FakeValueService class. Also, we saw how to use locales to generate location specific data.

74 thoughts on “What is Java Faker Library?”

  1. lintas berita

    Howdy! Someone in my Facebook group shared this website with us so I came to take a look. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers! Fantastic blog and excellent design and style.

  2. Howdy, i read your blog occasionally and i own a similar one and i was just curious if you
    get a lot of spam comments? If so how do you reduce it,
    any plugin or anything you can suggest? I get so much lately it’s driving me crazy
    so any help is very much appreciated.

  3. situs bokepdice

    I was just seeking this info for a while. After 6 hours of continuous Googleing, finally I got it in your website. I wonder what is the lack of Google strategy that do not rank this type of informative websites in top of the list. Usually the top websites are full of garbage.

  4. It?s really a great and helpful piece of info. I am glad that you shared this helpful information with us. Please keep us up to date like this. Thanks for sharing.

  5. It’s the best time to make some plans for the future and it’s time to be happy. I’ve read this post and if I could I desire to suggest you some interesting things or suggestions. Perhaps you could write next articles referring to this article. I wish to read more things about it!

  6. Thanks for posting. I really enjoyed reading it, especially because it addressed my issue. It helped me a lot and I hope it will help others too.

  7. I have been browsing online more than three hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. Personally, if all webmasters and bloggers made good content as you did, the web will be much more useful than ever before.

  8. Thank you for posting this post. I found it extremely helpful because it explained what I was trying to say. I hope it can help others as well.

  9. wholesale fabric store

    You could definitely see your expertise in the work you write. The sector hopes for even more passionate writers like you who are not afraid to mention how they believe. All the time follow your heart.

  10. Good day! I could have sworn I’ve been to this web site before but after looking at a few of the posts I realized it’s new to me. Nonetheless, I’m certainly pleased I came across it and I’ll be bookmarking it and checking back regularly!

  11. houston junk car buyer

    I’m impressed, I must say. Seldom do I come
    across a blog that’s both educative and amusing, and let me
    tell you, you have hit the nail on the head. The problem is something which too few people are
    speaking intelligently about. I’m very happy I found this during my search for something relating to this.

  12. Hi there! Would you mind if I share your blog with my facebook group?
    There’s a lot of people that I think would really appreciate your content.
    Please let me know. Thanks

  13. I am extremely inspired together with your writing talents and also with the format in your blog.
    Is this a paid subject matter or did you customize it yourself?
    Anyway keep up the nice quality writing, it’s uncommon to look a great blog like this one these
    days..

  14. Hello, always i used to check blog posts here in the early hours in the
    morning, as i love to gain knowledge of more and more.

  15. ทางเข้าSUPERSLOT

    Appreciate you sharing, great article post.Really looking forward to read more. Really Great.

  16. Does your site have a contact page? I’m having trouble
    locating it but, I’d like to shoot you an e-mail.
    I’ve got some ideas for your blog you might be interested in hearing.
    Either way, great blog and I look forward to seeing it develop over time.

  17. When I originally commented I seem to have clicked on the -Notify me
    when new comments are added- checkbox and from now on each time a comment is added I
    get 4 emails with the same comment. Is there an easy
    method you are able to remove me from that service?
    Appreciate it!

  18. Howdy, I believe your web site could be having web browser compatibility problems.
    When I take a look at your blog in Safari, it looks fine however, when opening in Internet Explorer, it has some overlapping
    issues. I just wanted to provide you with a quick heads up!

    Aside from that, great blog!

  19. I’m extremely impressed with your writing skills and also with the
    layout on your weblog. Is this a paid theme or did you customize it yourself?
    Either way keep up the nice quality writing, it’s rare to see
    a nice blog like this one today.

  20. I got this web site from my buddy who informed me regarding this website and at the moment this time
    I am visiting this web page and reading very informative
    content here.

  21. I’ve been browsing online more than 3 hours today, yet I never
    found any interesting article like yours. It’s pretty worth enough for me.
    Personally, if all website owners and bloggers made good content as you did, the net will be much more useful
    than ever before.

  22. magnificent issues altogether, you just won a new reader.
    What might you recommend in regards to your put up that you made a few days
    ago? Any sure?

  23. It is in reality a nice and useful piece of information. I am satisfied that you
    shared this helpful information with us. Please stay
    us informed like this. Thanks for sharing.

Leave a Comment

Your email address will not be published. Required fields are marked *