EasyMock test tutorial and easymock Test

Source: Internet
Author: User

EasyMock test tutorial and easymock Test

This tutorial uses the EasyMock framework for testing based on JUnit introduction.

EasyMock Introduction

EasyMock instantiated objects based on interfaces or classes:

import static org.easymock.EasyMock.createNiceMock;....// ICalcMethod is the object which is mockedICalcMethod calcMethod = createNiceMock(ICalcMethod.class); 

createNiceMock()The mock created by the method returns the default value instead of the overload method.Mock () cannot be created like this.

EasyMock can be used to configure a mock object.expect()The method tells EasyMock to simulate the method with given parameters,andReturn()Corresponding return value.times()The number of call times defined by the method.replay()The simulated object is available after the method is called.

// setup the mock objectexpect(calcMethod.calc(Position.BOSS)).andReturn(70000.0).times(2);expect(calcMethod.calc(Position.PROGRAMMER)).andReturn(50000.0);// Setup is finished need to activate the mockreplay(calcMethod); 

 

The latest version of EasyMock (2015 by the end of 3.4) released /. The following java classes are defined:

package com.vogella.testing.easymock.first;public enum Position {    BOSS, PROGRAMMER, SURFER} 
package com.vogella.testing.easymock.first;public interface ICalcMethod {    double calc(Position position);} 
package com.vogella.testing.easymock.first;public class IncomeCalculator {    private ICalcMethod calcMethod;    private Position position;    public void setCalcMethod(ICalcMethod calcMethod) {        this.calcMethod = calcMethod;    }    public void setPosition(Position position) {        this.position = position;    }    public double calc() {        if (calcMethod == null) {            throw new RuntimeException("CalcMethod not yet maintained");        }        if (position == null) {            throw new RuntimeException("Position not yet maintained");        }        return calcMethod.calc(position);    }} 

The class to be tested is IncomeCalculator. This class is used to calculate the salary of a specified position and calculation method.

Test classIncomeCalculator:

package com.oppo.ads.searchBid.task.thread;import org.junit.Test;import static org.junit.Assert.*;// use static imports to// have direct access to these methodsimport static org.easymock.EasyMock.createNiceMock;import static org.easymock.EasyMock.expect;import static org.easymock.EasyMock.replay;import static org.easymock.EasyMock.verify;import static org.junit.Assert.assertEquals;import static org.junit.Assert.fail;import org.junit.Before;import org.junit.Test;public class IncomeCalculatorTest {    private ICalcMethod calcMethod;    private IncomeCalculator calc;    @Before    public void setUp() throws Exception {        // NiceMocks return default values for        // unimplemented methods        calcMethod = createNiceMock(ICalcMethod.class);        calc = new IncomeCalculator();    }    @Test    public void testCalc1() {        // Setting up the expected value of the method call calc        expect(calcMethod.calc(Position.BOSS)).andReturn(70000.0).times(2);        expect(calcMethod.calc(Position.PROGRAMMER)).andReturn(50000.0);        // Setup is finished need to activate the mock        replay(calcMethod);        calc.setCalcMethod(calcMethod);        calc.setPosition(Position.BOSS);        assertEquals(70000.0, calc.calc(), 0);        assertEquals(70000.0, calc.calc(), 0);        calc.setPosition(Position.PROGRAMMER);        assertEquals(50000.0, calc.calc(), 0);        calc.setPosition(Position.SURFER);        verify(calcMethod);    }    @Test(expected = RuntimeException.class)    public void testCalc2() {        // Setting up the expected value of the method call calc        expect(calcMethod.calc(Position.SURFER)).andThrow(new RuntimeException("Don't know this guy")).times(1);        // Setup is finished need to activate the mock        replay(calcMethod);        calc.setPosition(Position.SURFER);        calc.setCalcMethod(calcMethod);        calc.calc();    }}

It can be seen that EasyMock needs to be tested based on interfaces, and operations are still quite troublesome!

References

Http://www.vogella.com/tutorials/EasyMock/article.html

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.