The document provides an overview of unit testing in Java using the JUnit 5 framework, detailing the steps to set up the testing environment and execute tests. It covers the use of annotations, assertion methods, and the test-driven development approach. Additionally, it explains how to perform tests without an IDE and introduces various assertion methods and annotations for better test management.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
3 views2 pages
JUnit 5
The document provides an overview of unit testing in Java using the JUnit 5 framework, detailing the steps to set up the testing environment and execute tests. It covers the use of annotations, assertion methods, and the test-driven development approach. Additionally, it explains how to perform tests without an IDE and introduces various assertion methods and annotations for better test management.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2
#This is all about the unit testing using the java in the JUnit 5 framework
but first comes the manual way to do it-
--in testing we call method as UNITS----
$ steps to test a program--
1.prepare (setup test environment, write test methods) 2.provide testing input 3.run the time 4.provide expected output 5.perform assertion(varify the result) 6.report test results (alert the developer if the test passes or it fails)
$ JUnit 5 take care of the common steps to test a unit of program
except the --- 1.intput 2.output expected 3.and preparations of testing which are the testing method etc. ----------------------------------------------------------------------------------- ------------------------------------------------------------------------ #setup the junit 5-- 1.open a new project in intellij and mark as a maven project. 2.open pom.xml file and add dependencies from the (maven junit repository) and open the unit testing and tools. 3.click on JUint at first and select version and copy paste the code in the pom.xml file. 4.force start the file using M symbol on right. 5.make the testing file in test folder of the same package as the file is. 6.you're ready to go! ----------------------------------------------------------------------------------- ------------------------------------------------------------------------ The @Test annotation -- marks the method for testing other work would be prohibited. 1.the method that has the test annotation can be public, protected or default. (except Private). 2.seccess is default in junit 5, it just check for the faliures and if all test pass then it show default result. 3.Assertion method(static method) -- assertEquals(exp, actual) and assertArrayEquals(exp, actual); 4.we must make the instance for the in the test method of the class which we're running the test as--- main c= new main(); ----------------------------------------------------------------------------------- ------------------------------------------------------------------------ #In the test driven development we follow the steps- 1.we first make the class in the actual program, then add pom xml file and the test file in same package along with the dependency of maven. 2.we don't wirte the content of the method in actual file but first write the test file to let it fail first. 3.then write the code in the method of actual code file and see if the method is working properly which would be suggested by test result. ----------------------------------------------------------------------------------- ------------------------------------------------------------------------ #work out the junit without using the IDE's --- 1.add plugin -- maven sunfire. 2.add plugin -- maven-compilor. 3.add dependency -- junit 5 (usual). 4.download the maven and add it to path using enironmental variables. ----------------------------------------------------------------------------------- ------------------------------------------------------------------------ #Some more assertEquals()--methods. 1.assertEquals(expected, realValue, "the test is failing because of etc etc.").----------write the message in case of failing the test. 2.you can do the above using implementation of supplier interface. ex- assertEquals(expected, realvalue, ()->"the test is failing because of etc etc.").----------alternate method. both used to give the message in case of the failure but the first one, evaluates once and in socond, string evaluation is done in case of faliure. ----------------------------------------------------------------------------------- ------------------------------------------------------------------------ # assertNotEquals(expected, actual)------if same the result will fail, otherwise pass. # assertNotEquals(expected, actual, "this is a failiure)------if fails then shows this message. # assertTrue(example{true})------if the value inside it is true then test passes. # assertFalse(example{false})-----if the value inside false then it is gonna be false. # assertArrayEquals(expected, actual)------both arrays should have same elements and in same order also the equal no. of elements. ----------------------------------------------------------------------------------- ------------------------------------------------------------------------ # To check the performance of the method-- 1. assertTimeout(duration,method calling)--------now duration can be in whatever time unit and method calling is usual. example--assertTimeout(Duration.ofMillis(30),()->e.array(unSorted)); ----------------------------------------------------------------------------------- ------------------------------------------------------------------------ #Some more Annotations-- @BeforeEach the unit that has tagged BeforeEach will be used before all the test units on it's own. @AfterEach also used similar as for the before each all examples are implemented in the the practice programs named project 1 to 6. ----------------------------------------------------------------------------------- ------------------------------------------------------------------------ @TestInstance(TestInstance.LifeCycle.perClass) this annotation is for the class and it makes the class instance work per class or once instead of the perMethod which is a default application.