Using Assertions with JUnit - Java Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
by the end of this video you will learn when and where to apply the different types of assertions with unit testing so this includes assert equals assert not equals assert same assert true and many more okay first we're going to take a look at the classes that we will be testing with the different types of assertions so first we have this player class they will have a name and an age and they will be instantiated through the constructor and then we just have the getters and setters for that name and age but also below i've overridden the equals method to return true if the name of these two players are the same so this equals method just to help us differentiate between two of the different sessions and we have this player statistics class which will take in a player along with an integer for the number of games and goals which they've played and scored in and then we have a few statistics down here so we have games per goal which will return the number of games divided by the number of goals and then we also have goals per game we have a boolean called under 30 which will return true if they're under the age of 30. we also have this createcsv record method which will firstly check if the number of games is zero and it will return null otherwise it will return an array of doubles which consists of the goals per game and the games per goal and lastly we have this static method which will just return player two if they are younger than player one otherwise they will return player one and then at the end we have the getters and setters as we are testing the player statistics class i will create a player statistics test class in the test java directory before we begin writing tests uh just ensure that you have the appropriate junit dependency with your build tool to include junit into the class part so my first test is going to test the overridden equals method of the player's class now remember the equals method will equate two player instances as long as they have the same name so i have created two players with the same name to test that the two players are equal i will use the first assertion which is called asset equal whenever we write an assertion the first argument can actually be a string and this can be a descriptive message for why the assertion may have failed so one example might be might be players not equal with the same name but for this video i won't be including any messages so if i run this test we can see that player 1 and player 2 are equal because they have the same name we can also test that two objects are not equal we can test that the two players are not equal by using assert not equals as we can see that the test has passed because the two players are not equal because they have different names rather than equating two objects with its equals method we can assert upon the actual memory location of the objects so this would be similar to using the double equal sign to equate two objects so we can use the assets same assertion to ensure that the exact object returned is the younger of the two players so i will use a same the first object that we pass in is what is expected so out of the two players it will be player 2 who is 25 years old rather than player 1 who is 27 so i'll pass in player 2 and the actual player that will be returned will be obtained by using the get younger player method from the player statistics class so i'll do player statistics dot get younger player and it will pass in player 1 and player 2. we can see this test has passed but just to demonstrate the difference between assert equals and assert same i'm just going to change this to assert equals and i'm going to change this to player 1 so this really should be returning false because the player statistics get younger player should be returning player two but by using assert equals it will only be checking the name of player one to make sure that it's correct so asset equals will always return a player with the name patrick so therefore it the test will always pass when really what we want to do is check that the actual object is the younger one so that is why we use a set same because it will check the actual object memory location so this test will fail we are able to assert upon a boolean value which will allow us to test the under 30 method so the under 30 will return whether the player is under 30 or not so our player is under the age of 30 and therefore the statistics under 30 method will return true we can also do the same for asserting a false boolean using assert false so the assert force will pass because the player is not under 30 they're the age of 37 and the statistics under 30 will return false the equivalent of assert false and the search rule could be used by writing a set equals and then we can just pass in what is expected such as false and then what the actual value which will be and that is the statistics under 30. however by using assert true or assert false we've slightly improved the readability of our tests two further assertions which we can make can be made against whether a value returned is null and from the create csb record method we will be able to tell whether null has been returned or if the array has been returned so because the player has zero games we would expect the statistics create csv record to return null similarly we can assert that an object is also not null and because the number of games is three we would expect the create csv record to return a non-null report to complete the final type of assertion which we can make we're going to use the create csv record to assert the array that is returned using assert array equals so if the player has played four games and they have eight goals they will have two goals per game and they will average 0.5 games per goal which is what we expect expect the statistics.createcsv record to return that summarizes this video on assertions we have been able to apply seven different assertions to our class using the unit testing framework of junit there is one assertion that i've left out and that's called a set that however that can be quite a large topic so i will do a separate video on a set that in a future video i will also look at cleaning up this test class because we have quite a lot of repeated code for when we instantiate the player and also the player statistics instance
Info
Channel: Aneesh Mistry
Views: 16,529
Rating: undefined out of 5
Keywords: Software Engineering, Software Development, Java, JUnit, Testing framework, Assertion, Tutorial, Step-by-step, Aneesh Mistry Tutorial, Aneesh Tutorial, Aneesh Mistry, Java 8, Software Tutorial, Software Engineer, Mockito Tutorial, Junit Tutorial, Backend Engineer, IntelliJ, How to test software, Test Automation, testing software, beginner software engineer, learn to code, learning to code, step by step tutorial, JUnit Tutorial, JUnit Mockito, Assertions, JUnit assertions, Matchers
Id: lPHjo3EWeGA
Channel Id: undefined
Length: 8min 55sec (535 seconds)
Published: Mon Apr 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.