Announcement Announcement Module
No announcement yet.
@Autowired does not work under Maven Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Autowired does not work under Maven

    Hello! I'm moving my unit-tests from JUnit 3.8 to JUnit 4.4 and switching them to use new annotations from Spring 2.5.

    As result everything works quite fine under Eclipse (tests are run, all dependencies are injected, tests are performed, changed rolled-back (mostly))

    But then I tried to run tests under maven (used as build-system for our project) I met the problem - no any @Autowired dependency was injected (so, I've got many NPE exception then tried to use this dependencies).

    I searched forum and internet and found thread here:
    but there author had opisite problem: everything worked fine under Maven and does not worked under Eclipse.

    I alos looked into

    Solution proposed there (switch from using asm 1.5.3 to 2.2.3) does not helped in my case.

    Does anybody has any idea why it may happens?

    My environment: Spring 2.5.1 (cannot switch to 2.5.2 due to some incompatibilities with current version CXF), Java 1.6 (version does not make sense), Maven 2.0.8

    Major question - why it is worked under Eclipse - but does not work under maven?

  • #2
    Make sure that every version of all the jars are correct! Make sure that there are no duplicate/incompatible versions in your classpath. So make sure that you use the same JUnit jars, spring jars etc. as you do in eclipse.

    I hope that you are using maven also to create your eclipse stuff (maven eclipse:eclipse) so that the versions should be/are correct.

    Also make sure that your compile and test classpath are setup correctly (i.e. that spring-test.jar is there for testing!).


    • #3
      Yes, what is strange - i'm using eclipse:eclipse for eclipse project generation, so, actually, working eclipse project is build from maven.

      But - it works in Eclipse and does not work with Maven...

      spring-test.jar in classpath - I'm sure - since there is no any compilation errors (my unit-test inherited AbstractTransactionalJUnit4SpringContextTests)

      So, as I wrote - everything compilted and run quite well - but - it looks like JUnit under maven do not see @Autowired annotations - and did not inseeterd any dependencies.

      I do not see any other warning or error messages except final NPE, produced by fact that required dependencies are not set

      As I understand - spring 2.5 included ASM into spring-core so, asm-incompatibility should not be a problem.

      What (in general) may couse such case of problems.

      I looked into dependenciy report generated by maven site:
      'Dependency Convergence' do not displayed any version incompatibilities.

      in 'Dependency' report I can see, that only asm 2.2.3 is used - as well as other required libraries are in correct version.

      Result war-file (build with skipping tests) include ONLY ask-2.2.3 - no any other version of asm

      But - I found what asm:asm-attr:1.5.3 is used for 'Compile' via 'Transitive Dependencies'. May it cause such problems?


      • #4
        There shouldn't be any problems.

        Have you setup your maven correctly for testing? If I recall correctly the default surefire stuff doesn't work with JUnit 4 but rather JUnit 3 and you needed to change some stuff/configuration to make it work with 4.4.


        • #5
          Yes, test-cases do not required injection performed quite well, so, JUnit 4 working without problem.

          OK - I will try to reduce dependency of project to minimum to make it working and then will include dependencies step by step until Autowire will not stop to work. Seems it is only a way.

          Will report here what caused this problem.


          • #6
            Marten, you were right!

            Problem was in surefire configuration. By default version 2.2 were used. This version is not supported JUnit4. I do not know how it is called tests at all and caused only Autowired problem!

            So, setting
            in pom.xml helped my problem.