Java 如何将我的代码更改为使用2个参数而不是4个参数进行Junit测试?
我需要对我的代码进行一些Junit测试。但是,我已经对它进行了完整的编码,因此它需要4个参数,而不是2个参数。谁能帮我修改代码,因为我不知道哪里出错了!基本上,我的代码在控制台中加和减两次 类新时间:Java 如何将我的代码更改为使用2个参数而不是4个参数进行Junit测试?,java,junit,console,Java,Junit,Console,我需要对我的代码进行一些Junit测试。但是,我已经对它进行了完整的编码,因此它需要4个参数,而不是2个参数。谁能帮我修改代码,因为我不知道哪里出错了!基本上,我的代码在控制台中加和减两次 类新时间: public class NewTime { public boolean valid24Time(int hh, int mm){ if(hh<0 || hh>23 || mm < 0 ||mm > 59) //if time is out of
public class NewTime {
public boolean valid24Time(int hh, int mm){
if(hh<0 || hh>23 || mm < 0 ||mm > 59) //if time is out of range
return false;
else
return true;
}
public int addTime(int hh1, int mm1, int hh2, int mm2){
int time1= hh1*60+mm1; //time1 in minutes
int time2=hh2*60+mm2; //time 2 in minutes
int time=time2+time1; //add times
int hh=(time/60)%23; //get hours
int mm= time%60; //get minutes
return hh*100+mm;
}
public int subtractTime(int hh1, int mm1, int hh2, int mm2){
int time1= hh1*60+mm1;//time1 in minutes
int time2=hh2*60+mm2; //time 2 in minutes
int time=time2-time1; //subtract times
int hh=(time/60)%23; //get hours
int mm= time%60; //get minutes
return hh*100+mm;
}
正如所指出的,这不是一个单元测试。你的方法需要四个参数?因此,请提供四个。在JUnit测试中,您不会与用户或System.out进行交互 你的课完全错了。此JUnit测试证明您遇到了一个问题:
import org.junit.Assert;
import org.junit.Test;
/**
* NewTimeTest description here
* @author Michael
* @link https://stackoverflow.com/questions/32453866/how-to-change-my-code-into-taking-2-parameters-instead-of-4-for-junit-testing
* @since 9/8/2015 5:34 AM
*/
public class NewTimeTest {
@Test
public void testValid24Time_Success() {
NewTime newTime = new NewTime();
for (int h = 0; h < 24; ++h) {
for (int m = 0; m < 60; ++m) {
Assert.assertTrue(String.format("Invalid time for %d:%d", h, m), newTime.valid24Time(h, m));
}
}
}
@Test
public void testAddTime() {
NewTime newTime = new NewTime();
int expected = 80;
Assert.assertEquals(expected, newTime.addTime(0, 0, 1, 20));
}
}
显然,您根本不知道如何为代码编写JUnit测试。我举个例子:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class Test {
@Test
public void test() {
NewTime time = new NewTime();
assertEquals(true, time.valid24Time(5, 30));
assertEquals(false, time.valid24Time(5, 61));
assertEquals(202, time.addTime(1, 1, 1, 1));
assertEquals(1220, time.addTime(5, 5, 7, 15));
assertEquals(0, time.subtractTime(1, 1, 1, 1));
assertEquals(620, time.subtractTime(6, 10, 12, 30));
}
}
顺便说一下,您的代码很难阅读和使用。例如,为什么最后一行的结果是620?这不是JUnit测试。您应该阅读JUnit的页面。我知道,但我想编辑它以进行JUnit测试。@Manu我不明白它是如何工作的,我只是无法掌握它。我编写了这段代码,我想将其更改为接受JUnit,但我完全不知所措。我会接受@Tunaki关于阅读入门页面的建议,并尝试将其放入JUnit测试类,然后回来用新的JUnit代码提问。不确定您是否需要提供尽可能多的代码,尤其是在问题不清楚的情况下。尝试JUnit测试,仔细思考问题并返回。我认为这是你最好的选择。仅仅因为你期望80,并不意味着他的代码是错误的。没有规定结果以分钟为单位。代码很奇怪:-你是说在午夜增加一小时20分钟应该会导致其他事情吗?真的很奇怪。你的单元测试也不是很好。我不喜欢把几种方法塞进一个测试中。请给一位顾客一张。更好的是,每个人都有成功和失败的例子来展示他们是如何工作的。我同意。我会在工作中用不同的方式写,但这只是初学者的一个例子:-很公平。我仍然认为,一个恰当的例子并不需要付出更多的努力。
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class Test {
@Test
public void test() {
NewTime time = new NewTime();
assertEquals(true, time.valid24Time(5, 30));
assertEquals(false, time.valid24Time(5, 61));
assertEquals(202, time.addTime(1, 1, 1, 1));
assertEquals(1220, time.addTime(5, 5, 7, 15));
assertEquals(0, time.subtractTime(1, 1, 1, 1));
assertEquals(620, time.subtractTime(6, 10, 12, 30));
}
}