How to write a Unit Test?

后端 未结 5 2031
面向向阳花
面向向阳花 2020-11-22 07:39

I have a Java class. How can I unit test it?


In my case, I have class does a binary sum. It takes two byte[] arrays, sums them, and returns a new

5条回答
  •  暖寄归人
    2020-11-22 08:13

    1. Define the expected and desired output for a normal case, with correct input.

    2. Now, implement the test by declaring a class, name it anything (Usually something like TestAddingModule), and add the testAdd method to it (i.e. like the one below) :

      • Write a method, and above it add the @Test annotation.
      • In the method, run your binary sum and assertEquals(expectedVal,calculatedVal).
      • Test your method by running it (in Eclipse, right click, select Run as → JUnit test).

        //for normal addition 
        @Test
        public void testAdd1Plus1() 
        {
            int x  = 1 ; int y = 1;
            assertEquals(2, myClass.add(x,y));
        }
        
    3. Add other cases as desired.

      • Test that your binary sum does not throw a unexpected exception if there is an integer overflow.
      • Test that your method handles Null inputs gracefully (example below).

        //if you are using 0 as default for null, make sure your class works in that case.
        @Test
        public void testAdd1Plus1() 
        {
            int y = 1;
            assertEquals(0, myClass.add(null,y));
        }
        

提交回复
热议问题