Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8567155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:49:04+00:00 2026-06-11T17:49:04+00:00

Using Spring 3.1.2, JUnit 4.10.0, and pretty new to both versions. I’m having the

  • 0

Using Spring 3.1.2, JUnit 4.10.0, and pretty new to both versions. I’m having the problem that I can’t get the annotation-based autowiring to work.

Below are two samples, the one not using annotations, which is working fine. And the second one using annotation, which doesn’t work, and I don’t find the reason. I followed the samples of spring-mvc-test pretty much.

Working:

package com.company.web.api;
// imports

public class ApiTests {   

    @Test
    public void testApiGetUserById() throws Exception {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("/com/company/web/api/ApiTests-context.xml");
        UserManagementService userManagementService = (UserManagementService) ctx.getBean("userManagementService");
        ApiUserManagementController apiUserManagementController = new ApiUserManagementController(userManagementService);
        MockMvc mockMvc = standaloneSetup(apiUserManagementController).build();

        // The actual test     
        mockMvc.perform(get("/api/user/0").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
    }
}

Failing, because userManagementService is null, not getting autowired:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration       // should default to ApiTests-context.xml in same package
public class ApiTests {

    @Autowired
    UserManagementService userManagementService;

    private MockMvc mockMvc;

    @Before
    public void setup(){
        // SetUp never gets called?!
    }

    @Test
    public void testGetUserById() throws Exception {

        // !!! at this point, userManagementService is still null - why? !!!       

        ApiUserManagementController apiUserManagementController 
            = new ApiUserManagementController(userManagementService);

        mockMvc = standaloneSetup(apiUserManagementController).build();

        // The actual test
        mockMvc.perform(get("/api/user/0").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
    }
}

Note that both test classes above should be using the same context configuration, and the userManagementService is defined in there.

ApiTests-context.xml:

<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="username" value="user"/>
        <property name="password" value="passwd"/>
    </bean>

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
          p:dataSource-ref="dataSource" p:mappingResources="company.hbm.xml">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
            </props>
        </property>
        <property name="eventListeners">
            <map>
                <entry key="merge">
                    <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
                </entry>
            </map>
        </property>
    </bean>

    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory"/>

    <!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->

    <context:annotation-config/>
    <tx:annotation-driven/>
    <context:mbean-export/>

    <!-- tried both this and context:component-scan -->
    <!--<bean id="userManagementService" class="com.company.web.hibernate.UserManagementServiceImpl"/>-->
    <context:component-scan base-package="com.company"/>

    <!-- Hibernate's JMX statistics service -->
    <bean name="application:type=HibernateStatistics" class="org.hibernate.jmx.StatisticsService" autowire="byName"/>

</beans>

and the UserManagementService (interface) as well as UserManagementServiceImpl has the @Service annotation.

Two minor questions/observations: setup() never gets called, even though it has the @Before annotation. Furthermore, I noticed that my test methods don’t get executed/recognized if they don’t start with the name ‘test’, which is not the case though with all spring-mvc-test samples I saw.

pom.xml:

    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>com.springsource.org.junit</artifactId>
        <version>4.10.0</version>
        <scope>test</scope>
    </dependency>

enter image description here

Update:

The problem only occurs when I run the tests from maven; it’s ok when I run the test from within my IDE (IntelliJ IDEA).

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.12.3</version>
            <configuration>
                <includes>
                    <include>**/*Tests.java</include>
                </includes>
            </configuration>
        </plugin>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:49:05+00:00Added an answer on June 11, 2026 at 5:49 pm

    Autowiring wont happen unless you do a component scan.

    Why have you commented it out in your code ?

    <!--<context:component-scan base-package="com.company"/>-->
    

    Also re:junit. If you’re in eclipse can you just go to the dependency tree view of the pom and filter on junit. Check you’re actually using that version and not pulling in an older junit.

    Edit: Ok I just checked your config and was able to get it working this side. My only guess can be is that you’re somehow running it with a bad test runner which is causing it to use the wrong junit.

    Edit 2 (SOLVED): So it turns out that the problem is because you are using a custom version of junit. Surefire looks for the provided junit library and cant find it. As a result it defaults to junit 3, which is what causes your app to skip loading the config.

    You can explictly specify the custom provider like

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.12.3</version>
        <dependencies>
          <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>2.12.3</version>
          </dependency>
        </dependencies>
      </plugin>
    

    But I have found that it does not work well with custom repos. If possible I would suggest to use the standard version of junit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In spring/junit you can load application context files using @ContextConfiguration such as @ContextConfiguration({classpath:a.xml, classpath:b.xml})
I'm using Spring 3.0.4 and JUnit 4.5. My test classes currently uses Spring's annotation
I am using Struts2, Spring 2 and Junit 4.5. My main question is, whether
I am using Spring MVC with Hibernate. My page has a button that creates
In my project I can successfully test database code. I'm using Spring, Hibernate, HSQLDB,
Can I specify the Jackson ObjectMapper that Spring's RestTemplate uses? I'm not 100% that's
I'm using JUnit-dep 4.10 and Hamcrest 1.3.RC2. I've created a custom matcher that looks
I am using Spring 3.2, Hibernate and JUnit 4. My Dao class is as
I am trying to execute a SQL script in my JUnit test using Spring.
I'm using Spring 3.1.0.RELEASE. I'm having trouble autowiring a private variable from a Spring4JUnitRunner

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.