What is JUnit?
➔ JUnit is a unit testing framework for Java programming language.
➔ It plays a crucial role test-driven development, and is a family of unit testing
frameworks collectively known as xUnit.
➔ JUnit promotes the idea of "first testing then coding", which emphasizes on setting
up the test data for a piece of code that can be tested first and then implemented.
➔ This approach is like "test a little, code a little, test a little, code a little." It increases
the productivity of the programmer and the stability of program code, which in
turn reduces the stress on the programmer and the time spent on debugging.
3.
Features of JUnit
➔JUnit is an open source framework, which is used for writing and running
tests.
➔ Provides annotations to identify test methods.
➔ Provides assertions for testing expected results.
➔ Provides test runners for running tests.
➔ JUnit tests allow you to write codes faster, which increases quality.
➔ JUnit is elegantly simple. It is less complex and takes less time.
4.
What is aUnit Test Case ?
➔ A Unit Test Case is a part of code, which ensures that another part of code
(method) works as expected.
➔ A formal written unit test case is characterized by a known input and an expected
output, which is worked out before the test is executed.
➔ The known input should test a precondition and the expected output should test a
post-condition.
➔ Unit Test case will test the given code and execute code with given input and from
responsible code the input data will be passed through and give output.
5.
What is Mocking?
➔Mocking is a process of developing the objects that act as the mock or clone of the real
objects. In other words, mocking is a testing technique where mock objects are used instead
of real objects for testing purposes.
➔ There are three key concepts of mocking, i.e., stub, fake, and mock.
◆ Stub: Stub objects hold predefined data and provide it to answer the calls during testing. They are
referred to as a dummy object with a minimum number of methods required for a test. It also provides
methods to verify other methods used to access the internal state of a stub, when necessary. Stub
object is generally used for state verification.
◆ Fake: Fake are the objects that contain working implementations but are different from the production
one. Mostly it takes shortcuts and also contains the simplified version of the production code.
◆ Mock: Mock objects act as a dummy or clone of the real object in testing. They are generally created by
an open-source library or a mocking framework like Mockito, EasyMock, etc. Mock objects are typically
used for behavior verification.
6.
Mockito
➔ Mockito isa mocking framework that tastes really good. It lets you write beautiful
tests with a clean & simple API. Mockito doesn’t give you hangover because the
tests are very readable and they produce clean verification errors.
➔ Mockito is a Java-based library used to create simple and basic test APIs for
performing unit testing of Java applications. It can also be used with other
frameworks such as JUnit and TestNG.
➔ Two important prerequisites are to be kept in mind before starting with learning
Mockito are:
◆ Java: Hands-on experience in Java programming.
◆ JUnit: Basic understanding of the JUnit Framework.
7.
Mockito Anotations
➔ @ExtendWith(MockitoExtension.class)
◆This annotation will annotate the JUnit test with a MockitoJUnitRunner:
This will allows you to use the Mockito framework to create and inject mocked
objects into your JUnit 5 test classes.
➔ @InjectMocks
◆ This annotation will inject mock fields into the tested object automatically.
◆ It is used to create class instances that need to be tested in the test class.
◆ It is used when we need all internal dependencies initialized with mock objects to
work the method correctly.
8.
➔ @Spy:-
◆ Thisannotation to spy on an existing instance.
◆ This will instrumented to track all the interactions with it.
◆ This annotation is used to call the actual method that is being called to get the
expected value or can say test the code.
➔ @Mock:-
◆ This annotation will create mock class and interfaces while run test cases.
◆ We can use @Mock to create and inject mocked instances without having to call
Mockito.mock manually.
◆ It makes it easier to find the problem mock in case of a failure, as the name of the
field appears in the failure message
9.
Benefits Of Mockito
➔No handwriting -
◆ The developers do not need to write their Mock codes. Like create a new Mockito class with
same parameters.
➔ Safe Refactoring -
◆ Even if an interface method is renamed or the parameters are reordered, the test codes created
as Mocks will not break.
◆ It will give errors when the main method updated and there is missing parameter in the mock
method.
➔ Exception support -
◆ Mockito enables exceptions.
➔ Annotation support -
◆ Mockito supports the creation of Mocks with annotation.
➔ Order Support -
◆ Mockito allows checking on the order of method calls.
10.
What is JUnitAnnotations?
➔ JUnit Annotations is a special form of syntactic meta-data that can be added to Java
source code for better code readability and structure. Variables, parameters, packages,
methods and classes can be annotated.
➔ Annotations make it easy to write and execute tests by simplifying the code, improving
readability, and reducing the need for boilerplate code
➔ The @Test annotation is one of the most commonly used JUnit annotations and is used to
mark a method as a test method that JUnit should run.
➔ Annotations were introduced in Junit4, which makes Java code more readable and simple.
This is the big difference between Junit3 and Junit4 that Junit4 is annotation based.
11.
Junit Annotations UsedFor Testing
@Test This annotation is a replacement of org.junit.TestCase which
indicates that public void method to which it is attached can be
executed as a test Case.
@BeforeEach This annotation is used if you want to execute some statement
such as preconditions before each test case.
@AfterEach This annotation can be used if you want to execute some
statements after each Test Case for e.g resetting variables,
deleting temporary files ,variables, etc.
@Parameterized This annotation enables the test execution of a single test
method multiple times with different parameters.
12.
JUnit5 Assertions ClassMethods
➔ Junit test case is incomplete with the assertion class methods. Few methods are
assertEquals(),assertTrue() etc.
➔ Assertions are utility methods to support asserting conditions in tests.
➔ These methods are accessible through the Assert class in JUnit 4, and the
Assertions class in JUnit 5.
➔ There is a big change in the assertions methods with JUnit5 is added one more
parameter contain the message like following.
◆ assertFalse(condition, "5 is not greater then 6");
➔ Assert methods verify the output with the expected output from the testcase.
13.
assertEquals() This methodis used to compare two values are equals
like expectedValue and outputValue from the given
method.
assertNotEquals() This method is used to compare two values are not
equals like expectedValue and outputValue from the
given method.
assertNull() This method is used to check the expected output will be
null or not.
assertNotNull() This method is used to check the expected output will be
not null or not.
assertTrue This method is used to check the expected output will be
true.
assertFalse() This method is used to check the expected output will be
false.
14.
Create Junit TestClass
➔Create Junit test class from any service processminer modules.
➔ Write the Test case that cover main class methods.
➔ Show how the Test case will run.
➔ How to check the coverage processminer service class.
➔ Benefits of junit are as follows with processminer.
◆ It will cover all the code possibility so there will be less percentage of error and code
failure.
◆ It will help to write good quality code and it will stop you to write faulty code when each
component is depended on other .
◆ With the help of junit test case we can test the behaviour of every chunks of code in given
method.
15.
Drop Backs ofJunit Test Case
➔ Order is not guaranteed:-
◆ JUnit doesn’t guarantee that tests will run in the order.
◆ Group testing cannot be done in JUnit, which is available in TestNG.
◆ Can’t create HTML reports of test cases. You need to use ANT to create tests HTML
reports.