Redian新闻
>
求教一个Java TTD 测试驱动的 面试题
avatar
求教一个Java TTD 测试驱动的 面试题# JobHunting - 待字闺中
s*e
1
有一个面试题,是让用测试驱动TDD的方式开发和测试一个小程序。程序是:
写一个程序,打印从1到100的数字,如果这个数字是3的倍数,那么就打印AAA,而不是
打印出这个数字,如果这个数字是5的倍数,就打印出BBB,而不是打印出这个数字,如
果这个数字是即是3的倍数,也是5的倍数,就打印出AAABBB.
单纯写这个程序很简单,但是要用TDD的方式写出测试驱动就比较费解。主要是不知道
TDD的测试代码该如何写才能实现单元测试。 请指教,希望能给出程序和解释。
此外,请问如果一个方法没有返回值,是void的,该如何用做单元测试呢?
avatar
s*e
2
求高人指点!谢谢!

【在 s********e 的大作中提到】
: 有一个面试题,是让用测试驱动TDD的方式开发和测试一个小程序。程序是:
: 写一个程序,打印从1到100的数字,如果这个数字是3的倍数,那么就打印AAA,而不是
: 打印出这个数字,如果这个数字是5的倍数,就打印出BBB,而不是打印出这个数字,如
: 果这个数字是即是3的倍数,也是5的倍数,就打印出AAABBB.
: 单纯写这个程序很简单,但是要用TDD的方式写出测试驱动就比较费解。主要是不知道
: TDD的测试代码该如何写才能实现单元测试。 请指教,希望能给出程序和解释。
: 此外,请问如果一个方法没有返回值,是void的,该如何用做单元测试呢?

avatar
z*r
3
use junit test?
@test
public void testMethod() {
assertTrue(yourMethod() == null);
}
if your method returns null then it should pass this test.
TDD means you write test first and then write your code until it passes your
test
avatar
s*e
4
我觉得这样做的话,不能测试出正确啊? 我的困惑就是如果这个方式是void的,没有
返回值,如何测试方法里面的运行结果是正确的。比如我说的这个打印的问题,没有返
回结果,如何用assert去比较?该如何测试方法是正确的?
谢谢!

your

【在 z********r 的大作中提到】
: use junit test?
: @test
: public void testMethod() {
: assertTrue(yourMethod() == null);
: }
: if your method returns null then it should pass this test.
: TDD means you write test first and then write your code until it passes your
: test

avatar
z*r
5
I see what u are asking. You should have separate methods for doing output
and computation. Use Junit to test your computation method. It's generally
not easy and not necessary to test the output method.
You should have a method String convertIntToStr(int a)
and test that
asssertTrue(convertIntToStr(3).equals("AAA"));
assertTrue(convertIntToStr(5).equals("BBB"));
.....

【在 s********e 的大作中提到】
: 我觉得这样做的话,不能测试出正确啊? 我的困惑就是如果这个方式是void的,没有
: 返回值,如何测试方法里面的运行结果是正确的。比如我说的这个打印的问题,没有返
: 回结果,如何用assert去比较?该如何测试方法是正确的?
: 谢谢!
:
: your

avatar
s*e
6
谢谢你的回复,我把我写的代码发到你的站内信里了,你能看一下我的代码,该如何测
试?谢谢指教! :)

【在 z********r 的大作中提到】
: I see what u are asking. You should have separate methods for doing output
: and computation. Use Junit to test your computation method. It's generally
: not easy and not necessary to test the output method.
: You should have a method String convertIntToStr(int a)
: and test that
: asssertTrue(convertIntToStr(3).equals("AAA"));
: assertTrue(convertIntToStr(5).equals("BBB"));
: .....

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。