0% found this document useful (0 votes)
47 views15 pages

Junit

JUnit is a unit testing framework for Java that allows developers to write and run repeatable tests to ensure code quality. It is designed to be simple, easy to use, and provides feedback on test failures. Tests can be run in parallel to reduce execution time.

Uploaded by

Akshobhya yash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views15 pages

Junit

JUnit is a unit testing framework for Java that allows developers to write and run repeatable tests to ensure code quality. It is designed to be simple, easy to use, and provides feedback on test failures. Tests can be run in parallel to reduce execution time.

Uploaded by

Akshobhya yash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Junit

JUnit
JUnit is a unit testing framework for the Java programming language. It is
an open-source project hosted on GitHub. It is used to write and run
repeatable tests. It is an essential tool for software developers to ensure
that the code they write is of high quality and works as expected.

JUnit is designed to be simple and easy to use. It enables developers to


write tests quickly and easily, and also provides detailed feedback when
tests fail. It also allows developers to run tests in parallel, which reduces
the time it takes to run tests.
BeforeClass
The @BeforeClass annotation is used to indicate a method which should
be run before all tests have been run. This method is used to perform any
necessary setup tasks, such as establishing database connections or
creating temporary files.

**always use public static void


AfterClass
The @AfterClass annotation is used to indicate a method which should be
run after all tests have been run. This method is used to perform any
necessary cleanup tasks, such as closing database connections or deleting
temporary files.
Before
The @Before annotation is used to indicate a method which should be run
before each test method. This method is used to perform any necessary
setup tasks, such as establishing database connections or creating
temporary files.

**always use public void


After
The @After annotation is used to indicate a method which should be run
after each test method. This method is used to perform any necessary
cleanup tasks, such as closing database connections or deleting temporary
files.
DisplayName
The @DisplayName annotation is used to indicate a custom name for the
Junit test classes and also junit test methods. If this annotation is not used
then by default the class name and the test method name will be printed
when the test cases are executed. This annotation takes a string as an
input and that string can have characters, numbers, special characters as
well as emojis too.
Disabled(J5)/Ignore
The @Disabled/@Ignore annotation is used to disable entire test classes or
test methods. It allows us to skip or disable tests. It also takes in an
optional parameter string that allows the user to know the reason for
skipping or disabling that very test case.
Assertions
There are multiple assertions functions in junit such as assertTrue,
assertFalse, assertNull, assertNotNull, assertEquals, assertNotEquals,
assertArrayEquals, assertTimeout etc.
Testing exceptions and performance
The @Test(expected = ) is used to test the expected exception in JUnit. For
testing the performance of the JUnit test you can add timeout = to the test
annotation to check the performance. Note that in Junit 5 you have a
separate annotation for Timeout
RunWith
The @RunWith annotation is used to indicate which class should be used
to run the tests. This allows developers to use different test runners, such
as JUnitCore, JUnitParams, Suite.class or parameterized.class.
Parameter
The @Parameter annotation is also used to indicate a method which
should be run with a specific set of parameters. This allows developers to
specify the parameters that will be passed to the test methods, ensuring
that the tests are run with the correct parameters.
Assumptions
The assumptions in JUnit are pre-conditions that needs to be satisfied in
order to run the subsequent statements and assertions in the test case. If
an assumptions is not satisfied then the program will simply skip the test
case, throwing the TestAbortedException and the test case will pass. There
are assumptions such as assumeTrue, assumeFalse, assumingThat etc.
TestMethodOrder(J5)
The annotation @TestMethodOrder is used to specify the order in which the
test cases will be executed. We can use Alphanumeric,class order,
Lexicographical MethodName.class order, Random.class order or custom
order such as OrderAnnotation.class
TestInstance(J5)
The annotation @TestInstance is used to specify whether a variable will be
shared among all the methods present in the class or it will not be shared
among different methods and will remain un-influenced by the methods
using the variable. Thus, we have two cases here. Lifecycle.PER_CLASS
and Lifecycle.PER_METHOD

You might also like