使用Mockito编写ATG测试用例 [英] Using Mockito for writing ATG test case
问题描述
有人对使用Mockito编写ATG的单元测试用例有想法吗?我在浏览时遇到了以下讨论- 用于ATG开发的自动化单元测试和 使用PowerMock获得ATG核NPE中的测试结果
Does anyone have idea about writing unit test case for ATG using Mockito? I came across following discussions while goggling - Automated unit tests for ATG development and Using PowerMock to obtain the ATG Nucleus in testing results in NPE
但是需要帮助来设置Nucleus和其他依赖项(DAS,DPS,DSS等),并使用Mockito进行液滴的样本测试类.
But need a help in setting up Nucleus and other dependencies (DAS, DPS, DSS etc.) and a sample test class for droplet using Mockito.
我们在使用ATG Dust时必须设置所有依赖项.我想知道我们是否可以用Mockito完全替代ATG Dust.这是我们如何编写测试用例的示例-
We are using ATG Dust where we have to set all the dependencies. I am wondering if we can replace ATG Dust completely with Mockito. Here is the example how we are writing the test cases -
- 用于设置Nucleus的基类-
package com.ebiz.market.support;
import java.io.File;
import java.util.Arrays;
import atg.nucleus.NucleusTestUtils;
import atg.test.AtgDustCase;
import atg.test.util.FileUtil;
public class BaseTestCase extends AtgDustCase {
public atg.nucleus.Nucleus mNucleus = null;
private final String ATGHOME="C://ATG/ATG9.4//home";
private final String ATGHOMEPROPERTY = "atg.dynamo.home";
protected void setUp() throws Exception {
super.setUp();
String dynamoHome = System.getProperty(ATGHOMEPROPERTY);
if(dynamoHome == null)
System.setProperty(ATGHOMEPROPERTY, ATGHOME);
File configpath = NucleusTestUtils.getConfigpath(this.getClass(), this.getClass().getName(), true);
FileUtil.copyDirectory("src/test/resources/config/test/", configpath.getAbsolutePath(), Arrays.asList(new String [] {".svn"}));
copyConfigurationFiles(new String[]{"config"}, configpath.getAbsolutePath(), ".svn");
}
public File getConfigPath() {
return NucleusTestUtils.getConfigpath(this.getClass(), this.getClass().getName(), true);
}
}
- 通过扩展基类来编写测试用例-
public class BizDropletTest extends BaseTestCase {
private BizDroplet bizDroplet;
@Before
public void setUp() throws Exception {
super.setUp();
mNucleus = NucleusTestUtils.startNucleusWithModules(new String[] { "DSS", "DPS", "DAFEAR" }, this.getClass(),
this.getClass().getName(), "com/ebiz/market/support/droplet/BizDroplet");
autoSuggestDroplet = (AutoSuggestDroplet) mNucleus.resolveName("com/ebiz/market/support/droplet/BizDroplet");
try {
bizDroplet.doStartService();
} catch (ServiceException e) {
fail(e.getMessage());
}
}
/**
Other methods
*/
}
那么,Mockito如何处理这些问题?再次,对我而言,目标是完全用Mockito代替ATG Dust,因为由于巨大的依赖关系,ATG Dust需要花费大量时间进行测试.
So, how Mockito can handle these? Again, for me the target is to replace ATG Dust with Mockito completely because ATG Dust take lot of time in running tests due to huge dependencies.
谢谢.
推荐答案
使用Mockito,您无需设置Nucleus或其他依赖项(除非您需要它).您只需模拟需要使用的对象即可.
Using Mockito you don't setup Nucleus or other dependencies (unless you need it). You simply mock the objects that you need to use.
考虑一个简单的类ProductUrlDroplet
,该类从存储库中检索产品,然后基于此输出URL. service
方法如下所示:
Consider a simple class ProductUrlDroplet
that retrieves a product from the repository and then outputs a url based on this. The service
method would look something like this:
public void service(DynamoHttpServletRequest pRequest, DynamoHttpServletResponse pResponse) throws ServletException, IOException {
Object product = pRequest.getObjectParameter(PRODUCT_ID);
RepositoryItem productItem = (RepositoryItem) product;
String generatedUrl = generateProductUrl(pRequest, productItem.getRepositoryId());
pRequest.setParameter(PRODUCT_URL_ID, generatedUrl);
pRequest.serviceParameter(OPARAM_OUTPUT, pRequest, pResponse);
}
private String generateProductUrl(DynamoHttpServletRequest request, String productId) {
HttpServletRequest originatingRequest = (HttpServletRequest) request.resolveName("/OriginatingRequest");
String contextroot = originatingRequest.getContextPath();
return contextroot + "/browse/product.jsp?productId=" + productId;
}
一个简单的测试类将是:
A simple test class for this will then be:
public class ProductUrlDropletTest {
@InjectMocks private ProductUrlDroplet testObj;
@Mock private DynamoHttpServletRequest requestMock;
@Mock private DynamoHttpServletResponse responseMock;
@Mock private RepositoryItem productRepositoryItemMock;
@BeforeMethod(groups = { "unit" })
public void setup() throws Exception {
testObj = new ProductUrlDroplet();
MockitoAnnotations.initMocks(this);
Mockito.when(productRepositoryItemMock.getRepositoryId()).thenReturn("50302372");
}
@Test(groups = { "unit" })
public void testProductURL() throws Exception {
Mockito.when(requestMock.getObjectParameter(ProductUrlDroplet.PRODUCT_ID)).thenReturn(productRepositoryItemMock);
testObj.service(requestMock, responseMock);
ArgumentCaptor<String> argumentProductURL = ArgumentCaptor.forClass(String.class);
Mockito.verify(requestMock).setParameter(Matchers.eq(ProductUrlDroplet.PRODUCT_URL_ID), argumentProductURL.capture());
Assert.assertTrue(argumentProductURL.getValue().equals("/browse/product.jsp?productId=50302372"));
}
}
关键组成部分是您需要初始化要测试的类(testObj
).然后,您只需为要使用的对象的每个输入参数构造响应(在这种情况下,productRepositoryItemMock
代表RepositoryItem
,而productRepositoryItemMock.getRepositoryId()
返回一个String
,您可以稍后对其进行测试).
The key components are that you need to initialise the class you want to test (testObj
). You then simply construct the response for each of the input parameters of the objects you are going to use (in this case productRepositoryItemMock
represents the RepositoryItem
and productRepositoryItemMock.getRepositoryId()
returns a String
that you can then test against later).
您还将注意到,此测试仅验证service
方法,而不验证单个方法.您的操作方式取决于您,但是通常我一直专注于在Formhandlers和Droplet中测试我的service
和handleXXX
方法.
You will also notice that this test only validates the service
method and not the individual methods. How you do it is up to you but generally I've been focused on testing my service
and handleXXX
methods in the formhandlers and droplets.
测试XXXManager,XXXUtil和XXXService类将具有各自的测试,应模拟"到液滴和形状处理程序中.对于这些,我会为每种方法编写测试.
Testing the XXXManager, XXXUtil and XXXService classes will all have their own tests and should be 'mocked' into the droplets and formhandlers. For these I would write tests for each method though.
PowerMockito
仅在需要模拟static
方法和类且文档足以说明这一点时才真正出现.
PowerMockito
only really comes into the picture when you need to mock static
methods and classes and the documentation does enough to explain that.
这篇关于使用Mockito编写ATG测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!