java - How should I use EasyMock's @Mock annotation (new in version 3.2)? -


it looks easymock version 3.2 supports using annotations setup mock objects. new easymock (and java in general) , trying understand how use this. these annotations new or provide alternative way things? documentation says:

since easymock 3.2, possible create mocks using annotations. nice , shorter way create mocks , inject them tested class. here example above, using annotations: ...

then there listing shows use of @testsubject , @mock annotations, don't understand how works. seems if magically sets private field of class under test mock object. in of cases, want make mock objects return pre-defined values use in junit test cases (don't care verifying ones called, how many times called, etc). example, tests want create fake httpservletrequest object this:

public class sometest {     // construct mock object typical http request url below      private static final string request_url = "http://www.example.com/path/to/file?query=1&b=2#some-fragment";     private static final map<string, string> requestheadermap;     static {         map<string, string> requestheaders = new linkedhashmap<string, string>();         requestheaders.put("host", "www.example.com");         // ... (add other desired headers here) ...         requestheadermap = collections.unmodifiablemap(requestheaders);     }      private httpservletrequest httpservletrequest;     // ...      @before     public void setup() throws exception {                     httpservletrequest = createnicemock(httpservletrequest.class);         expect(httpservletrequest.getrequesturi()).andreturn(request_url).anytimes();         expect(httpservletrequest.getheadernames()).andreturn(collections.enumeration(requestheadermap.keyset())).anytimes();         capturedstring = new capture<string>();         expect(httpservletrequest.getheader(capture(capturedstring))).andanswer(new ianswer<string>() {             public string answer() throws throwable {                 string headername = capturedstring.getvalue().tolowercase();                 if (requestheadermap.containskey(headername))                     return requestheadermap.get(headername);                 else                      return "";             }         }).anytimes();          replay(httpservletrequest);          // ...     }      @test     public void somemethod_givenanhttpservletrequest_shoulddosomething() {         // ...     } } 

could change above code use annotations? if so, should i? under circumstances?

i thought perhaps putting @mock annotation above instance variable declaration automatically take care of createnicemock(...) part, not seem work, suspect misunderstanding something.

examining source code, using reflection inject @mock field of @testsubject. javadoc method

public static void injectmocks(final object obj) 

in easymocksupport.java says:

inject mock every fields annotated {@link mock} on class passed in parameter. then, inject these mocks fields of every class annotated testsubject.

the rules are

  • static , final fields ignored
  • if mock can assigned field, it. same mock assigned more once
  • if no mock can assigned field, skip silently
  • if 2 mocks can assigned same field, return error
  • fields searched recursively on superclasses

note: if parameter extends easymocksupport, mocks created using allow replayall/verifyall work afterwards

@param obj object on inject mocks

@since 3.2

public static void injectmocks(final object obj) { ... }

for use @mock annotation, need @testsubject has httpservletrequest field easymock set @mock on (via reflection). annotations provided make little easier wire test, let's skip createmock, , calling settter yourself.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -