springockito-annotations

Springockito-annotations

Tired of writing custom xml files to override beans with mocks? - springockito-annotations is created for You. After adding to classpath, You only have to change integration test classes a little bit:

@ContextConfiguration(loader = SpringockitoContextLoader.class,
locations = <xml_contexts_you_used_before>)
public class SpringockitoAnnotationsMocksIntegrationTest extends AbstractJUnit4SpringContextTests {
...
}

and from now on.. :

Mocks

Replacing beans with mocks is simple as adding an annotated field to your test class:

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

...
}

the bean named 'innerBean' will be replaced with mock in your tests. If You want to verify some interactions, You can add @Autowired annotation to the field - so it gets injected.

Spies

Wrapping bean instances with spies is similar:

@ContextConfiguration(loader = SpringockitoContextLoader.class,
locations = "classpath:/context.xml")
public class SpringockitoAnnotationsMocksIntegrationTest extends AbstractJUnit4SpringContextTests {    

   @WrapWithSpy
   private InnerBean innerBean;

...
}

- and 'innerBean' is spied.

Spies/Mocks naming

You can now specify, in a number of ways, the name of bean that will be spied or mocked. Following examples are using @ReplaceWithMock, but they work equally well with @WrapWithSpy.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {"classpath:context.xml"})
public class NamingExample {

    //bean name, as in previous versions, defaults to field name  
    @ReplaceWithMock
    private SomeBean myBean; // bean name: "myBean"

    @ReplaceWithMock(beanNameStrategy = FIELD_NAME)
    private SomeBean myBean; //bean name: "myBean"

    @ReplaceWithMock(beanNameStrategy = FIELD_TYPE_NAME) 
    private SomeBean myBean; //bean name: "a.b.c.SomeBean"

    @ReplaceWithMock(beanName = "arbitraryName")
    private SomeBean myBean; //bean name: "arbitraryName"

    @Resource(name = "resourceBean")
    @ReplaceWithMock
    private SomeBean myBean; //bean name: "resourceBean"

    @Autowired
    @Qualifier("autowiredQualifierBean")
    @ReplaceWithMock
    private SomeBean myBean; //bean name: "autowiredQualifierBean"
}

In case of ambiguity the explicitly specified name/strategy wins, also springockito declarations win over these of Resource/@Autowired&@Qualifier.

Reseting mocks/spies (in 'experimental' package, hope you'll like it)

Using DirtiesMocksTestContextListener you can now declaratively reset mocks/spies defined with @ReplaceWithMock/@WrapWithSpy annotations. Just use @DirtiesMocks annotations in a similar way to Spring's @DirtiesContext.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = SpringockitoContextLoader.class, locations = {"classpath:context.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, DirtiesMocksTestContextListener.class})
public class ResetMocksSpiesExample{
    
    @DirtiesMocks
    public void shouldXYZ() {
        ...
    }
}

You can also declare @DirtiesMocks at class level as @DirtiesMocks(classMode = DirtiesMocks.ClassMode.AFTER_CLASS) or @DirtiesMocks(classMode = DirtiesMocks.ClassMode.AFTER_EACH_TEST_METHOD).

To save you from redeclaring standard Spring listeners you can reuse abstract org.kubek2k.springockito.annotations.experimental.junit.AbstractJUnit4SpringockitoContextTests class.

Maven2 usage

Just add these to your pom file:

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

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.