Using Mockito for writing ATG test case

前端 未结 1 578
心在旅途
心在旅途 2021-01-22 08:24

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 Usin

相关标签:
1条回答
  • 2021-01-22 09:20

    Using Mockito you don't setup Nucleus or other dependencies (unless you need it). You simply mock the objects that you need to use.

    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"));
    }
    
    }   
    

    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).

    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.

    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 only really comes into the picture when you need to mock static methods and classes and the documentation does enough to explain that.

    0 讨论(0)
提交回复
热议问题