Home

Springockito

This is a small extension to spring that simplifies way of creation mockito mocks in the intergation tests' related context xml files.

Mocking

Having a definition in context definition file, such as:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
...
   <bean id="accountService" class="org.kubek2k.account.DefaultAccountService" />
   <bean id="bank" class="org.kubek2k.bank.Bank">
      <property name="accountService" ref="accountService" />
   </bean>
...
</beans>

you can simply override it by mock with the definition in file that is loaded only within tests, ex:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mockito="http://www.mockito.org/spring/mockito"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.mockito.org/spring/mockito http://www.mockito.org/spring/mockito.xsd">
...
    <mockito:mock id="accountService" class="org.kubek2k.account.DefaultAccountService" />
..
</beans>

Spying

Sometimes You are in need of plugin into the established application context to check some action happened on some beans under certain conditions, but you don't want bother recording the actual behavior of beans. Here the spying comes in handy:

Having definition:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
...
    <bean id="accountService" class="DefaultAccountService" />
...
</beans>

we can spy the service within integration test using definition:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.mockito.org/spring/mockito http://www.mockito.org/spring/mockito.xsd">
...
     <mockito:spy beanName="accountService" />
...
</beans>

and in test we can do something like:

@Autowired
private AccountService accountService;

@Test
public void shouldCountInterestForDeposit() {
    // given
    Deposit deposit = new Deposit(12);
    bank.addDeposit(deposit);

    // when
    bank.endOfTheMonth();
    
    // then
    verify(accountService).countInterestFor(deposit);
}

hope you like it :)

Maven2 usage

Springockito is available from Maven Central Repository and to use it add following dependency to your pom.xml:

<dependencies>
     ...
     <dependency>
       <groupId>org.kubek2k</groupId>
       <artifactId>springockito</artifactId>
       <version>1.0.9</version>
       <scope>test</scope>
     </dependency>
     ...
</dependencies>

Springockito-annotations

This is a subproject of springockito that pushes the idea even further, ex:

@ContextConfiguration(loader = SpringockitoContextLoader.class,
locations = "classpath:/context.xml")
public class SpringockitoAnnotationsMocksIntegrationTest extends AbstractJUnit4SpringContextTests {
    
    @ReplaceWithMock
    @Autowired
    private InnerBean innerBean;

    @WrapWithSpy
    @Autowired
    private AnotherInnerBean anotherInnerBean;
     ....
}

and your mocks are ready to go - no need to write test-specific xml context files. More info here https://bitbucket.org/kubek2k/springockito/wiki/springockito-annotations

License

License is distributed under MIT license Copyright (c) 2011 Jakub Janczak

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Updated

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.