<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: BOUFATAH Amine</title>
    <description>The latest articles on DEV Community by BOUFATAH Amine (@aminejava).</description>
    <link>https://dev.to/aminejava</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F310884%2Fa81b1cea-b3bd-48d9-b0fa-9b9debe5775f.png</url>
      <title>DEV Community: BOUFATAH Amine</title>
      <link>https://dev.to/aminejava</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aminejava"/>
    <language>en</language>
    <item>
      <title>Quick introduction to EasyRandom</title>
      <dc:creator>BOUFATAH Amine</dc:creator>
      <pubDate>Tue, 18 Apr 2023 22:55:33 +0000</pubDate>
      <link>https://dev.to/aminejava/quick-introduction-to-easyrandom-500b</link>
      <guid>https://dev.to/aminejava/quick-introduction-to-easyrandom-500b</guid>
      <description>&lt;p&gt;Hey Folks , &lt;/p&gt;

&lt;p&gt;Today I'd like to give some words about an interesting library that I have recently discovered and which I'd wish I had known before. This library called EasyRandom is useful when writing test code/sample code.&lt;/p&gt;

&lt;h2&gt;
  
  
  EasyRandom simply put
&lt;/h2&gt;

&lt;p&gt;EasyRandom is a library that generates Java beans. It is mainly used to help create example objects that you use for testing.&lt;/p&gt;

&lt;p&gt;Sometimes while we are writing test code, we find ourselves writing a lot of sample data to test our components, it may be tedious to write such data especially if the object tree is way too big, think of a Person object having one address object, a List of Employers, a Department and so on.. Writing sample data for this scenario is tiresome and uninteresting, this is exactly when EasyRandom comes in handy. &lt;/p&gt;

&lt;p&gt;Let's dive into a simple example, suppose we have a java object representing an Employee as follows: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5shnaqf6syh1k4rx0g3k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5shnaqf6syh1k4rx0g3k.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a simple POJO, that has no nested objects/dependencies. &lt;/p&gt;

&lt;h2&gt;
  
  
  Single object
&lt;/h2&gt;

&lt;p&gt;Generating a single object will be done through the nextObject method: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq1hbtwkfk3sbtum9m560.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq1hbtwkfk3sbtum9m560.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A list of objects
&lt;/h2&gt;

&lt;p&gt;What if we wanted a list of Employees to be generated ? simple, use the objects method which will return a Stream of the desired objects: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F311du64govjtdvx3w2u1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F311du64govjtdvx3w2u1.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This method will generate a list of 10 employees.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Complex object
&lt;/h2&gt;

&lt;p&gt;Great!!! now let's put more complexity in our model: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fenehw1bq0ab6782vttz2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fenehw1bq0ab6782vttz2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The object generated after calling nextObject looks like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Employee(firstName=eOMtThyhVNLWUZNRcBaQKxI, lastName=yedUsFwdkelQbxeTeQOvaScfqIOOmaa, hiringDate=2024-06-18, contactNumber=JxkyvRnL, department=Department(name=RYtGKbgicZaHCBRQDSx), manager=Manager(managerLevel=VLhpfQGTMDYpsBZxvfBoeygjb), position=Position(positionCode=oDLDPR), previousEmployers=[Employer(employerName=IQrWtYnHcO, contactNumber=XgLyqkDjxtCDIj), Employer(employerName=zQeAydAJIwkGuKWiVZLRcElhreuxvw, contactNumber=YVeSelxWdEtYtJHV), Employer(employerName=VIhYOTAybWXyBzGqRe, contactNumber=wKkOqxJSqDjAtcDF), Employer(employerName=hXJISTd, contactNumber=cdpW), Employer(employerName=hXJISTd, contactNumber=cdpW), Employer(employerName=hXJISTd, contactNumber=cdpW), Employer(employerName=GGiDYDmCRViOaAXNumPGEBX, contactNumber=O), Employer(employerName=bplgXS, contactNumber=PAgcjtZAOBahwMGwNikIaKuxYYVASJc), Employer(employerName=RRVabC, contactNumber=DwmrfUyJVSbdBFIyjTfpDItaXB), Employer(employerName=bplgXS, 
.................

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could see from the result shown above that the library created many Employer objects for our Employee, in my case it created 48, maybe we do not need that many employers for our tests, and that's where EasyParameters comes in. EasyParameters is an object which enables us customize the creation of our objects: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feyx64zchobzvxvfdfpkt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feyx64zchobzvxvfdfpkt.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Through the EasyRandomParameters I've chosen to generate at max 3 items in a collection, I've also set the range of dates that should be picked when generating a random Date.  &lt;/p&gt;

