我是开发新手,特别是单元测试.我想我的要求很简单,但我很想知道别人的想法.
假设我有两个类似的 -
public class First {
Second second ;
public First(){
second = new Second();
}
public String doSecond(){
return second.doSecond();
}
}
class Second {
public String doSecond(){
return "Do Something";
}
}
Run Code Online (Sandbox Code Playgroud)
假设我正在为测试First.doSecond()方法编写单元测试.但是,假设,我想像Mock这样的Second.doSecond()类.我正在使用Mockito来做这件事.
public void testFirst(){
Second sec = mock(Second.class);
when(sec.doSecond()).thenReturn("Stubbed Second");
First first = new First();
assertEquals("Stubbed Second", first.doSecond());
}
Run Code Online (Sandbox Code Playgroud)
我看到模拟没有生效,断言失败了.有没有办法模拟我想测试的类的成员变量.?
@Injectand @Resource和@Autowired注释之间有什么区别?
我们什么时候应该使用它们?
我在春天定义了一张地图:
<util:map id="AdditionalParams" scope="prototype" map-class="java.util.HashMap"
key-type="java.lang.String" value-type="java.lang.String">
<entry key="Start" value="12345" />
<entry key="Finish" value="12365" />
</util:map>
Run Code Online (Sandbox Code Playgroud)
然后我将这个bean自动装配到一个定义为:
private @Autowired @Qualifier(value = "AdditionalParams") Map<String, String> additionalParams;
Run Code Online (Sandbox Code Playgroud)
执行此操作时,会抛出一个异常,说:
引起:org.springframework.beans.factory.BeanCreationException:创建名为'DutyCreator'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private java.util.Map DutyCreator.additionalParams; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:[java.lang.String中]找到的依赖性[地图值java.lang.String类型]无类型的匹配豆:预期至少1豆,其有资格作为候选自动装配对于这种依赖.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value = AdditionalParams)}
引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有找到类型为[java.lang.String]的匹配bean为依赖[map with value type java.lang.String]:期望至少有一个bean有资格作为autowire候选者对于这种依赖.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true),@ org.springframework.beans.factory.annotation.Qualifier(value = AdditionalParams)}
有任何想法吗?
干杯.
我遇到了一个@Autowired的例子
public class EmpManager {
@Autowired
private EmpDao empDao;
}
Run Code Online (Sandbox Code Playgroud)
我很好奇empDao如何得到集合,因为没有setter方法而且它是私有的.
我认为两种方式之间没有任何区别,@ Qualifier总是与@Autowired一起使用.
@Autowired
@Qualifier("alpha")
Run Code Online (Sandbox Code Playgroud)
VS
@Resource(name="alpha")
Run Code Online (Sandbox Code Playgroud)
有谁能让我知道区别?谢谢!
在java-spring网络应用程序中,我希望能够动态注入bean.例如,我有一个具有2种不同实现的接口:
在我的应用程序中,我正在使用一些属性文件来配置注入:
#Determines the interface type the app uses. Possible values: implA, implB
myinterface.type=implA
Run Code Online (Sandbox Code Playgroud)
我的注入实际上有条件地在属性文件中的属性值上加载.例如,在这种情况下myinterface.type = implA,只要我注入MyInterface,将注入的实现将是ImplA(我通过扩展条件注释完成了).
我希望在运行时 - 一旦属性发生更改,将发生以下情况(无需重新启动服务器):
myinterface.type=implBImplB将被注入到使用MyInterface的地方我想要刷新我的上下文,但这会产生问题.我想可能会使用setter进行注入,并在重新配置属性后重新使用这些setter.是否有这种要求的工作实践?
有任何想法吗?
UPDATE
正如一些人所建议我可以使用一个工厂/注册表来保存两个实现(ImplA和ImplB)并通过查询相关属性返回正确的实现.如果我这样做,我还有第二个挑战 - 环境.例如,如果我的注册表看起来像这样:
@Service
public class MyRegistry {
private String configurationValue;
private final MyInterface implA;
private final MyInterface implB;
@Inject
public MyRegistry(Environmant env, MyInterface implA, MyInterface ImplB) {
this.implA = implA;
this.implB = implB;
this.configurationValue = env.getProperty("myinterface.type");
}
public MyInterface getMyInterface() {
switch(configurationValue) {
case "implA":
return implA; …Run Code Online (Sandbox Code Playgroud) 在将其标记为重复之前的请求.我已经通过论坛,无法在任何地方找到问题的解决方案.
我正在使用Spring 3.2编写代码,一切都是纯粹基于注释的.代码接收从不同XSD文件派生的XML文件.
所以我们可以说,有五种不同的XSD(A1,A2,A3,A4,A5),我的代码接收任何类型的XML,我有逻辑来识别到达时的XML类型.
现在,我试图使用Spring OXM解组这些.但由于涉及多个XSD,我们实际上无法使用一个Un-marshaller.所以我们需要大约五个.
在Configuration课堂上,我添加了五个bean,如下所示:
@Bean(name="A1Unmarshaller")
public Jaxb2Marshaller A1Unmarshaller(){
Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
unMarshaller.setContextPath("package name for the classes generate by XSD A1");
}
@Bean(name="A2Unmarshaller")
public Jaxb2Marshaller A1Unmarshaller(){
Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
unMarshaller.setContextPath("package name for the classes generate by XSD A2");
}
@Bean(name="A3Unmarshaller")
public Jaxb2Marshaller A3Unmarshaller(){
Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
unMarshaller.setContextPath("package name for the classes generate by XSD A3");
}
@Bean(name="A4Unmarshaller")
public Jaxb2Marshaller A4Unmarshaller(){
Jaxb2Marshaller unMarshaller = new Jaxb2Marshaller();
unMarshaller.setContextPath("package name for the classes …Run Code Online (Sandbox Code Playgroud) 说我用春天,我有以下策略......
接口
public interface MealStrategy {
cook(Meat meat);
}
Run Code Online (Sandbox Code Playgroud)
第一战略
@Component
public class BurgerStrategy implements
MealStrategy {
@Autowired CookerDao cookeryDao;
@Override
public void cook(Meat meat) {
cookeryDao.getBurger(meat);
}
}
Run Code Online (Sandbox Code Playgroud)
下一步策略......
@Component
public class SausageStrategy implements
MealStrategy {
@Autowired CookerDao cookeryDao;
@Override
public cook(Meat meat) {
return cookeryDao.getSausage(meat);
}
}
Run Code Online (Sandbox Code Playgroud)
背景...
@Component
@Scope("prototype")
public class MealContext {
private MealStrategy mealStrategy;
public void setMealStrategy(MealStrategy strategy) {
this.strategy = strategy;
}
public void cookMeal(Meat meat) {
mealStrategy.cook;
}
}
Run Code Online (Sandbox Code Playgroud)
现在说这个上下文是通过mvc控制器访问的,比如...
@Autowired
private MealContext …Run Code Online (Sandbox Code Playgroud) 我无法理解@Resource注释的含义。我查看了在线资源,但它们似乎以难以理解的方式解释了相同的内容。@Resource如果可能的话,有人可以以简单的方式解释 的含义吗?
谢谢 !
这个星期我一直在努力学习Spring,JBoss,Maven,JPA和Hibernate,我玩得很开心.我对在类中注入资源的许多不同方法感到有些困惑.直到本周,我甚至不知道你可以以任何其他方式注入资源,而不是<property>在Spring XML配置中使用标记.
<bean id="catalogService" class="com.idbs.omics.catalog.service.CatalogService">
<property name="termDao" ref="termDao"></property>
</bean>
Run Code Online (Sandbox Code Playgroud)
当我开始尝试使用JPA时@PersistenceContext,我遇到了,但这似乎是一个非常公平的特殊情况.然后我开始阅读Spring的测试框架,我看到了第一个使用的示例,@Resource(name="catalogService")然后在Web服务示例中@Autowired崩溃了派对!
**The Question!**
Run Code Online (Sandbox Code Playgroud)
那么所有这些之间的区别是什么,是否有正确和错误的情况使用它们?我想我在这里寻找最好的做法.
干杯全都
有Spring和MyBatis的网络项目.我使用IntelliJ IDEA进行开发.虽然存在与数据访问对象的链接,但IDEA无法正确检查MyBatis bean并产生令人讨厌的欠压.
检查评论:
Could not autowire. No beans of 'ApplicationMapper' type found.
Run Code Online (Sandbox Code Playgroud)
我的Spring和MyBatis配置:Spring:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:spring/mybatis-config.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.db.gbs.gbsapps.rds.backend.model.integration.mapping"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
MyBatis的-config.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="mybatis/ApplicationMapper.xml"/>
</mappers>
</configuration>
Run Code Online (Sandbox Code Playgroud)
有没有办法解决这个小问题?
我不明白什么问题@Primary可以解决。
该文件说:
[@Primary]指示当多个候选者有资格自动装配单值依赖项时,应优先考虑Bean。如果候选对象中仅存在一个“主” bean,它将是自动装配的值。
示例代码:
@Configuration
class Configuration {
@Bean
@Primary
MyType bean1() {
return new MyType(1);
}
@Bean
MyType bean2() {
return new MyType(2);
}
}
Run Code Online (Sandbox Code Playgroud)
范例:
我有2个bean,bean1并且bean2都带有type MyType。bean1具有@Primary注释,因此当我将类型的对象自动装配MyType到某些构造函数时,bean1将被选择。
如果总是选择主bean,为什么有两个相同类型的bean有用吗?我何时以及如何使用bean2未标注为主要的?该示例显示了bean2冗余且未使用。
我有一些使用SpringJUnit4ClassRunner的测试用例,它使用@Resource注释来标记注入变量.
@Resource用作另一个可能在将来使用的DI框架.(@Resource vs @Autowired)
现在我开始使用Cucumber runner编写BDD测试用例.然而DI似乎没有发生.(@Autowired有效但不是@Resource)任何人都知道为什么不呢?
java ×11
spring ×11
annotations ×2
autowired ×2
bdd ×1
cdi ×1
cucumber-jvm ×1
inspection ×1
map ×1
mocking ×1
mockito ×1
mybatis ×1
polymorphism ×1
spring-boot ×1
spring-mvc ×1
xml ×1
xsd ×1