javatestingblack-box-testingwhite-box-testing

Whitebox and Blackbox testing


I ve been reading for the whitebox and blackbox testing.

If im not wrong:

Whitebox tests the implementation of a Software Program, but blackbox tests the inputs and outputs.

Can someone please give me an example of a simple code for the both cases?

Thank you in advance.

so, the code here is a blackbox testing?

  class Schalter
  {
     private boolean
     {
       private boolean _istAn;
       public Schalter(boolean anfangsAn)
       {
          _istAn = anfangsAn;        
       }       
       public boolean istAn()
       {
          return _istAn;
       }   
       public void umschalten()
       {
         _istAn = !_istAn;
       }
  }

Solution

  • Blackbox -> you're really just checking if you're getting correct output for the input you gave your program.

    Say you have a prompt that asks you to enter 2 digits to get the sum of the numbers.

    Enter 2 numbers: 2 5 output: 2 + 5 = 7

    That's all there is to black box really.


    White box you would want to check to see HOW it's happening.

    You could do the normal thing which would be something like

    int adder(int firstNum, int secondNum)
    { 
        return firstNum + secondNum;
    }
    

    this is more efficient than say something like:

    int adder(int firstNum, int secondNum)
    {
        int temp;
        for(int i = 0; i < (firstNum + secondNum + 1); i++)
            temp = i;
        return temp;
    }
    

    In whitebox testing you would look at your code and figure out which is more efficient and/or is easier to read. Obviously the first one is since:

    1. the code is simpler and easier to understand
    2. The first does not involve loops to find the answer this takes up more processing time than the first
    3. The first doesn't create extra variables that are not needed. This means less memory needed to keep track of the data.

    This is a simple and arbitrary example, but when you get into larger projects you'll do a lot of whitebox testing when you do unit tests to figure out if smaller segments of your code works and you would normally do black box testing when you start integrating the smaller segments of your code into the larger project to check if you were still getting correct output for given input.

    Another way to look at it you could use blackbox testing to see if you were getting bad output and if so then you could go in and do whitebox testing to see what you were doing wrong in your code.