&lt;p&gt;Run the program and this time the result is more convenient:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Employee(firstName=eOMtThyhVNLWUZNRcBaQKxI, lastName=yedUsFwdkelQbxeTeQOvaScfqIOOmaa, hiringDate=2022-05-19, contactNumber=JxkyvRnL, department=Department(name=RYtGKbgicZaHCBRQDSx), manager=Manager(managerLevel=VLhpfQGTMDYpsBZxvfBoeygjb), position=Position(positionCode=GGiDYDmCRViOaAXNumPGEBX), previousEmployers=[Employer(employerName=JuhuUTlSCYdTVoSsJGyClHNnCWZGwZ, contactNumber=ImfitPVWqHXHNeZIUAYI)])]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it for this quick introduction to EasyRandom. For more details please refer to the official documentation.  &lt;/p&gt;

</description>
      <category>java</category>
      <category>junit</category>
    </item>
    <item>
      <title>Wrapper classes and caching pitfalls</title>
      <dc:creator>BOUFATAH Amine</dc:creator>
      <pubDate>Mon, 02 Aug 2021 15:09:21 +0000</pubDate>
      <link>https://dev.to/aminejava/wrapper-classes-pitfall-519k</link>
      <guid>https://dev.to/aminejava/wrapper-classes-pitfall-519k</guid>
      <description>&lt;h2&gt;
  
  
  Wrapper Classes
&lt;/h2&gt;

&lt;p&gt;As you all know, the Java language comes up with Primitive Wrapper classes (Integer for int, Double for double, Long for long, Character for char and so on).&lt;/p&gt;

&lt;p&gt;Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.&lt;/p&gt;

&lt;p&gt;Wrapper classes are immutable just like Strings and most Java Developers ignore that just like String objects these wrapper classes have also their own constant pool and that's tricky...&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;Consider the following method: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T1rRrcQq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gs3sqtm1zfhz6bjmv8oi.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T1rRrcQq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gs3sqtm1zfhz6bjmv8oi.PNG" alt="Equals method" width="509" height="56"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And now the main method: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ooPW90QZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dblsfjuv8qm3x8av7wie.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ooPW90QZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dblsfjuv8qm3x8av7wie.PNG" alt="Main method" width="353" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Can you guess the program's output after launching the main method? &lt;/p&gt;

&lt;p&gt;If your answer was: &lt;/p&gt;

&lt;p&gt;1==1 ? true&lt;br&gt;
1==2 ? false&lt;br&gt;
127==127 ? true&lt;br&gt;
128==128 ? true&lt;/p&gt;

&lt;p&gt;Then unfortunately you got it wrong in the last one. Here's the right answer: &lt;/p&gt;

&lt;p&gt;1==1 ? true&lt;br&gt;
1==2 ? false&lt;br&gt;
127==127 ? true&lt;br&gt;
128==128 ? false &lt;/p&gt;

&lt;p&gt;Surprise!!! &lt;/p&gt;

&lt;h2&gt;
  
  
  What happened?
&lt;/h2&gt;

&lt;p&gt;The problem is that an Integer is an object and because we are using the == operator, we are comparing references in the heap here and not actual values. &lt;/p&gt;

&lt;p&gt;You may reply: "Okay but then why does the program print out true in the case of 1==1?". Alright let me explain that for you in a simple way, when you write the following code: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f7w1dnPG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gl0pd5s6qw7zs8pl203d.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f7w1dnPG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gl0pd5s6qw7zs8pl203d.PNG" alt="Code" width="132" height="23"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The compiler transforms it to this code behind the scenes:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3Lsu66Y---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/snnxwth1carf7bbkog07.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3Lsu66Y---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/snnxwth1carf7bbkog07.PNG" alt="Code" width="234" height="25"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is called Autoboxing. &lt;/p&gt;

&lt;h2&gt;
  
  
  Autoboxing/Unboxing
&lt;/h2&gt;

&lt;p&gt;It's a mechanism used by the compiler to transform automatically  wrapper classes to their primitive types (autoboxing) and the other way around (unboxing). &lt;/p&gt;

&lt;p&gt;When we call the &lt;em&gt;equals&lt;/em&gt; method like the following &lt;em&gt;equals(1,1)&lt;/em&gt;, the compiler is smart enough to notice that the equals method takes 2 Integer objects as parameters and not primitives, that's when &lt;strong&gt;Autoboxing&lt;/strong&gt; happens, the compiler transforms your primitive data type (int) into a wrapper object (Integer) by calling the valueOf() method.    &lt;/p&gt;

&lt;p&gt;Okay, so there must be something going on under the valueOf() method. Let's have a look: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n7pLtpLv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03j2pamosyrlngblq1d0.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n7pLtpLv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/03j2pamosyrlngblq1d0.PNG" alt="Code" width="491" height="313"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see this method caches objects that have values between -128 and 127. This basically means that whenever we ask for an Integer between these values Java will always provide the same instance and that's the reason why 1==1 renders true and 128 == 128 renders false. &lt;/p&gt;

&lt;h2&gt;
  
  
  Best practice
&lt;/h2&gt;

&lt;p&gt;The best practice here in order to test the equality of wrapper classes is to use the equals() method which compares object values rather than object references in the heap. &lt;/p&gt;

</description>
      <category>java</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
