简体   繁体   中英

Junit - Testing constructor without parameters?

I have to create JUnit test cases for this class. One of the tests is to test the constructor of the ShannonsTheorem class. Are there ways to test a constructor that does not have any parameters?

There is another class named ShannonsModel that also needs to have its constructor tested. According to the UML we were provided there are no parameters on that constructor either.

Thanks!

package network;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ShannonsTheorem {

    ShannonsModel model = new ShannonsModel();
    Scanner kb = new Scanner(System.in);

    /**
     * default constructor for ShannonsTheorem class.
     * 
     */
    public ShannonsTheorem()
    {

    }

    /**
     * Method to return the value in bandwidth variable.
     * @return
     */
    public double getBandwidth()
    {
        return model.getBandwidth();
    }

    /**
     * getSignalToNoise method to return the signalToNoise value in variable signalToNoise
     * @return
     */
    public double getSignalToNoise()
    {
        return model.getSignalToNoise();
    }

    /**
     * main method for ShannonsTheorem
     * @param args
     * while loop to handle user input to continue or end program
     */
    public static void main(String[] args) 
    {
        try {

            Scanner kb = new Scanner(System.in);

            Scanner scan = new Scanner(System.in);
            boolean stop = false;    
            while(!stop) { 

                ShannonsTheorem ST = new ShannonsTheorem();


                System.out.println("Enter bandwidth in hertz:");
                ST.setBandwidth(kb.nextDouble());
                System.out.println("Enter signalToNoise:");
                ST.setSignalToNoise(kb.nextDouble());

                System.out.println("Values are:");
                System.out.println("Bandwidth"); 
                System.out.println(ST.getBandwidth());
                System.out.println("SignalToNoise:"); 
                System.out.println(ST.getSignalToNoise());
                System.out.println(ST.maxiumumDataRate());


                System.out.println("Press any key to make another calculation.  Type N or n to Quit");
                String s = scan.nextLine();
                if(s.equals("n") || s.equals("N")) {
                    stop = true;
                } // end of if
            } // end of while loop

        }catch (InputMismatchException e){

            System.out.println("Input Exception was caught, restart program");

        }catch(NumberFormatException e){

            System.out.println("Format Exception was caught, restart program");

        }   
    }

    /**
     * public method to retrieve the maximum data rate. This method makes a call to the private method 
     * under the same name.
     * @return
     */
    public double maxiumumDataRate()
    {
        // calling to the private method maxiumumDataRate. Storing the return value from said method into variable result
        // when this public method is called it will return the result from the private method, 
        double result = model.maxiumumDataRate();
        System.out.print(model.toString());

        return result;
    }

    /**
     * setBandwidth method to set the bandwidth value in hertz
     * @param h
     */
    public void setBandwidth(double h)
    {
        model.setBandwidth(h);
    }

    /**
     * setSignalToNoise method to set the signalToNoise variable
     * @param snr
     */
    public void setSignalToNoise(double snr)
    {
        model.setSignalToNoise(snr);
    }
}

Why do you need to test the constructor ?

You could test that without any modification, the default constructor has some specific fields :

@Test
public void shouldCreateADefaultShannonTheorem() {
    ShannonsTheorem shannonsTheorem = new ShannonsTheorem();

    Object expectedModel = new ShannonsModel();
    assertEquals(expectedModel , shannonsTheorem.model);
    Object expectedKb = new Scanner(System.in);
    assertEquals(expectedKb  , shannonsTheorem.kb);
}

or you could test that without any change, the default constructor gives you some results :

    ShannonsTheorem shannonsTheorem = new ShannonsTheorem();

    double expectedbandwith = 0.0;
    assertEquals(expectedbandwith , shannonsTheorem.getBandwidth(), 0);

    int expectedSignalToNoise = 0;
    assertEquals(expectedSignalToNoise  , shannonsTheorem.getSignalToNoise(), 0);

    int expectedMaximumDataRate = 0;
    assertEquals(expectedMaximumDataRate , shannonsTheorem.maxiumumDataRate(), 0);


    // ...

that's why it is usefull to do TDD (test first) :

  1. what your application should do ? write the test. // here is the thinking

  2. write the code. // no thinking here !

  3. refactor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM