Results 1 to 4 of 4

Thread: component-scan problem in Eclipse project

  1. #1
    Join Date
    Oct 2012
    Posts
    3

    Question 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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..
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Oct 2012
    Posts
    3

    Default

    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?

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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