问题描述
如何为Phone对象模拟对象.
How to mock the object for the Phone object.
下面的代码
public class Fortest {
UserDao userdao = new UserDao();
Phone name = new Phone();
public String handleUser(User user) {
String returncode="failed";
//User usr = new User("bob");
String username=user.getUsername();
String pass=user.getPass();
System.out.println("username and password : "+username+" : "+pass);
Phone name = new Phone();
String ph = name.getA();
System.out.println("ph "+ph);
if(ph.equalsIgnoreCase("test")){
System.out.println("A "+ph);
returncode="done";
}
System.out.println("returning "+returncode);
return returncode;
//System.out.println("name "+name.toString());
//System.out.println(name.getA());
}
}
谢谢
推荐答案
首先,我将做一些假设.user.getUsername()
& user.getPass()
没有副作用.System.out.println
对您并不重要.
First I'm going to make some assumptions.user.getUsername()
& user.getPass()
have no side affects.The System.out.println
are not important to you.
因此,您完成的课程变为:
Thus done your class becomes:
public class Fortest {
Phone name = new Phone();
public String handleUser(User user) {
String ph = name.getA();
if(ph.equalsIgnoreCase("test")){
return "done";
}
return "failed";
}
}
因此您的测试有两个条件. phone.getA()
是测试",则返回完成",否则不是,则返回失败".
So your test has two conditions. Either phone.getA()
is "test" and you return "done" or it is not and you return "failed".
那么如何设置集"getA
".可以肯定的是,我们需要能够从测试中设置名称".为此,我们需要注入"它(我们可以通过其他多种方式来做到这一点,但我喜欢注入).我会用Guice,许多人会用Spring.有些人会使用其他注入框架之一.但是在测试中,我们大多数人会使用手动注射.
So how to set set "getA
". One thing is for sure, we will need to be able set "name" from the test. For that we need to "inject" it (we can do it a number of other ways, but I loves injection). I'd use Guice, many would use Spring. Some would use one of the other injection frameworks. But in the tests most of us would use manual injection.
public class Fortest {
Phone name;
Fortest(Phone name) {
this.name = name;
}
public String handleUser(User user) {
String ph = name.getA();
if(ph.equalsIgnoreCase("test")){
return "done";
}
return "failed";
}
}
public class TestFortest {
@Before
public void before() {
name = ; //...
subject = new Fortest(name);
}
}
现在测试非常简单:
public void whenTestModeIsEnabledThenReturnDone() {
setPhoneIntoTestMode();
String actual = subject.handleUser(null);
assertEquals(actual, "done");
}
public void whenTestModeIsDisabledThenReturnFailed() {
setPhoneIntoLiveMode();
String actual = subject.handleUser(null);
assertEquals(actual, "failed");
}
setPhoneIntoTestMode
/setPhoneIntoLiveMode
的实现取决于Phone
的复杂程度.如果比它复杂,我们将以某种方式(模拟,存根等)看待它.这可能是您编写的大量代码,也可能使用了Mocketo之类的工具.
The implementation of setPhoneIntoTestMode
/setPhoneIntoLiveMode
will depend on how complex Phone
is. If it is complex than we would look at "facking" it in some way (mocks, stubs, etc). This could be a chunk of code you write, it could be using a tool like Mocketo.
如果Phone对象很简单,并且具有或可以具有"setA
"方法,则只需使用该方法即可.
If the Phone object is simple, and has or can have a "setA
" method, then just use that.
我确定以后您将需要userdao
.届时将做同样的事情.注入并模拟/设置对象.
I'm sure later you will need userdao
. The same thing will be done at that point. Inject and mock/setup the object.
这篇关于junit测试类的以下代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!