top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Learnings on JUnit Testing

Hello Techies,

Here I am going to Explore Tags, Testcase class, Assertions and some more basics knowledge in JUnit Testing. This Blog will explore the capabilities & Limitations of Tags Let’s begin!!

 

Overview Of JUnit:

It is a Framework of Java Programming Language to perform unit testing. This framework is used to write and execute automated tests by Java Developers. It plays a crucial role in test-driven Development (TDD), and it is a family unit testing framework. Junit is also helpful to identify bugs in the initial stage of the development, so no need to spend lot of time to read and understand the whole code. JUnit has several graphs that represent the progress of a test. When the test runs smoothly, the graph displays a green color, and it turns red if the test fails. JUnit Testing enables developers to develop highly reliable and bug-free code.

 

What are Tags?

It is a kind of label’s you can attach to your test methods or classes to a group or filter them. It is useful for running specific subsets of tests based on different criteria. If you want to add a tag to all test methods found from your test class, you must annotate your test class with the @Tag annotation. The JUnit annotations are predefined texts provided by Java API, which helps JVM to recognize the type of method or class for testing. 

                                                                              

 

How to use Tags?

Tags are used to annotate your test methods or test classes with the @Tag annotation. Annotations Can be applied to declaration of classes, fields, methods, and other program elements. These tags provide additional information to the compiler and JVM.

 

Here are some @tags we use in JUnit testing,

Annotations in JUnit Test:

  • @Test

  • @Before

  • @After

  • @Ignores

  • @Beforeall

  • @Afterall

 

 Maven Configuration to run JUnit Test:

 

      



  Since the surefire plugin doesn’t fully support JUnit, we will also need to add a provider which tells maven where to find our test.



 

                       

1.@Test Annotation:

Informs JUnit about which public void method it should run as a test case. This Annotation is a powerful tool for performing unit and regression testing.

 

2. @Before Annotation:

 Used to execute statement before a test case. It will be very useful when we want to execute some common codes before running a test.

 

3. @After Annotation:

                Used to execute a statement after a test case.

4. @Ignores Annotation:

Used to ignore a statement during the execution process. Also, you can Annotate a class containing test methods with @Ignore and none of the containing tests will be executed.

 

5.@Beforeall Annotation:

Used to execute a statement before all the test cases. It will give a signal to annotate the method should run before all tests in the current test class.

6. @afterall Annotation:

Used to execute a statement after all the test cases. Afterall methods are inherited from super classes as long as they are not overridden according to the visibility of the Java Language.


Runner Class For JUnit:



Here are some sample testcases for Annotations:






JUnit Testcase Class

Testcase class has belonged to Junit test framework, this class has very useful methods those used in your test case.

 

If u want any information about your test case or test suites, you need Testcase class.

 

Below are some widely used method from this class.

 

 

  • int count Testcases () - Its return integer value of how many test cases has been executed in your test suite.

  • Test Result Create Result () - In this method you can provide result and write a code to create a result or log result.

  • String gets name () - In this method u can get the name of what test case is executed.

  • Test Result run () - This method is used to execute a test which returns a Test Result object.

  • void set Name (String name) - In this method, if you are having one test case and u want to set the name of test case or want to rename that test case in this situation you can use this method.

  • void Setup () - Here you are initializing object of test case. if u want to login into website, then u need to login first and need to open login page appears.

  • Teardown () -This method is used to write resource release code. e.g. Release database connection after performing transaction operation.

 

 

 How to use Assert in JUnit:

Assert true ()

By passing condition as a Boolean parameter used to assert in JUnit with the assert True () method. It throws an Assertion Error (without message) if the condition given in the method isn't True. By passing two parameters simultaneously in the assert True () method. One parameter would be an assertion error message, and the second parameter would specify the condition against which we need to apply our assertion method as True. It throws an Assertion Error (with message) if the condition given in the method is not True.

 

Assert Not Null ()

             Assert Not Null () method checks if the provided object does not hold a null value. We can pass an object as a parameter in this method and get an assertion error if the object we pass does hold “NULL” values along with the assertion error message if provided.

Assert False ()

          This method is to verify whether a given condition is false or not. Incase if the condition does not match in that case the assertion error will be thrown and the program execution will terminate at this same line, the assertion statement itself.

Assert Equals ()

Checks that two primitives/objects are equal.

Assert Null ()

Checks that an object is null.

Assert Same (object1, object2)

The assert Same () method tests if two object references point to the same object.

Assert Not Same ()

The assertors () method tests if two object references do not point to the same object.

Assert Array Equals (expected Array, result Array)

The assert Array Equals() method will test whether two arrays are equal to each other.



Conclusion:

In this blog we have explored more about Annotation and Assertions. I have learnt a lot about the JUnit test by writing this blog. Feel free to share your experience in the comments section. Let’s learn together and gain more knowledge on JUnit.

 

Thank You!

Happy Learning!

 

 

 

                                                                                    

 

 

 

 

 

 

 

 

 

 

 

4 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page