Easymock+junit+spring Learning ·

Source: Internet
Author: User

Easymock Learning

Author:luojie

    1. 1. Easymock Introduction

EasyMock is a class library that generates mock objects in a simple way for a specified interface or class, which can be used to assist unit testing with impersonation of a docking port or class.

For white-box testing, the code is not parsed until it differs from the expected result.

    1. 2. Easymock + junit Unit Test

EasyMock Adopt " Record ----- Playback " working mode, basic use steps:
* Control object for creating mock objects.
* Gets the desired mock object from the control object.
* Record the methods and return values used in the test method.
* Set Control object to "playback" mode.
* To test.
* After the test is complete, confirm that the mock object has performed all the actions just defined.

Public class Talkercontroller {

Public Integer Getresponsebyword (Stringword) {

/*if (word== "I'm angry! "){

return emotionconstants.very_angry_emotion;

}

else if (Word = = "I'm a little angry") {

Returnemotionconstants.normal_angry_emotion;

}

else if (Word = = "My mind is calm") {

Returnemotionconstants.normal_emotion;

}

else if (Word = = "I'm a little happy") {

Returnemotionconstants.normal_happy_emotion;

}

else if (Word = = "I'm Happy") {

return emotionconstants.very_happy_emotion;

}else{

return 0;

}*/

return 0;

}

Easymock To test the class initialization code:

@Before

Public voidinit () {

Calmotion = new calculatemotion ();

Talkermock = EasyMock. Createmock (Talkercontroller. class);

Calmotion.setcontroller (Talkermock);}

Easymock test class implementation functions Getresponsebyword Code:

@Test

Public void caltest () throwsexception{

EasyMock. expect (Talkermock.getresponsebyword ("I'm angry!")

")). Andreturn (5);

EasyMock. expect (Talkermock.getresponsebyword ("I'm happy

")). Andreturn (1);

EasyMock. Replay (Talkermock);

Humana = newHuman ("I'm angry!" ", 1);

HUMANB = newHuman ("I'm Happy", 1);

Calmotion.gethumanlist (). Add (a);

Calmotion.gethumanlist (). Add (b);

int total = Calmotion.calculateavgmotion ();

System. out. println ("Total motion:" +total);

Assert. assertequals (6,total);

}

    1. 3. easymock +spring + junit4

Easymock test the class-critical code:

JUNIT4 test Suites for:

@RunWith (Suite.class)

Specify the Runner

@Suite. suiteclasses ({calculatortest.class, parameterstest.class})

Specify the class to test

public class Testall

{

}

@RunWith (Suite.class)
@SuiteClasses (Testall.class)
In addition to specifying a class, you can also specify a suite class
public class TestAll2
{
}

    1. 4. Junit main syntax

JUNIT4 Use Java5 the annotations in the ( annotation ), the following is JUNIT4 a few common annotation :

* @Before: Initialization method is performed once for each test method (note the difference from Beforeclass, which is performed once for all methods)

* @After: Releasing resources is performed once for each test method (note the difference from Afterclass, which is

Have method to execute once)

* @Test: Test method, where you can test the expected exception and timeout time

* @Test (Expected=arithmeticexception.class) Check if the method being tested throws ArithmeticException exception

* @Ignore: Ignored test methods

* @BeforeClass: For all tests, execute only once and must be static void

* @AfterClass: For all tests, execute only once and must be static void

* A JUNIT4 unit test case Execution sequence is:

* @AfterClass, @After, @Test, @Before, @BeforeClass

* The sequence of calls for each test method is:

* @After, @Test, @Before;

    1. 5. Reference website:

http://jackyrong.iteye.com/blog/1934670 (Easymock Quick Start tutorial)

http://blog.csdn.net/vking_wang/article/details/9170979 (easymock basic syntax)

http://blog.csdn.net/u010218617/article/details/10468441 (easymock+spring)

http://blog.csdn.net/zhangxin09/article/details/42487319 (Junit+mockito)

Easymock+junit+spring Learning ·

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.