在Spring测试中,reflectiontesttils不能与@Autowired一起工作



我试图在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注释,这意味着bean实例在CourseServiceTest和Spring上下文中的所有其他bean作为依赖注入之前使用"Transactional"代理包装。该代理实例隐藏了原CourseServiceImpl实例的所有private字段。

所以你不能访问你想要的字段,因为注入的courseService实例不再是原来的CourseServiceImpl类,它是动态的cglibJDK代理类。

相关内容

  • 没有找到相关文章

最新更新