How to use method of org.jmock.integration.junit4.JMock class

Best Jmock-library code snippet using org.jmock.integration.junit4.JMock.

Run Jmock-library automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Copyright 2005-2010 The Kuali Foundation
3 * 
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * 
8 * http://www.opensource.org/licenses/ecl1.php
9 * 
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.kuali.kra.service;
17
18import static org.junit.Assert.assertEquals;
19
20import java.util.HashMap;
21import java.util.Map;
22
23import org.jmock.Expectations;
24import org.jmock.Mockery;
25import org.jmock.integration.junit4.JMock;
26import org.jmock.integration.junit4.JUnit4Mockery;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.kuali.kra.bo.Organization;
30import org.kuali.kra.service.impl.OrganizationServiceImpl;
31import org.kuali.rice.krad.service.BusinessObjectService;
32
33/**
34 * Test the methods in OrganizationServiceImpl.
35 */
36@RunWith(JMock.class)
37public class OrganizationServiceTest {
38
39    private Mockery context = new JUnit4Mockery();
40    private static final String ORGANIZATION_ID = "organizationId";
41    private static final String ORGANIZATION_VALID_ID_VALUE = "000001";
42    private static final String ORGANIZATION_INVALID_ID_VALUE = "100001";
43    
44    /**
45     * Verify that the correct organization is returned if it is found.
46     */
47    @Test
48    public void testGetOrganizationFound() {
49        mockOrganization(ORGANIZATION_VALID_ID_VALUE, true);
50    }
51    
52    /**
53     * Verify that null is returned if the organization is not found.
54     */
55    @Test
56    public void testGetOrganizationNotFound() {
57        mockOrganization(ORGANIZATION_INVALID_ID_VALUE, false);
58    }
59    
60    /**
61     * This method is to mock OrganizationServiceImpl
62     * Test both valid and invalid organization here
63     * @param organizationIdValue
64     * @param validOrganization
65     */
66    private void mockOrganization(String organizationIdValue, boolean validOrganization) {
67        OrganizationServiceImpl organizationServiceImpl = new OrganizationServiceImpl();
68        
69        final Map<String, Object> fieldValues = new HashMap<String, Object>();
70        fieldValues.put(ORGANIZATION_ID, organizationIdValue);
71        final Organization organization = getOrganization(validOrganization);
72        final BusinessObjectService businessObjectService = context.mock(BusinessObjectService.class);
73        context.checking(new Expectations() {{
74            one(businessObjectService).findByPrimaryKey(Organization.class, fieldValues); will(returnValue(organization));
75        }});
76        organizationServiceImpl.setBusinessObjectService(businessObjectService);
77        
78        assertEquals(organization, organizationServiceImpl.getOrganization(organizationIdValue));
79    }
80    
81    /**
82     * This method is to get Organization 
83     * New instance for valid organization and null for invalid organization
84     * @param validOrganization
85     * @return

86     */
87    private Organization getOrganization(boolean validOrganization) {
88        if(validOrganization) {
89            return new Organization();
90        }else {
91            return null;
92        }
93    }
94    
95}
96
Full Screen
copy
1package droolsbook.sampleApplication.service.impl;
2
3import static org.junit.Assert.*;
4
5import java.util.Arrays;
6import java.util.Collections;
7
8import org.jmock.Expectations;
9import org.jmock.Mockery;
10import org.jmock.api.Expectation;
11import org.jmock.integration.junit4.JMock;
12import org.jmock.integration.junit4.JUnit4Mockery;
13import org.junit.Before;
14import org.junit.Test;
15import org.junit.runner.RunWith;
16
17import droolsbook.bank.model.BankingFactory;
18import droolsbook.bank.model.Customer;
19import droolsbook.bank.model.DefaultBankingFactory;
20import droolsbook.bank.service.BankingValidationService;
21import droolsbook.bank.service.Message;
22import droolsbook.bank.service.ValidationReport;
23import droolsbook.bank.service.Message.Type;
24import droolsbook.bank.service.impl.BankingServiceImpl;
25import droolsbook.bank.service.impl.CEPServiceImpl;
26import droolsbook.sampleApplication.repository.CustomerRepository;
27
28@RunWith(JMock.class)
29public class BankingServiceImplTest {
30  Mockery context = new JUnit4Mockery();
31  BankingServiceImpl service;
32  CustomerRepository customerRepository;
33  BankingValidationService validationService;
34  BankingFactory bankingFactory;
35  
36  @Before
37  public void setUp() throws Exception {
38    customerRepository = context.mock(CustomerRepository.class);
39    validationService = context.mock(BankingValidationService.class);
40    
41    service = new BankingServiceImpl();
42    service.setCustomerRepository(customerRepository);
43    service.setValidationService(validationService);
44    service.setCepService(new CEPServiceImpl());
45    
46    bankingFactory = new DefaultBankingFactory();
47  }
48  
49  private void verifyValidate(final Customer customer) {
50    final ValidationReport report = context.mock(ValidationReport.class);
51    context.checking(new Expectations() {
52      {
53        one(validationService).validate(customer);
54        will(returnValue(report));
55        one(report).getMessagesByType(Message.Type.ERROR);
56        will(returnValue(Collections.emptySet()));
57      }
58    });
59  }
60
61  @Test
62  public void testAdd() {
63    final Customer customer = bankingFactory.createCustomer();    
64    verifyValidate(customer);    
65    context.checking(new Expectations() {
66      {
67        one(customerRepository).addCustomer(customer);
68      }
69    });
70    service.add(customer);
71
72    //todo test of cese with exception
73  }
74
75  @Test
76  public void testSave() {
77    final Customer customer = bankingFactory.createCustomer();
78    verifyValidate(customer);
79    context.checking(new Expectations() {
80      {
81        one(customerRepository).updateCustomer(customer);
82        will(returnValue(customer));
83      }
84    });
85    service.save(customer);
86  }
87
88}
89
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)