Zach Cochran
by Zach Cochran
2 min read

Categories

Tags

Intellij is such an overwhelming tool. It’s almost like opening up photoshop for the first time, with 500 different buttons and dropdowns everywhere and what am I supposed to do with this or that? That’s how I felt the first time I opened up Intellij.

So tonight I wanted to explore what Intellij could do for Unit Tests. They’re a thing that I never really focused much on back in the javascript and python world, mostly because I was doing testing for my real job and wanted nothing to do with it.

Turns out, like with most things in Intellij, there’s actually a built in means to generating all of the test methods you could want, automatically, based on the methods available in your class! Yes, really! My mind was blown!

Not only that, but it’s super easy to do! All you need to do is go to your class, click on your class name, and then press alt + enter. This will open up a menu and one of the options will be to generate tests. If you hit enter on it a new screen will come up and give you a list of all of the methods present in the class. You can select the ones that you want, and then it will ask you where you want the file. Select the folder and you’re done!

This ends up generating a nice boilerplate file for you to start writing your tests in, with each of your methods being represented.

I went ahead and made a simple example tonight because I wanted to also get a hang of starting to write test cases. This was a super simple example, and didn’t really test any logic (just getters and setters). But it did make me learn about using the @Before annotation to do setup before each test case.

Account.java

public class Account {

   private final int accountNumber;
   private String fname;
   private String lname;
   private double total;

   Account(int accountNumber, String fname, String lname) {
       this.accountNumber = accountNumber;
       this.fname = fname;
       this.lname = lname;
   }

   public int getAccountNumber() {
       return accountNumber;
   }

   public String getFname() {
       return fname;
   }

   public void setFname(String fname) {
       this.fname = fname;
   }

   public String getLname() {
       return lname;
   }

   public void setLname(String lname) {
       this.lname = lname;
   }

   public double getTotal() {
       return total;
   }

   public void setTotal(double total) {
       this.total = total;
   }
}

AccountTest.java

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class AccountTest {

   private Account joe;

   @Before
   public void setup() {
       joe = new Account(1234, "Joe", "Cartoon");
   }

   @Test
   public void getAccountNumber() {
       int actNum = joe.getAccountNumber();
       assertEquals(actNum, 1234);
   }

   @Test
   public void getFname() {
       assertEquals(joe.getFname(), "Joe");
   }

   @Test
   public void setFname() {
       joe.setFname("Joseph");
       assertEquals(joe.getFname(), "Joseph");
   }

   @Test
   public void getLname() {
       assertEquals(joe.getLname(), "Cartoon");
   }

   @Test
   public void setLname() {
       joe.setLname("Joseph");
       assertEquals(joe.getLname(), "Joseph");
   }

   @Test
   public void getTotal() {
       assertEquals(joe.getTotal(), 0.0, .001);
   }

   @Test
   public void setTotal() {
       joe.setTotal(1000.00);
       assertEquals(joe.getTotal(), 1000.00, .001);
   }
}

💚