Use of assert assertions in "original" JUNIT4 and simple use of Mockito frame mock mock objects

Source: Internet
Author: User

When writing the test code, we always have the value we expect for the method we are testing, and the actual execution value returned after the method is called. You need to assert that the two values are equal, throw an exception, hash code, and so on ...

Here bloggers mainly introduce simple assertions and mock-up. If you have had a relative understanding of junit testing, skip this article.

Here is the node class I prepared:

1  Packagedemo;2 3 /**4  * @authorLCC5  *6  */7  Public classNode {8     Private intvalue;9 Ten      PublicNode (intvalue) { One          This. Value =value; A     } -  -      PublicString toString () { the         return"Its original value is:" +value; -     } -  -      Public intGetValue () { +         returnvalue; -     } +  A      Public voidSetValue (intvalue) { at          This. Value =value; -     } -  -}

And the bubbling sorting algorithm for the node class:

1  Packagedemo;2 3 /**4  * @authorLCC5  * 6  */7  Public classBubblesort {8 9      Publicnode[] Bubblesort (node[] a) {Ten  One          for(inti = 0; i < a.length; i++) { A              for(intj = 0; J < A.length; J + +) { -                 if(A[i].getvalue () >A[j].getvalue ()) { -Node temp =A[i]; theA[i] =A[j]; -A[J] =temp; -                 } -             } +         } -System.out.println (A[1].tostring ());//output when no mock is used: "It would have the value: 2 +         returnA; A     } at  -}

Now we need to test the bubble sorting method, of course, because this method is relatively simple actually does not have the mock also can, but the blogger does not think at a time to have what good example. If you have any questions, you are welcome to discuss with bloggers.

Now using a test method without a mock (in practice, there are few cases without a mock.) Here only as contrast)

 Packagedemo;ImportOrg.junit.Assert;Importorg.junit.Test;/** * @authorLCC **/ Public classbubblesorttest {bubblesort bubblesort=NewBubblesort (); /*** Test methods for Bubblesort*/@Test Public voidTestbubblesort () {Node Node1=NewNode (1); Node Node2=NewNode (2); Node Node3=NewNode (3); Node[] Nodes={NODE1,NODE2,NODE3};        Bubblesort.bubblesort (nodes); Assert.assertequals (3, Nodes[0].getvalue ()); Assert.assertequals (2, Nodes[1].getvalue ()); Assert.assertequals (1, nodes[2].getvalue ()); }}

Here is an explanation of the role of Assertequals:

Assertequals ([String message],object target,object result) target is not equal to result, interrupt test method, output message
Assertequals (A, B) tests whether a is equal to B (A and B are the original type values (primitive value) or must have a equal method for the implementation comparison)
Assertequals asserts that two objects are equal, and if not, the method throws a Assertionfailederror exception with the corresponding information.

For other specific assertions, refer to http://ryxxlong.iteye.com/blog/716428

This is not a repeat.

Let's use a mock to test this method:

1  Packagedemo;2 3 ImportOrg.junit.Assert;4 Importorg.junit.Test;5 Import StaticOrg.mockito.mockito.*;6 7 /**8  * @authorLCC9  * Ten  */ One  Public classBubblesorttest { A  -Bubblesort Bubblesort =NewBubblesort (); -  the     /** - * Test methods for Bubblesort -      */ - @Test +      Public voidTestbubblesort () { -          +Node Node1 =NewNode (1); A //node Node2 = new node (2); atNode MockNode2 = Mock (node.class); -Node Node3 =NewNode (3); -  -When (Mocknode2.getvalue ()). Thenreturn (2); -When (Mocknode2.tostring ()). Thenreturn ("Now the output is the value of the mock call when you're ready."); -          inNode[] Nodes ={node1,mocknode2,node3}; -          to Bubblesort.bubblesort (nodes); +Assert.assertequals (3, Nodes[0].getvalue ()); -Assert.assertsame (MockNode2, nodes[1]);//since we mock the node2.getvalue () so we cannot assert this method directly, we should assert its hash code theAssert.assertequals (1, nodes[2].getvalue ()); *     } $ Panax Notoginseng}

Now the SYSTEM.OUT.PRINTLN output in the JUnit test bubble sort Run is the value of our mock. A mock is simply a simulation, not a real execution, but rather a value that you have prepared before the mock object is called, so we only need to prepare the class for the method call when we test the method being tested.

Code and article written bad, thanks for browsing! I hope this article will be helpful to you.

Use of assert assertions in "original" JUNIT4 and simple use of Mockito frame mock mock objects

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.