简体   繁体   English

如何使用 EasyMock 测试返回另一个方法的方法?

[英]How to test method which returns another method using EasyMock?

I am writing test case using EasyMock.我正在使用 EasyMock 编写测试用例。 My test method calls "returns verification.getVerification(paramter 1, paramter 2, parameter 3)".我的测试方法调用“返回 verify.getVerification(paramter 1, paramter 2, parameter 3)”。 When I invoke my test method from test class, it returns null.当我从测试类调用我的测试方法时,它返回 null。

Sharing my code snippet below:在下面分享我的代码片段:

//EntityField.java

private Class <? extends Entity> classtype;
private String paths;
Permission filter;
@Inject
private transient RestrictInterface restriction;

public EntityField(final Class <? extends Entity> classtype, final String 
path, final Permission filterclass)
{
   this.classtype = classtype;
   this.paths = path;
   filter = filterclass;
}

public Permission getBasePermission() //my test method
{
   if(Active.class.isAssignableFrom(classtype))
   {
      filterclass=new 
    SimplePermission(Active.active_attribute,Operator.equals,Boolean.TRUE);
}
else if (NotActive.class.isAssignableFrom(classtype))
{
    filterclass=new 
    SimplePermission("notactive",Operator.equals,Boolean.TRUE);
}
return restriction.getBasePermission(classtype,filterclass);
}


//Test.java
@Test
public void testgetBaseRestriction() {
//NiceMock


EntityField entityfieldobject = new EntityField (classtype, path, 
filterclass);

//Mock Objects
RestrictInterface restriction = createNiceMock(RestrictInterface.class);
Permission filter = new 
SimplePermission(Active.active_attribute,Operator.equals,Boolean.TRUE);
final Class = Active.class;

//expects 
expect(restriction.getBaseRestriction(eq(classtype),eq(filterclass)))
.andStubReturn(filter);

//replay
replay(restriction);

Permission object = entityfieldobject.getBasePermission();
 // here object returns null    

verify(restriction);
}

I wanted to test whether filterclass value is set or not in my test class.我想测试我的测试类中是否设置了 filterclass 值。 How to avoid this null value in test case.如何在测试用例中避免这个空值。 Any help would be appreciated.任何帮助,将不胜感激。

Thanks谢谢

Updated answer, now that I have full code: restriction doesn't seem injected in entityfieldobject , So the null is normal.更新的答案,现在我有完整的代码: restriction似乎没有注入entityfieldobject ,所以 null 是正常的。 EasyMock is not aware of @Inject . EasyMock 不知道@Inject Please do inject the dependency yourself.请自己注入依赖项。 I recommend to use constructor injection for that.我建议为此使用构造函数注入。

The code below gives a good example.下面的代码给出了一个很好的例子。

import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.mock;
import static org.easymock.EasyMock.replay;
import static org.junit.Assert.assertNotNull;

public class TestClass {

    @Test
    public void testgetBaseRestriction() {
        Verification verification = mock(Verification.class);
        expect(verification.getVerification("1", "2", "3")).andReturn(new Permission());
        replay(verification);

        EntityField entityfieldobject = new EntityField(verification);
        assertNotNull(entityfieldobject.getVerification());
    }

}

class Permission {}

interface Verification {
    Permission getVerification(String s1, String s2, String s3);
}

class EntityField {

    private final Verification verification;

    public EntityField(Verification verification) {
        this.verification = verification;
    }

    public Permission getVerification() {
        return verification.getVerification("1", "2", "3");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM