0
我有一个基本的接口,其他类正在实现。在JUnit测试中使用Mock类与依赖注入
package info;
import org.springframework.stereotype.Service;
public interface Student
{
public String getStudentID();
}
`
package info;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class StudentImpl implements Student
{
@Override
public String getStudentID()
{
return "Unimplemented";
}
}
然后我有一个服务来注入类为
package info;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
public class InfoService {
@Autowired
Student student;
public String runProg()
{
return student.getStudentID();
}
}
我想知道的是,我怎么建立一个JUnit测试,这样一Student接口的Mock类使用stubbed方法,而不是StudentImpl中的方法。注射确实有效,但我想使用amock类来模拟结果,而不是为了测试。任何帮助将不胜感激。
有什么特别的你必须在JUnit的地方,为了这个工作?目的是使用'when(fakeStudent.getStudentId())。然后返回(“Some Value”);' – 2013-04-11 08:05:30
不,在你的测试中,你只需创建你的mock然后自己“连接”它。我给答案增加了一个简单的例子。 – NilsH 2013-04-11 08:06:33
仍似乎没有工作。我仍然从'StudentImpl'获取字符串。 – 2013-04-11 08:14:03