Simple Junit Test Case Example in Java
What is JUnit?
JUnit is a simple, powerful, open source framework to write and run repeatable tests. I love JUnit testcases. During my Java Project development, I extensively use JUnit for code coverage. It is an instance of the xUnit architecture for unit testing frameworks.
JUnit features include:
- Assertions for testing expected results
- Test fixtures for sharing common test data
- Test runners for running tests
In this tutorial I'll cover point 1 and 3. You need below maven dependancies in your project.
< dependency > < groupId > junit </groupId > < artifactId > junit </artifactId > < version > 4.12 </version > </dependency > |
Let's begin writing code.
Step-1
Create projectCrunchifyJunitTest
and specify package com.crunchify.junit
. Here is a Package structure for quick reference.
Step-2
Right click on CrunchifyJunitTest
=> New Class =>CrunchifyJunitTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com . crunchify . junit ; /** * @author Crunchify * */ import static org . junit . Assert . assertEquals ; import org . junit . Test ; public class CrunchifyJunitTest { @ Test public void testingCrunchifyAddition ( ) { assertEquals ( "Here is test for Addition Result: " , 30 , addition ( 27 , 3 ) ) ; } @ Test public void testingHelloWorld ( ) { assertEquals ( "Here is test for Hello World String: " , "Hello + World" , helloWorld ( ) ) ; } public int addition ( int x , int y ) { return x + y ; } public String helloWorld ( ) { String helloWorld = "Hello +" + " World" ; return helloWorld ; } } |
Step-3
Eclipse will suggest and automatically addorg.junit.Test
dependency once you type @Test.
Step-4
To test your JUnit test
- Right click on class
- Click on Run As
- Click on
JUnit Test
You should see something like this.
Step-5
Try to change method expected value param from 30 => 300
and from Hello World => Hello -- World
and test again.
@ Test public void testingCrunchifyAddition ( ) { assertEquals ( "Here is test for Addition Result: " , 300 , addition ( 27 , 3 ) ) ; } @ Test public void testingHelloWorld ( ) { assertEquals ( "Here is test for Hello World String: " , "Hello -- World" , helloWorld ( ) ) ; } |
Step-6
Now lets run the same test case via other java
program. CreateCrunchifyRunnerJUnit.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com . crunchify . junit ; import org . junit . runner . JUnitCore ; import org . junit . runner . Result ; import org . junit . runner . notification . Failure ; /** * @author Crunchify * */ public class CrunchifyRunnerJUnit { public static void main ( String [ ] args ) { Result result = JUnitCore . runClasses ( CrunchifyJunitTest . class ) ; for ( Failure failure : result . getFailures ( ) ) { System . out . println ( failure . toString ( ) ) ; } if ( result . wasSuccessful ( ) ) { System . out . println ( "Both Tests finished successfully..." ) ; } } } |
Step-7
Run CrunchifyRunnerJUnit.java
as a Java Application
.
Step-8
In case of 1st code
(correct expected param), you should see output like this:
Both Tests finished successfully . . . |
Step-9
In case of 2nd code
(incorrect expected param), you should see something like this:
testingCrunchifyAddition ( com . crunchify . junit . CrunchifyJunitTest ) : Here is test for Addition Result : expected : < 300 > but was : < 30 > testingHelloWorld ( com . crunchify . junit . CrunchifyJunitTest ) : Here is test for Hello World String : expected : < Hello [--] World > but was : < Hello [+] World > |
And you are all set. Do let me know if you see any trouble running this code.
Available Annotation in JUnit testcase:
Annotation | Description |
---|---|
@Test public void method() | The annotation @Test identifies that a method is a test method. |
@Before public void method() | Will execute the method before each test. This method can prepare the test environment (e.g. read input data, initialize the class). |
@After public void method() | Will execute the method after each test. This method can cleanup the test environment (e.g. delete temporary data, restore defaults). |
@BeforeClass public void method() | Will execute the method once, before the start of all tests. This can be used to perform time intensive activities, for example to connect to a database. |
@AfterClass public void method() | Will execute the method once, after all tests have finished. This can be used to perform clean-up activities, for example to disconnect from a database. |
@Ignore | Will ignore the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included. |
@Test (expected = Exception.class) | Fails, if the method does not throw the named exception. |
@Test(timeout=100) | Fails, if the method takes longer than 100 milliseconds. |
Available Assert Test Methods:
Statement | Description |
---|---|
fail(String) | Let the method fail. Might be used to check that a certain part of the code is not reached. Or to have failing test before the test code is implemented. |
assertTrue(true) / assertTrue(false) | Will always be true / false. Can be used to predefine a test result, if the test is not yet implemented. |
assertTrue([message], boolean condition) | Checks that the boolean condition is true. |
assertsEquals([String message], expected, actual) | Tests that two values are the same. Note: for arrays the reference is checked not the content of the arrays. |
assertsEquals([String message], expected, actual, tolerance) | Test that float or double values match. The tolerance is the number of decimals which must be the same. |
assertNull([message], object) | Checks that the object is null. |
assertNotNull([message], object) | Checks that the object is not null. |
assertSame([String], expected, actual) | Checks that both variables refer to the same object. |
assertNotSame([String], expected, actual) | Checks that both variables refer to different objects. |
Source: https://crunchify.com/simple-junit-4-tutorial-hello-world-example/
Enviar um comentário for "Simple Junit Test Case Example in Java"