From the course: Java: Testing with JUnit
Write and run test classes and methods
From the course: Java: Testing with JUnit
Write and run test classes and methods
- [Instructor] Let's just start writing some tests. First, we'll need to create a class that we can write tests for. Let's start with the very, very simplified BankAccount class. I'm going to create a class BankAccount. And I'm going to give it two properties: a balance and a minimumBalance. And private double minimumBalance. Like this. And then we would like to create a constructor that's going to set these two fields. So I'm going to make a public constructor, and it's going to take a double balance and a double minimumBalance. Can say this.balance equals balance and this.minimumBalance equals minimumBalance. Probably, we need to get balance and minimumBalance later, so let's also create getters for this, and I'm going to be lazy and just generate these. There we go. For now, let's just create two additional methods: withdraw and deposit. First, withdraw for when you want to take money from the account. It will return the amount you are withdrawing, and if that's not possible, it will throw an exception. So I'm just going to say if the balance minus the amount is still bigger than the minimumBalance, then we're good to go. We'll simply replace the balance with our new balance. And then we'll return the amount. Else, and then we'll just throw the RuntimeException. This is only just to show you what it should be doing. Clearly, this is not how you'd really implement this. Next, the deposit method. We'll now create the deposit method and then we'll return a new balance and it takes the amount deposited as a parameter. So let's go ahead, public double deposit like this. Again, double amount. So balance just becomes the old balance plus the amount and will return it. Clearly, I should be checking for negative numbers but it's just a very simplified example for the purpose of getting to know JUnit. All right, we have two methods. We have getters and we have two properties. We're all set. Let's now create a test class to see if we can test the behavior of our class. First, I'm going to be creating the class BankAccountTest in our test folder. So I'm going to go ahead, say BankAccountTest. And for now, I'm just going to be writing two test methods. One to test withdraw, and one to test the deposit. So first, withdraw. I'm going to call it testWithdraw. And I'm going to create a new instance of BankAccount in here. The balance will be 500 and the minimum will be -1,000. Now I'm going to withdraw 300 and then I'm going to use assertEquals to see whatever it does what I would expect. So I'm going to go ahead and say bankAccount.withdraw 300 and then I'm going to use assertEquals. Import static org.junit.jupiter.api.Assertions. All of them. Okay. And I'm going to be seeing what it is what I would expect. Well, I expect it will be 200. And the actual is the getBalance on our instance. So I'm going to call getBalance like this. And we also need to add the annotation on top of our test method so that it recognizes our method as a test. We can run this test now, and as you will see, it's going to pass. If we change something to our method, for example, use the smaller sign instead of the bigger sign, we can see that our test will fail. So I've changed our method and now I'm going to move back over to test, run it again, and you can see that our test is failing right now because it's throwing the RuntimeException instead. So let's go back to our code and change our test back. And here you see the purpose of the unit test demonstrated at the same time as well. Our test was no longer passing because our code was not doing what we would expect it to do. All right, that's fixed. Now, let's do something similar for deposit. Let's create a testDeposit. And again, we'll create a new BankAccount instance in our method. And the balance here is 400 and the minimum is 0. So we're going to deposit 500. And I expect the balance to become 900, and to test it, I'm going to use the getBalance to get the actual balance. So again, I'm going to go for assertEqual. Expect is 900 and then I'm going to say bankaccount.getBalance. Now I still need to add @Test on top, and then we can run this test method as well. So let me run it and as you'll see, it will run successfully as well. We can also run all the tests in a class by pressing the play icon next to the class name. So this time, I'm not running an individual test but I'm actually running all the tests in this class. You can see this in the lower left corner. It is now showing the test results of both methods. Well, that's it. You should now be able to write a very basic unit test yourself. Let's see how we can make the output better structured by adding a display name to all of this in the next video.