java - Is there a neater way of testing calls to mocked methods for each item in a list -


This is an example of a pattern that I've encountered a lot lately. To test how I have a method that takes a list and can apply some other method (s) to each item in the list, I define a loop in the call parameter specified in Iterator and JMK expectations. The call is made against each item of the iterator (see trivial examples below).

Look at the Hamakrister Matters, but there is no such thing which test (or how wrongly available matters work) for it. Does anyone have a more elegant attitude?

  Package Com Hbsbc.maven.versionupdater; Import java.util.ArrayList; Import java.util.Iterator; Import java.util.list; Import org.apache.maven.plugin.testing.AbstractMojoTestCase; Import org.jmock.expectations; Import org.jmock.Mockery; Import org.jmock.Sequence; Import org.jmock.internal.NamedSequence; Public category FooTest AbstractMojoTestCase {public interface bar {void doIt (String AGR) extends; } Public square Foo {Private times; Public Zero Execute (Ultimate list & lt; string & gt; all parameters) {for (int i = 0; i & lt; allParameters.size (); i ++) {if (i% 2 == 0) { Bar.doIt (allParameters.get (i)); }}} Public bar getBar () {return bar; } Public Zero Set Bar (last time) {this.bar = bar; }} Public Zero Test ExecuteEven () {Fun Jokes = New Joke (); Last time = Mockery.Mock (bar class); Final sequence sequence = new name ("sequence"); Last list & lt; String & gt; AllParameters = new ArrayList & lt; String & gt; (); Last list & lt; String & gt; Expected Parameters = new Array's & lt; String & gt; (); For (int i = 0; i  

I think your current test implementation is close to the ideal. Any other compaction risk is either changing the terms of the test or obscuring the reader (or both) the intent of the exam.

However, if you are a For a method call a certain number, you can use exactly (n) .of () :

  mockery.checking (new requirements) {{ OK (expectedParameters.length ()) .of (bar) .doIt (any (expected parameter)));}});  

(I've abandoned the equality check, but you get this idea). This is similar to the jmockit example in a different answer. Keep in mind that this does not test the same thing as your original exam. It does not check in particular:

  1. doIt
  2. The order of the call is that every element of the parameter list For example, this test will pass if your method has moved to the list in the reverse order, or if it is just going to the doIt method < Code> n bar but the first element of the list passed each time if you want to make sure that each element in the list If you have passed, you have to do a lot to establish a personal candidate for each person. If you do not care about the order of invocation, you can skip the use of the sequence (in that case you want to change your original method of accepting the collection instead of the list).


Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -