Sunday, 4 November 2012

Defining Mockito mock bean from Spring xml definition

You can define a spring bean in spring xml context that will be a Mockito mock of a given type:

  

But if you want to inject that bean by type, you have to define it as a Proxy of given interface:

    
      
        
      
    
    
      com.example.Repository
    
  

2 comments:

Tomek Kaczanowski said...

Interesting. I admit I haven't ever used Spring to create mocks. I prefer to have them directly in tests code so looking by the code alone I can see what is being mocked.

Java/JEE Developer said...

I have used tests with the test xml definition of the context mainly when I was writing test for Spring Integration projects, where much bounding of invocation is defined in the xml files.