Announcement Announcement Module
Collapse
No announcement yet.
Autowire dependencies not found Unit Test Maven Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowire dependencies not found Unit Test Maven

    Sorry about the cryptic title but I wanted to capture the essentials of the issue. I am running into a wall trying to configure a Junit test with

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"/IntegrationTest-context.xml"})
    public class UserServiceIntegrationTest {


    In the past I have created my own bootstrapper classes using ClasspathXmlContext and written code in unit test base classes to set up dependencies correctly, but I wanted to try this new Annotation based approach. At any rate I wrote a unit test:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"/IntegrationTest-context.xml"})
    public class UserServiceIntegrationTest {

    @Autowired
    private UserService userService;

    @Test
    public void testInitialization() {
    Assert.assertNotNull(userService);
    }
    }


    Now the class to be insantiated has an entry in it like so:

    @Service("userService")
    @Transactional
    public class UserService extends ServiceBase {
    private static Logger logger = Logger.getLogger(UserService.class);

    @Resource(name = "mongoTemplate")
    private MongoTemplate mongoTemplate;


    At any rate when I go to run the Unit test in eclipse, spring throws an error indicating that it cannot find the class for the MongoTemplate dependency.
    Caused by: java.lang.ClassNotFoundException: org.springframework.data.document.mongodb.MongoTem plate

    I am using maven to resolve dependencies but apparently the SpringJUnit4ClassRunner doesn't seem to be aware of this dependency resolution.

    Any thoughts on how to get the Junit tests to use the Maven dependencies?

    I love answering my own questions, stupid user me

    <!-- Offers convenience methods and automatic mapping between MongoDB
    JSON documents and your domain classes. -->
    <bean id="mongoTemplate" class="org.springframework.data.document.mongodb.M ongoTemplate">
    <constructor-arg ref="mongo" />
    <constructor-arg value="doitnext" />
    </bean>

    It would help if the class declared in the IntegrationTest-context.xml was correct.
    Last edited by steowens; Sep 4th, 2011, 11:54 AM.
Working...
X