单元测试WCMUsePOJO类 [英] Unit test WCMUsePOJO class

查看:144
本文介绍了单元测试WCMUsePOJO类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为扩展WCMUsePOJO的以下类编写单元测试用例.现在,此类使用如下所示的getSlingScriptHelper方法.

I am writing unit test cases for following class which extends WCMUsePOJO. Now, this class is using a getSlingScriptHelper method shown below.

public class ConstantsServiceProvider extends WCMUsePojo {
private static final Logger logger = LoggerFactory.getLogger(ConstantsServiceProvider.class);

private String var1;

@Override
public void activate() throws Exception {
    ConstantsService constantsService = getSlingScriptHelper().getService(ConstantsService.class);
    if(constantsService != null) {
       var1  = constantsService.getVar1();
    }
}

public string getVar1() { return var1; }

}

问题是如何模拟getSlingScriptHelper方法?以下是我的单元测试代码.

The question is how do I mock getSlingScriptHelper method? Following is my unit test code.

公共类ConstantsServiceProviderTest {

public class ConstantsServiceProviderTest {

@Rule
public final SlingContext context = new SlingContext(ResourceResolverType.JCR_MOCK);

@Mock
public SlingScriptHelper scriptHelper;

public ConstantsServiceProviderTest() throws Exception {

}

@Before
public void setUp() throws Exception {
    ConstantsService service = new ConstantsService();
    scriptHelper = context.slingScriptHelper();
    provider = new ConstantsServiceProvider();

    provider.activate();
}

@Test
public void testGetvar1() throws Exception {
    String testvar1 = "";
    String var1 = provider.getVar1();
    assertEquals(testvar1, var1);
}
} 

推荐答案

您唯一要"*"模拟的是SlingScriptHelper实例本身,这样它将模仿已声明服务的依赖项注入.

The only thing that you should "have to"* mock is the SlingScriptHelper instance itself, so that it will mimic the dependency injection of the declared service.

其他所有内容(例如Bindings实例)都可以是具体的实现,例如:

Everything else (e.g. the Bindings instance) can be a concrete implementation, for example:

import org.apache.sling.api.scripting.SlingBindings;
import org.apache.sling.api.scripting.SlingScriptHelper;
import org.junit.Test;

import javax.script.Bindings;
import javax.script.SimpleBindings;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ConstantsServiceProviderTest {

    private SlingScriptHelper mockSling = mock(SlingScriptHelper.class);
    private ConstantsServiceProvider constantsServiceProvider = new ConstantsServiceProvider();
    private Bindings bindings = new SimpleBindings();

    @Test
    public void testFoo() throws Exception {
        //Arrange
        final String expected = "Hello world";
        final ConstantsService testConstantsService = new TestConstantsService(expected);
        when(mockSling.getService(ConstantsService.class)).thenReturn(testConstantsService);
        bindings.put(SlingBindings.SLING, mockSling);

        //Act
        constantsServiceProvider.init(bindings);

        //Assert
        final String actual = constantsServiceProvider.getVar1();
        assertThat(actual, is(equalTo(expected)));
    }

    class TestConstantsService extends ConstantsService {
        String var1 = "";

        TestConstantsService(String var1) {
            this.var1 = var1;
        }

        @Override
        String getVar1() {
            return var1;
        }
    }

}

如上所述,此处的入口点是通过WCMUsePojo超类的init()方法进行的(由于该方法是Use.class接口的实现,因此该测试结构也可以通过界面,即使您不直接使用WCMUsePojo.)

The entry point here, as you said above, is via the init() method of the WCMUsePojo superclass (as this method is an implementation of the Use.class interface, this test structure also works for testing that via that interface, even if you don't use WCMUsePojo directly.)

*这可以是任何类型的test-double,不一定是模拟的.

*this could be any type of test-double, not necessarily a mock.

这篇关于单元测试WCMUsePOJO类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