Java 使用mockito测试服务,使用自动连线测试ioC
我试图用Mockito和testNG测试一个服务,但我有一些疑问。如果服务声明如下所示,则必须创建get/set以注入服务:Java 使用mockito测试服务,使用自动连线测试ioC,java,spring,mockito,Java,Spring,Mockito,我试图用Mockito和testNG测试一个服务,但我有一些疑问。如果服务声明如下所示,则必须创建get/set以注入服务: @Autowired(required = true) protected ITipService serveiTip; 当我尝试使用maven清理和打包时,我发现了以下异常: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean wit
@Autowired(required = true)
protected ITipService serveiTip;
当我尝试使用maven清理和打包时,我发现了以下异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'consultaDeutes' defined in URL
[file:/D:/workspaceGPT/GPT/gpt.ui/target/test-classes/applicationContext-gui-deutes-Test.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'serveiTip' of bean class [cat.base.gpt.ui.ConsultaDeutesTest]: Bean property 'serveiTip' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
我相信使用自动接线时,get/set是不必要的
这是我的测试上下文:
?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:mockito="http://www.mockito.org/spring/mockito"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.mockito.org/spring/mockito https://bitbucket.org/kubek2k/springockito/raw/tip/springockito/src/main/resources/spring/mockito.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="cat.base.gpt.ui" />
<!-- mock del serveis que podem atacar per solicitar info -->
<mockito:mock id="serveiSubjecte" class="cat.base.tip.service.ISubjectesService"/>
<mockito:mock id="serveiTip" class="cat.base.tip.service.ITipService"/>
<mockito:mock id="serveiGpt" class="cat.base.gpt.domini.service.IGptService"/>
<mockito:mock id="sessio" class="cat.base.baseframe.session.IBaseSession"/>
<mockito:mock id="usuari" class="cat.base.baseframe.user.IBaseUser"/>
<!--
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:cat/base/bfp/ui/applicationResources" />
</bean>
-->
<bean name="consultaDeutes" class="cat.base.gpt.ui.ConsultaDeutesTest">
<property name="serveiTip" ref="serveiTip"/>
<property name="serveiGpt" ref="serveiGpt"/>
</bean>
</beans>
?xml version=“1.0”encoding=“UTF-8”>
应用程序上下文:
使用@Autowired将使spring自动向该字段注入匹配的bean。因此,不再需要在xml中定义“consultaDeutes”bean。如果您想使用xml定义,我相信您应该为尝试注入的每个属性定义一个setter,例如:servetip、serveiGpt 在测试中使用@Autowired可能需要对测试类的定义添加两个注释:
@ContextConfiguration(value = "/myContext.xml")
//@RunWith(SpringJUnit4ClassRunner.class) This is JUnit specific
@ActiveProfiles("dev")
public class TestCompareService {
@Autowired(required = true)
protected ITipService serveiTip;
....
}
实际上,我在粘贴特定于JUnit的@RunWith注释时犯了一个错误。对于TestNG,您可以进行查找。道歉你觉得呢?但是如果需要用户咨询我的网站流量?也许我不明白你的意思。如果你需要在测试和pro代码中使用不同的内容并保留相同的xml文件,你可以随时利用配置文件:。将“production”配置文件定义为默认配置文件,并在测试中使用@ActiveProfiles(“dev”)或您选择的任何名称(我也将其添加到原始示例中)注释测试类,好的!你是对的,在上下文中,如果我使用autowiring注入,我不需要添加属性,我会发布结果端。如果仍然希望test和pro之间有不同的内容,并且你不介意按照示例建议创建多个spring配置xml(applicationContext gui deutes test.xml)您可以使用@ContextConfiguration(value=“/myContext.xml”)为当前测试选择所需的一个
@ContextConfiguration(value = "/myContext.xml")
//@RunWith(SpringJUnit4ClassRunner.class) This is JUnit specific
@ActiveProfiles("dev")
public class TestCompareService {
@Autowired(required = true)
protected ITipService serveiTip;
....
}