Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to create unit tests for custom fields?

Ivo Timmermans August 15, 2011

Which mocking/test classes are available that can help me test a class/method like an implementation of AbstractMultiSettableCFType.getValueFromIssue(CustomField, Issue)? I tried various approaches, using PowerMockito and such, but every time I run into some database backend instantiation that I'm not sure how to configure correctly.

// public class OtrsLinkCFType extends AbstractMultiSettableCFType
OtrsLinkCFType q = new OtrsLinkCFType(cfvp, gcm, om);
Collection<OtrsPageReference> refs = (Collection<OtrsPageReference>) q.getValueFromIssue(null, issue);
 

 

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
MattS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 15, 2011

Simple Mockito works for me:

/**
 * Unit tests for the CurrencyCFType custom field type.
 */
public class CurrencyCFTypeTest {
 
    @Mock private CustomFieldValuePersister customFieldValuePersister;
    @Mock private JiraAuthenticationContext jiraAuthenticationContext;
    @Mock private GenericConfigManager genericConfigManager;
 
    @Mock private Issue issue;
    @Mock private CustomField customField;
    @Mock private I18nHelper i18nHelper;
    @Mock private FieldLayoutItem fieldLayoutItem;
 
    private DoubleConverter doubleConverter;
 
    @Before 
    public void setUp() { 
        // Create all the mock objects marked with @Mock
        MockitoAnnotations.initMocks(this);
        doubleConverter = new DoubleConverterImpl(jiraAuthenticationContext);
 
        // This is needed by DoubleConverter getDisplayFormat
        when(jiraAuthenticationContext.getI18nHelper()).thenReturn(i18nHelper);
        when(i18nHelper.getLocale()).thenReturn(Locale.getDefault());
    }
 
    @Test
    public void testGetSingularObjectFromString() {
        CurrencyCFType currencyCFType = new CurrencyCFType(customFieldValuePersister,
                                                           doubleConverter,
                                                           genericConfigManager);
        assertTrue(currencyCFType.getSingularObjectFromString("10.0").equals(new Double(10.0)));
 

But more complex methods get stuck pretty quickly:

@Test
    public void testVelocityParameters() {
        CurrencyCFType currencyCFType = new CurrencyCFType(customFieldValuePersister,
                                                           doubleConverter,
                                                           genericConfigManager);
        // This requires a servlet context for ComponentManager and
        // getI18nBean to work. Plain Mockito doesn't handle that well
        // but Power Mockito does. For more details see
        // https://www.adaptavist.com/display/~jmcgivern/2010/05/09/Unit+Testing+JIRA+with+PowerMock+for+Mockito</pre<>>
        //Map<String, Object> velocityParams = currencyCFType.getVelocityParameters(issue, customField, fieldLayoutItem);
        //assertNotNull(velocityParams.get("currencySymbol"));
    }
 
0 votes
MattS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 15, 2011

Gosh, code formatting is terrible on this new platform!

0 votes
MattS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 15, 2011

Wait. the title says you want unit tests but the first line says you want to debug the class. Debug with log messages or a debugger.

Ivo Timmermans August 15, 2011

Test, I mean test. I guess if you're testing your classes you're automatically debugging them as well, though :P Edited.

MattS
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 15, 2011

We could bounce that one around for a while :-)

TAGS
AUG Leaders

Atlassian Community Events