Java 使用一个类来测试另一个类?
对于下面给定的类,我被要求使用一个新的Java 使用一个类来测试另一个类?,java,Java,对于下面给定的类,我被要求使用一个新的TimeTest类来测试这个类,该类有一个方法doTest(),以便在我的Time类中试用这些方法。我以前通过main方法测试过类,但我不知道如何使用类测试类。有人能帮我开始吗 public class Time { private int minute; private int hour; private int totalMinute; public Time() { minute = 0;
TimeTest
类来测试这个类,该类有一个方法doTest()
,以便在我的Time
类中试用这些方法。我以前通过main
方法测试过类,但我不知道如何使用类测试类。有人能帮我开始吗
public class Time {
private int minute;
private int hour;
private int totalMinute;
public Time() {
minute = 0;
hour = 0;
}
public Time(int hours, int minutes) {
setHour(hours);
setMinute(minutes);
}
private void setHour(int hours) {
if (hours < 24 && hours >= 0) {
hour = hours;
} else {
hour = 0;
}
}
private void setMinute(int minutes) {
if (minutes < 60 && minutes >= 0) {
minute = minutes;
} else {
minute = 0;
}
}
public void setTime(int hours, int minutes) {
setHour(hours);
setMinute(minutes);
}
public int getElapsedTime(Time that) {
int thisTime = this.getTotalMinutes();
int thatTime = that.getTotalMinutes();
if (thisTime > thatTime) {
return thisTime - thatTime;
}
return thatTime - thisTime;
}
private int getTotalMinutes() {
totalMinute = (hour * 60) + minute;
return totalMinute;
}
public String getAsString() {
if (hour < 10 && minute < 10) {
return "0" + hour + ":0" + minute;
} else if (hour < 10 && minute >= 10) {
return "0" + hour + ":" + minute;
} else if (hour >= 10 && minute < 10) {
return hour + ":0" + minute;
} else {
return hour + ":" + minute;
}
}
}
公共课时间{
私人整数分钟;
私人整数小时;
私人整数分钟;
公共时间(){
分钟=0;
小时=0;
}
公共时间(整数小时,整数分钟){
设定小时(小时);
设置分钟(分钟);
}
专用无效设置小时(整数小时){
如果(小时数<24&&hours>=0){
小时=小时;
}否则{
小时=0;
}
}
专用无效设置分钟(整数分钟){
如果(分钟<60&&minutes>=0){
分钟=分钟;
}否则{
分钟=0;
}
}
公共无效设置时间(整数小时,整数分钟){
设定小时(小时);
设置分钟(分钟);
}
public int getElapsedTime(该时间){
int thistTime=this.getTotalMinutes();
int thatTime=that.getTotalMinutes();
如果(这次>那次){
这一次返回,那一次返回;
}
返回那个时间-这次;
}
private int getTotalMinutes(){
总分钟=(小时*60)+分钟;
返回总分钟数;
}
公共字符串getAsString(){
如果(小时<10分钟<10分钟){
返回“0”+小时+”:0”+分钟;
}否则,如果(小时<10分钟>=10){
返回“0”+小时+:“+分钟;
}否则(小时>=10分钟<10分钟){
返回时间+“:0”+分钟;
}否则{
返回小时+“:”+分钟;
}
}
}
在该类中编写另一个testclass,并使用主函数。从主函数测试类
你需要测试你的时间课吗?我说得对吗
public class test
{
public static void main(String args[])
{
Time t = new Time();
//now use any method of time class
//i.e. t.methodname(pram)
}
}
使用main函数在该类中编写另一个testclass。从主函数测试类 你需要测试你的时间课吗?我说得对吗
public class test
{
public static void main(String args[])
{
Time t = new Time();
//now use any method of time class
//i.e. t.methodname(pram)
}
}
是开始测试逻辑的最佳地点。是开始测试逻辑的最佳地点。简单的方法是在新测试类的main()中执行任何操作 因此,如果之前main创建了类的新对象,然后调用了该对象上的方法,那么测试类将创建原始类的新对象并调用其方法 比如:
public class TimeTest
{
public static void main(String [] args)
{
Time timeA = new Time();
Time timeB = new Time(12, 30);
System.out.println("Time Diff: " + timeB.getElapsedTime(timeA);
// etc
}
}
一旦你了解了这一点,就可以研究不同的测试框架,比如JUnit,它可以自动处理测试的管理方面,让你像编写代码一样进行测试。简单的方法是在新的测试类中执行main()中的任何操作 因此,如果之前main创建了类的新对象,然后调用了该对象上的方法,那么测试类将创建原始类的新对象并调用其方法 比如:
public class TimeTest
{
public static void main(String [] args)
{
Time timeA = new Time();
Time timeB = new Time(12, 30);
System.out.println("Time Diff: " + timeB.getElapsedTime(timeA);
// etc
}
}
一旦你了解了这一点,就可以研究不同的测试框架,比如JUnit,它可以自动处理测试的管理方面,让你像编写代码一样进行测试。看看。它可以为您提供一个易于使用的测试环境
测试代码时的主要概念是:测试应该只声明一件事。那是什么取决于你的代码
下面是对当前类进行测试的示例。我希望断言,在将时间类设置为1:13后,我将返回准确的字符串。此测试应该存在于自己的类中,独立于Time
对象
@Test
public void testGetAsString() {
Time testObj = new Time();
String expected = "01:13";
testObj.setTime(1, 13);
String result = testObj.getAsString();
Assert.assertEquals("the time isn't properly rendered", result, expected);
}
这样的单元测试可以极大地帮助您完成更大的项目、重构,并且是测试驱动开发的三个部分之一
如果您感兴趣,还可以研究其他模拟或模拟另一个对象行为的测试框架,并且可以在这方面帮助您。看看。它可以为您提供一个易于使用的测试环境
测试代码时的主要概念是:测试应该只声明一件事。那是什么取决于你的代码
下面是对当前类进行测试的示例。我希望断言,在将时间类设置为1:13后,我将返回准确的字符串。此测试应该存在于自己的类中,独立于Time
对象
@Test
public void testGetAsString() {
Time testObj = new Time();
String expected = "01:13";
testObj.setTime(1, 13);
String result = testObj.getAsString();
Assert.assertEquals("the time isn't properly rendered", result, expected);
}
这样的单元测试可以极大地帮助您完成更大的项目、重构,并且是测试驱动开发的三个部分之一
如果您感兴趣,还可以研究其他模拟或模拟另一个对象行为的测试框架,并且可以在这方面帮助您。您应该查看JUnit框架
测试类应如下所示:
public class TimeTest {
public TimeTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void doTest()
{
Time instance = new Time();
//your code to test the methods
//use assertEquals(expectedResult, result) to compare the result you expected with the result from your code
}
}
您应该查看JUnit框架
测试类应如下所示:
public class TimeTest {
public TimeTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
@Test
public void doTest()
{
Time instance = new Time();
//your code to test the methods
//use assertEquals(expectedResult, result) to compare the result you expected with the result from your code
}
}
这是可以满足你要求的课程。如果您不想在这个类中使用main方法,那么在另一个类中编写main方法,并创建TimeTest对象,并从拥有main方法的类中调用doTest(time)方法
public class TimeTest {
public static void main(String[] args) {
Time time = new Time();
time.setTime(10, 10);
doTest(time);
}
private static void doTest(Time time) {
//set the time to 10 hours and 10 minutes
System.out.println("Get as String: " + time.getAsString());
// set the new time to test the getElapsedTime(time) method
Time newTime = new Time(11, 30);
System.out.println("Get Elapsed Time: " + time.getElapsedTime(newTime) + " minutes");
}
}
谢谢这是可以满足您要求的课程。如果您不想在这个类中使用main方法,那么在另一个类中编写main方法,并创建TimeTest对象,并从拥有main方法的类中调用doTest(time)方法
public class TimeTest {
public static void main(String[] args) {
Time time = new Time();
time.setTime(10, 10);
doTest(time);
}
private static void doTest(Time time) {
//set the time to 10 hours and 10 minutes
System.out.println("Get as String: " + time.getAsString());
// set the new time to test the getElapsedTime(time) method
Time newTime = new Time(11, 30);
System.out.println("Get Elapsed Time: " + time.getElapsedTime(newTime) + " minutes");
}
}
谢谢
这就是如何在类时间测试的方法doTest中尝试其他类时间方法的方法
这就是你如何在类时间测试的方法doTest中尝试其他类时间方法的方法。你能详细说明一下吗?你打算如何将其扩展到实际的测试中,并在调用测试方法后断言对象状态的真实情况?这只是一个家庭作业。他正在做基本的工作