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
But need a help in setting up Nucleus and other dependencies (DAS, DPS, DSS etc.) and a sample test class for droplet using Mockito.
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 -
- A Base class for setting 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); } }
- Writing the test case by extending the base class -
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 */ }
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.
Thanks.
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. Theservice
method would look something like this:A simple test class for this will then be:
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 caseproductRepositoryItemMock
represents theRepositoryItem
andproductRepositoryItemMock.getRepositoryId()
returns aString
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 myservice
andhandleXXX
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 mockstatic
methods and classes and the documentation does enough to explain that.