top of page
grishmamahajan56

Overview of Junit

Junit is unit testing framework for java which is simple and open-source framework. Unit testing is small piece of code that help you to identify bug in early stages, it is one of the best methods for regression testing.

Junit help early bug identification in the initial stage to the developers, so no need to spend lot of time to read and understand the whole code . make sure that the new change are working fine and has no negative impact on existing functionality, regression testing comes in picture, by using Junits it will be easy and fast to run all the test cases and identify issues in whole system.

Junit is also help to identify bug 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.

Junit provide reliable and readable code .

















Finding a bug is easy in small piece of code its boost the confidence of developer’s .


Annotation provides by Junit

  1. @Test – The @Test annotation is used to run a Junit test.

  2. @Before - @Before annotation is used to run any specific test before each test.

  3. @After - This method executes after each test.

  4. @BeforeClass - This method executes once before running all test. Initialization of properties files, databases etc are done in the beforeClass method.

  5. @AfterClass - Like @BeforeClass, @AfterClass executes once after executing all test methods.

  6. @Ignores - This annotation can be used if you want to ignore some statements during test execution for e.g. disabling some test cases during test execution.

  7. @Test(timeout=500) - This annotation can be used if you want to set some timeout during test execution for e.g. if you are working under some SLA (Service level agreement), and tests need to be completed within some specified time.

  8. @Test(expected=IllegalArgumentException.class) - This annotation can be used if you want to handle some exception during test execution. For, e.g., if you want to check whether a particular method is throwing specified exception or not.

eg


Output



JUnit Assert Class

Assert is a method useful in determining Pass or Fail status of a test case, The assert methods are provided by the class org. junit. Assert which extends java. If all assert statements are passed, test results are successful. If any assert statement fails, test results are failed.Junit Assert class provide list of methods

  1. void assertEquals(boolean expected, boolean actual) - It checks whether two values are equals similar to equals method of Object class

  2. void assertFalse(boolean condition) - functionality is to check that a condition is false.

  3. void assertNotNull(Object object) - “assertNotNull” functionality is to check that an object is not null.

  4. void assertNull(Object object) - “assertNull” functionality is to check that an object is null.

  5. void assertTrue(boolean condition) - “assertTrue” functionality is to check that a condition is true.

JUnit TestCase Class

TestCase class is belong 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 countTestCases() - Its return integer value of how many test cases has been executed in your test suite.

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

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

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

  • void setName(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 .eg if u want to login into website, then u need to login first and need to open login page appear.

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

Reporting In JUnit-

By default, JUnit tests generate simple report XML files for its test execution. These XML files can then be used to generate any custom reports as per the testing requirement. We can also generate HTML reports using the XML files.The Micro Focus Unit Testing Framework is capable of producing JUnit-style reports for each individual test case, even when there are multiple test cases in a test run. To generate JUnit HTML report from our maven project. Add maven-surefire-report-plugin to pom.xml reporting element.



This kind of report (Micro Focus Unit Testing) junit is provide.


Conclusion -


Junit is unit testing framework for java .JUnit is beautifully simple. It is less complex and takes less time. JUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results.we see junit is having lots of advantageous but there are some disadvantage also there ,Like its not suitable for higher level testing i.e. for large test suites. Group testing cannot be done in JUnit









61 views

Recent Posts

See All
bottom of page