我正在尝试在CourseServiceImpl的courseDao字段中添加模拟对象,但它无效。
class FoodMenuTableVC: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
itemList.count + 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cellIdentifier = "headerCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! HeaderCell
//cell.orderPriceLabel.text = ....?
return cell
} else {
//This works fine
let cellIdentifier = "itemCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ItemCell
let name = localSelectedVendorName!
let keys = Array(itemList.keys)
let values = Array(itemList.values)
cell.itemPriceLabel.text = String(values[indexPath.row - 1].price)
cell.itemOrderCountLabel.text = " "
}
return cell
}
反射语句会抛出一个错误字段" courseDao"没找到。但是,当我使用new运算符创建一个对象时,它可以正常工作。
@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);
}
servlet的config.xml中
ReflectionTestUtils.setField(new CourseServiceImpl(), "courseDao", courseDao, CourseDao.class);
冬眠-CONFIG-的test.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文件也是一样。
CourseServiceImpl
<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" />
请告知。
答案 0 :(得分:1)
您的CourseServiceImpl
类具有@Transactional
注释,这意味着bean实例在"Transational"
中作为依赖项注入CourseServiceTest
代理,并在Spring上下文中注入所有其他bean。此类代理实例会隐藏原始private
实例的所有CourseServiceImpl
字段。
因此,您无法访问所需的字段,因为已注入的courseService
实例不再是原始CourseServiceImpl
类,而是动态cglib
或JDK
代理类。