我必须测试 Java
我在课堂上被告知我必须在主方法中编写和测试我的代码,我编写了它,但我不知道如何测试它。我应该如何测试我的方法?我应该接受用户输入,然后获取第一个字母、最后一个字母等。
import java.util.Scanner;
public class Word
{
public static void main(String[] args)
{
}
public String word;
public void Word()
{
String word = "";
}
public void Word(String word1)
{
String word = word1;
}
public String getWord()
{
return word;
}
public void setWord(String newWord)
{
String word = newWord;
}
public void getFirstLetter()
{
String firstLetter = word.substring(0, 1);
}
public void getLastLetter()
{
String lastLetter = word.substring(word.length() - 1, word.length());
}
public void removeFirstLetter()
{
String noFirstLetter = word.substring(1, word.length());
}
public void removeLastLetter()
{
String noLastLetter = word.substring(0, word.length() - 1);
}
public int findLetter (String parameter)
{
word.indexOf(parameter);
return 1;
}
}
I was told in my class that I have to write and test my code in the main method, I wrote it, but I dont know how to test it. How I am supposed to test my methods? I am supposed to take user input, and then get the get the first letter, last letter, etc.
import java.util.Scanner;
public class Word
{
public static void main(String[] args)
{
}
public String word;
public void Word()
{
String word = "";
}
public void Word(String word1)
{
String word = word1;
}
public String getWord()
{
return word;
}
public void setWord(String newWord)
{
String word = newWord;
}
public void getFirstLetter()
{
String firstLetter = word.substring(0, 1);
}
public void getLastLetter()
{
String lastLetter = word.substring(word.length() - 1, word.length());
}
public void removeFirstLetter()
{
String noFirstLetter = word.substring(1, word.length());
}
public void removeLastLetter()
{
String noLastLetter = word.substring(0, word.length() - 1);
}
public int findLetter (String parameter)
{
word.indexOf(parameter);
return 1;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过使用一些定义的输入调用方法来测试方法,并将结果与预期输出进行比较。
示例:
假设您有一个像这样的方法:
您将像这样测试它:
所以基本上您定义了一个“契约”,其中包含该方法获取的输入以及结果应该是什么(就返回值或其他更改状态而言)。然后检查是否得到了输入的结果,如果是,则可以假设该方法工作正常。
为了非常确定(您通常不能完全确定),您需要使用不同类型的输入多次测试该方法(尽可能多地合理,以测试不同的情况,例如短单词、长单词)。
您还经常测试您的方法如何处理错误的输入,例如通过传递 null 或空字符串。
You test your methods by calling them with some defined input and compare the results with your expected output.
Example:
Suppose you have a method like this:
You'd test it like this:
So basically you define a "contract" of what input the method gets and what the result should be (in terms of return value or other changed state). Then you check whether you get that result for your input and if so you can assume the method works correctly.
In order to be quite sure (you often can't be perfectly sure) you'd test the method several times with different types of input (as many as reasonable, to test different cases, e.g. short words, long words).
You often also test how your method handles wrong input, e.g. by passing null or empty strings.
您应该看看 junit 等工具。
您可以创建一个简单的测试类并测试您的类及其行为。
使用像 Eclipse 这样的工具,您可以很好地运行这样的测试。
You should have a look at tools like junit.
You can create a simple Test class and test your class and its behavior.
With tools like Eclipse you could nicely run such a test.
只是一个提示:您非常接近需要一个 Word 实例,而不是您可以调用您的方法
编辑:
我忽略了这一点:
您编写的代码创建了一个变量
word
并将 newWord 分配给它,然后消失。如果您(显然)想要设置类的成员,您应该使用
this
来引用实例(您在 main() 中创建的)。Just a hint: you're very close you need an instance of Word, than you can call your methods
EDIT:
I overlooked this:
The code you've written creates a variable
word
and newWord is assigned to it and then disappears.If you (obviously) want to set a member of a class you should use
this
wich references the instance (you created in main()).因为我想说这是作业,所以我会尽量不明确给出答案。在 main 方法中,您应该设置您的单词,然后调用每个方法并打印输出以验证其是否正确。
Since I would say this is homework, I will try not to explicitly give the answer. In the main method, you should set your word, then call each method and print the output to verify it is correct.
同意杰森的观点。如果您想测试某些内容,只需
System.out.println()
即可。不过,在您的方法中,您的返回类型不是String
而是void
,因此您可以更改它,并在主程序run
上将其打印出来代码>.如果没有,只需将
System.out.println()
放入void
方法中。应该问题不大!Agree with Jason. If you wanna test something, simply
System.out.println()
it. In your methods though, your return type is not aString
but avoid
, so you could change that, and print it out on the main programrun
.If not, just put the
System.out.println()
in yourvoid
methods. Shouldn't have much of a problem!