我正在尝试对该方法进行Junit测试。
public List<QuestionBank> getQuestionBankDetails(){
if(stubbed){
return (List<QuestionBank>) BeanPopulateUtils.getPopulateBeanValues(new QuestionBank(), true);
}else{
throw new NotImplementedException("Non-stubbed SOAP call not implemented");
}
}
这是我的Junit案例:
@Test
public void testGetQuestionBankDetails(){
List<QuestionBank> questionBankList = questionBankFacade.getQuestionBankDetails();
int i=1;
for(QuestionBank questionBank : questionBankList){
assertEquals("Name "+i+"", questionBank.getName());
assertEquals("Description "+i+"", questionBank.getDescription());
assertEquals("Pubished Status "+i+" ", questionBank.getPubishedStatus());
assertEquals("Questions "+i+"", questionBank.getQuestions());
i++;
}
}
Junit测试案例正在取得成功,但我想问的是我将如何在'else'案例中进行测试
Throw new NotImplementedException("Non-stubbed SOAP call not implemented");
答案 0 :(得分:2)
尝试使用可能的接口来区分存根和默认实现。并且最好使用配置逻辑来“注入”正确的行为,或者使用Factory来生成所请求的类实例。
见例如:
BankDetails.class
package example;
import java.util.List;
public interface BankDetails {
List<QuestionBank> getQuestionBankDetails();
}
BankDetailsDefaultImpl.class
package example;
import java.util.List;
public class BankDetailsDefaultImpl implements BankDetails {
@Override
public List<QuestionBank> getQuestionBankDetails() {
return (List<QuestionBank>) BeanPopulateUtils.getPopulateBeanValues(new QuestionBank(), true);
}
}
BankDetailsStubbedImpl.class
package example;
import java.util.List;
public class BankDetailsStubbedImpl implements BankDetails {
@Override
public List<QuestionBank> getQuestionBankDetails() {
throw new NotImplementedException("Non-stubbed SOAP call not implemented");
}
}
QuestionBankFacade.class
package example;
public class QuestionBankFacade {
private final BankDetails bankDetails;
public QuestionBankFacade(final BankDetails bankDetails) {
this.bankDetails = bankDetails;
}
public BankDetails getQuestionBankDetails() {
return this.getQuestionBankDetails();
}
}
最后是JUnit测试:
QuestionBankFacadeTest.class
package example;
import static org.junit.Assert.*;
import java.util.List;
import org.apache.commons.lang.NotImplementedException;
import org.junit.Test;
public class QuestionBankFacadeTest {
@Test
public void testDefaultImplementation() {
final QuestionBankFacade questionBankFacade = new QuestionBankFacade(new BankDetailsDefaultImpl());
List<QuestionBank> questionBankList = questionBankFacade.getQuestionBankDetails();
//Test case that you already have
// for(QuestionBank questionBank : questionBankList){
// assertEquals("Name "+i+"", questionBank.getName());
// assertEquals("Description "+i+"", questionBank.getDescription());
// assertEquals("Pubished Status "+i+" ", questionBank.getPubishedStatus());
// assertEquals("Questions "+i+"", questionBank.getQuestions());
// i++;
// }
}
@Test(expected = NotImplementedException.class)
public void testStubbedImplementation() {
final QuestionBankFacade questionBankFacade = new QuestionBankFacade(new BankDetailsStubbedImpl());
questionBankFacade.getQuestionBankDetails();
}
}
答案 1 :(得分:0)
如果stubbed
是类变量,则创建一个对象并将该变量设置为false。然后调用函数getQuestionBankDetails();
。这样你就可以检查其他部分
答案 2 :(得分:0)
由于缺少代码,我假设您的QuestionBankFacade
类看起来像这样:
private boolean stubbed;
public boolean isStubbed() {
return stubbed;
}
public void setIsStubbed(boolean isStubbed) {
this.stubbed = isStubbed;
}
public List<QuestionBank> getQuestionBankDetails() {
if (stubbed) {
return (List<QuestionBank>) BeanPopulateUtils.getPopulateBeanValues(new QuestionBank(), true);
}
throw new NotImplementedException("Non-stubbed SOAP call not implemented");
}
你可以写一个这样的测试:
@Test(expected=NotImplementedException.class)
public void getQuestionBankDetailsThrowsAnExceptionIfStubbed() {
boolean memento = questionBankFacade.isStubbed();
questionBankFacade.setIsStubbed(false);
try {
questionBankFacade.getQuestionBankDetails();
} finally {
questionBankFacade.setIsStubbed(memento);
}
}
答案 3 :(得分:0)
您需要编写一个单独的测试方法来测试else条件代码路径。