我正在尝试在CourseServiceImpl的courseDao字段中添加模拟对象,但无法正常工作。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
    locations = {"file:src/main/webapp/WEB-INF/config/servlet-config.xml"}
)
@ActiveProfiles("test")
public final class CourseServiceTest {
  @Mock
  private CourseDao courseDao;
  @Autowired
  private CourseService courseService;
  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
  }

@Test
  public void testCourseServiceNotNull() {
    assertNotNull(courseService);
    assertNotNull(courseDao);
    ReflectionTestUtils.setField(courseService, "courseDao", courseDao, CourseDao.class);
  }

反射语句引发错误,未找到字段“courseDao”。但是,当我使用new运算符创建对象时,它可以正常工作。
ReflectionTestUtils.setField(new CourseServiceImpl(), "courseDao", courseDao, CourseDao.class);

servlet-config.xml
<mvc:annotation-driven />
<mvc:resources location="pdfs" mapping="/pdfs/**" />

<security:global-method-security
    pre-post-annotations="enabled" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/view/" p:suffix=".jsp" p:order="2" />

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"
    p:contentNegotiationManager-ref="contentNegId" p:defaultViews-ref="defaultViewList"
    p:order="1" />

<bean id="contentNegId"
    class="org.springframework.web.accept.ContentNegotiationManager">
    <constructor-arg>
        <bean
            class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
            <constructor-arg>
                <map>
                    <entry key="json" value="application/json" />
                    <entry key="xml" value="application/xml" />
                </map>
            </constructor-arg>
        </bean>
    </constructor-arg>
</bean>

<util:list id="defaultViewList">
    <bean
        class="org.springframework.web.servlet.view.json.MappingJackson2JsonView" />
    <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
        <constructor-arg>
            <bean class="org.springframework.oxm.xstream.XStreamMarshaller"
                p:autodetectAnnotations="true" />
        </constructor-arg>
    </bean>
</util:list>

<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"
    p:order="0" />

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver"
    p:defaultLocale="en" />

<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource"
    p:basename="messages" />

<context:property-placeholder location="classpath:messages.properties" />

<import resource="/hibernate-config.xml" />
<import resource="/hibernate-config-test.xml" />

hibernate-config-test.xml
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/school-repo-test" p:username="user"
        p:password="password" />


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
        p:dataSource-ref="myDataSource" p:hibernateProperties-ref="hibernateProps"
        p:annotatedClasses-ref="mapClasses">
    </bean>

    <util:list id="mapClasses">
        <value>org.school.model.Course</value>
        <value>org.school.model.Role</value>
        <value>org.school.model.Staff</value>
        <value>org.school.model.Student</value>
        <value>org.school.model.Subject</value>
    </util:list>

    <util:properties id="hibernateProps">
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.show_sql">true</prop>
    </util:properties>

    <bean id="txManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory" />

除配置文件名称外,hibernate-config文件的其余部分相同。

CourseServiceImpl
@Service(value = "courseService")
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public class CourseServiceImpl implements CourseService {
  @Autowired
  private MessageSource messageSource;
  @Autowired
  private CourseDao courseDao;
  @Autowired
  private ApplicationContext context;

请指教。

最佳答案

您的CourseServiceImpl类具有@Transactional批注,这意味着在将Beant实例作为依赖项注入"Transactional"以及Spring上下文中的所有其他bean之前,用CourseServiceTest代理包装的bean实例。这样的代理实例会隐藏原始private实例的所有CourseServiceImpl字段。

因此,由于注入的courseService实例不再是原始CourseServiceImpl类,而是动态cglibJDK代理类,因此您无法访问所需的字段。

08-03 12:09