3

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);
    }
}
4
  • 1
    Test it same way as constructor with parameters, just do not pass any parameters in it. Commented Oct 7, 2016 at 14:23
  • What talex said. Not much different. If you wanted to get more specific you could test the expected instance variables of the instantiated class and make sure they are what you expect for the no arg constructor. Commented Oct 7, 2016 at 14:26
  • What would I test exactly? The class doesn't have any fields for the constructor to initialize either. I think that's where I'm confused more than anything. Commented Oct 7, 2016 at 14:27
  • oh okay I'll attempt to test the variables and see where that takes me. Thanks! Commented Oct 7, 2016 at 14:28

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.