Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

junit - Learning Java Test-Driven Development

I'm learning Java Test-Driven Development, with JUnit 4. I have been given a test scenarios, and have to write the implementation code, to get the tests to pass.

This is the test scenario:

package java;

import static org.junit.jupiter.api.Assertions.assertFalse;

import java.time.LocalDateTime;
import org.junit.jupiter.api.Test;

class CallCenterTests {

  private final CallCenter callCenter = new CallCenter();
  private final LocalDateTime currentTime = LocalDateTime.of(2021, 1, 12, 17, 24);


  @Test
  public void testWillNotAcceptOutOfHours() {
    assertFalse(callCenter.willAcceptCallback(currentTime, LocalDateTime.of(2021, 1, 12, 20, 15)));
  }

  @Test
  public void testWillNotAcceptLessThanTwoHoursInFuture() {
    assertFalse(callCenter.willAcceptCallback(currentTime, LocalDateTime.of(2021, 1, 12, 18, 26)));
  }

  @Test
  public void testWillNotAcceptMoreThanSixWorkingDaysInFuture() {
    assertFalse(callCenter.willAcceptCallback(currentTime, LocalDateTime.of(2021, 1, 18, 12, 1)));
  }

}

This is what I know looking at the test scenarios:

There has to be written a class called CallCenter, where callCenter is the object reference. We are using the LocalDateTime class, with a object reference called currentTime, which has parameter values of todays date and time. There is a willAcceptCallBack method for the CallCenter class.

I am really new to Test-Driven Development, how would I write the method for this to get the tests to pass?

public boolean willAcceptCallBack(currentTime, LocalDateTime())
{
            // Potential scenarios:
            // 1st write a scenario that will not accept out of hours calls
            // 2nd write a scenario that will not accept calls less than 2 hours in the future
            // 3rd write a scenario that will accept calls more than 6 days in the future
}

Thanks in advance


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I believe the real requirements are in the test method names. Like testWillNotAcceptOutOfHours means that willAcceptCallback() should return false if the time is out of hours. I hope you have been informed of your call center’s opening hours? Do they end at 20:00, for example? Asking just because I read from the test that 20:15 is outside hours.

Your method should return either false or true depending on the dates and times passed to it. Use the isBefore and/or isAfter methods of LocalDateTime. You will probably also need the toLocalDate and toLocalTime methods.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...