Test a project using JUnit 

Open your project that we generated previously and follow these steps:

  1. Go to the testkotlin | com.packtpub.sunnat629.testing_application, as in this screenshot:

  1. Now create a class named JUnitTestClass.kt where we will create some test cases using the annotations. Here is the sample code:
class JUnitTestClass {

companion object {
@BeforeClass
@JvmStatic
fun runBeforeClass(){
println("============ @BeforeClass ============ ")
}

@AfterClass
@JvmStatic
fun runAfterClass(){
println("============ @AfterClass ============")
}
}

@Before
fun runBefore(){
println("============ @Before ============")
}

@After
fun runAfter(){
println("============ @After ============ ")
}

@Test
fun runTest1(){
println("============ @TEST One ============")
}

@Test
fun runTest2(){
println("============ @TEST Two ============")
}
}

You can see that we have written the @BeforeClass and @AfterClass annotated function in the companion object {}, which means these functions are static. In Kotlin, you have to write the static variables and functions in the companion object {}.

We have used the @JvmStatic annotation. This is especially used in Kotlin to specify that this function is static and needs to be generated in the element of this function.

  1. Now run this test by clicking the Run Test icon beside the function name, as in the following screenshot:

After running the test on all the test cases, it will show the results; namely, pass or fail. Here is the output:

Here you can see that we had two test cases named runTest1 and runTest2 that have passed the test. 

  1. Now modify our runTest1 function and write logic:
@Test
fun runTest1(){
println("============ @TEST One Start ============")
assertEquals(6, doSum(3,2))
println("============ @TEST One End ============")
}

private fun doSum(num1: Int, num2: Int): Int{
return num1 + num2
}

Here, we have done a very simple equation to check the testing function. We have used a method of Assert class. The assertEquals() is a method of assert, and mainly checks the equality with the two inputs. Here, for example, we provide 6 and (2+3), which is not true and it will show an error.

If the equation is correct, then you will see the test is passed or it will show an error with the expected result. Here is what the result looks like:

There are a lot of Assert methods. Here are some of them:

  • The assertArrayEquals: This will return the equality of two array types input
  • The assertEquals: This will return the equality of two same types of input such as int, long, double, String, and so on
  • The assertTrue: This will assert that the given condition is true
  • The assertFalse: This will assert that the given condition is false
  • The assertNotNull: This will assert that the given object is not null
  • The assertNull: This will assert that the given object is null
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.106.135