Announcement Announcement Module
Collapse
No announcement yet.
component-scan problem in Eclipse project Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • component-scan problem in Eclipse project

    I'm trying to test a service that uses an aspect to manage a necessary connection.

    The aspect handles creating a pool, opening the connection, etc. It's working when the webapp war is deployed. It's not working in a simple unit test I've written.

    The test is something very close to this:
    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("/test.xml")
    public class GatewayTest {
    	
    	@Autowired
    	GatewayImpl gatewayImpl;
    	
    	@Test
    	public void testConnection() throws Exception {
    
    		assertNotNull(gatewayImpl.getManagedConnection());
    	...
    Running the test, I get a BeanCreationException, on that @Autowired annotation at the GatewayImpl.

    I have this in test.xml:
    <context:component-scan base-package="com.example" />
    In this Eclipse project, there are multiple source directories, including src/test/java/com/example and src/main/java/com/example.

    The GatewayImpl I need for the test is in src/main, not src/test. src/test doesn't even have a GatewayImpl.

    Anyone have any idea how to resolve this?


    Thanks,
    Colin

  • #2
    Please use the search as these questions have been answered numerous times before... Program to interfaces not concrete classes. So use Gateway (the interface) instead of GatewayImpl..

    Comment


    • #3
      Right, the thought occurred to me as I was driving home last night. The only change I made was moving to the Gateway from GatewayImpl, and there ya go, test works.

      Why was the component-scan failing? It scans differently on classes and interfaces?

      Comment


      • #4
        The component-scan isn't failing your testcase is... I suggest a read of the AOP chapter in the reference guide this explains how AOP is applied. In short spring uses proxies to apply AOP which creates an object of type Gateway which wraps around the actual class.

        Comment

        Working...
        X