-1

Here is the code. I am wondering about how to create a test for the constructor. There are no getters for the last two fields.

public class Log implements Reporter {

    /**The number of passengers processed*/
    private int numCompleted;
    /** The total wait time.*/
    private int totalWaitTime;
    /** The total process time*/
    private int totalProcessTime;



    /**
     * The log constructor.
     */
    public Log() {
        this.numCompleted = 0;
        this.totalWaitTime = 0;
        this.totalProcessTime = 0;

    }

    @Override
    public int getNumCompleted() {

        return numCompleted;
}
Sam
  • 3
  • 5
  • Can you share the full class? – codeLover Oct 16 '18 at 04:13
  • It depend on your criteria test. Example, after create the instance, you can you Whitebox (https://static.javadoc.io/org.powermock/powermock-reflect/1.6.4/org/powermock/reflect/Whitebox.html) to get the value of field and assert the value. – Cường Lư Quốc Oct 16 '18 at 04:15
  • Depends what behavior you're trying to test. As presented, your class does nothing. – shmosel Oct 16 '18 at 05:28
  • [It is impossible to answer your question because you do not provide a specification of what your code ought to do](https://stackoverflow.com/a/53757321/545127). – Raedwald Dec 13 '18 at 08:54

1 Answers1

5

In the same way you would write a test for a constructor with parameters. Instantiate the object and afterwards verify the state is matching your expectations.

Sample entity:

public class Log {

    private int numCompleted;
    private int totalWaitTime;
    private int totalProcessTime;

    public Log(){
        this.numCompleted = 0;
        this.totalWaitTime = 0;
        this.totalProcessTime = 0;
    }

    public Log(int numCompleted, int totalWaitTime, int totalProcessTime) {
        this.numCompleted = numCompleted;
        this.totalWaitTime = totalWaitTime;
        this.totalProcessTime = totalProcessTime;
    }

    public int getNumCompleted() {
        return numCompleted;
    } 

    public int getTotalWaitTime() {
        return totalWaitTime;
    }

    public int getTotalProcessTime() {
        return totalProcessTime;
    }

}

Sample tests:

@Test
public void testNoArgConstructor(){
    Log log = new Log();
    assertEquals(0, log.getNumCompleted());
    assertEquals(0, log.getTotalWaitTime());
    assertEquals(0, log.getTotalProcessTime());
}

@Test
public void testArgConstructor(){
    Log log = new Log(1,2,3);
    assertEquals(1, log.getNumCompleted());
    assertEquals(2, log.getTotalWaitTime());
    assertEquals(3, log.getTotalProcessTime());
}
  • Could you remind me how to do that? – Sam Oct 16 '18 at 04:51
  • But how would it be like a constructor with parameters when there are no parameters in the log constructor? – Sam Oct 16 '18 at 05:16
  • @Sam Added example with arg / no-arg constructor and associated tests –  Oct 16 '18 at 05:27
  • I don't have the getters for the second and third assertEquals statements. This what I have so far: Log log = new Log(); assertEquals(0, log.getNumCompleted()); But for the second statement below, I am not sure what to do. Currently, Java won't allow this since the constructor does not have any parameters Log log1 = new Log(0,0); – Sam Oct 16 '18 at 16:00
  • @Sam I assume both `totalWaitTime` and `totalProcessTime` expose some behavior on `Log` class. If you don't have getters on them (which is fine by the way) you should test that behavior. –  Oct 16 '18 at 17:53