Results 1 to 7 of 7
  1. #1
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    30
    Rep Power
    0

    Default Mock object behaviour

    Hi All,

    I am creating mock object for my service to test my Spring MVC controller. This service used some proxies for getting the results. Now my question is do I have to mock these proxies too in test class? I am getting error like
    Java Code:
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.myproject.MyProxy] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency
    My test class looks like
    Java Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    @WebAppConfiguration
    public class MyControllerTest {
    	
    	private MockMvc mockMvc;
    
    	@Mock
    	private MyService myService;
    
           ..........................................
    
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Mock object behaviour

    What happened when you tried?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: Mock object behaviour

    Just to be clear, where are these proxies?
    Are they part of the service you are mocking?

    Then I would be surprised if you were expected mock out things that aren't actually going to be called.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    30
    Rep Power
    0

    Default Re: Mock object behaviour

    In MyService I have
    Java Code:
    @Resource
    MyProxy myProxy;
    
    public void doSomething() {
      String resp = myProxy.getResponseStatus();
    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: Mock object behaviour

    So something else is wrong if Spring is looking to inject a proxy into a mock object.

    Now, it's possible Spring is simply going to load everything anyway, which would include your "real" MyService, in which case there should be a MyProxy somewhere.
    After all, you'll need one in real life.
    But I would hope for testing it wouldn't require that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    anjibman is offline Member
    Join Date
    Dec 2010
    Posts
    30
    Rep Power
    0

    Default Re: Mock object behaviour

    Do I have to add beans in test context defination file? Right now in MyControllerTest-context.xml I have
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context-3.2.xsd
    		http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    	
        <mvc:annotation-driven />
        
    </beans>

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,797
    Rep Power
    19

    Default Re: Mock object behaviour

    I'm going to have to say "what does the documentation say", as I would have to read it myself to check my memory.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 12-12-2012, 08:43 PM
  2. Please help me with Mock Objects.
    By fatabass in forum New To Java
    Replies: 1
    Last Post: 09-12-2012, 01:27 AM
  3. develop mock ups
    By karthikeyan_raju in forum NetBeans
    Replies: 0
    Last Post: 10-19-2009, 03:58 PM
  4. 800 Mock Questions for SCJP 1.6
    By Enthuware in forum Java Software
    Replies: 2
    Last Post: 01-16-2008, 11:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •