这是我的测试代码
package com.tasks;
@ContextConfiguration(classes = ITest.class)
@ActiveProfiles(profiles = "test")
@RunWith(SpringJUnit4ClassRunner.class)
public class ITest {
@Inject
IAccessor iAccessor1;
@Test
public void testRun() {
}
@Configuration
@Profile("test")
@ComponentScan(basePackageClasses{com.tasks.ITest.class})
public static class ITestConfiguration{
@Bean
@Primary
public IAccessor iDataAccessor(){
IAccessor iAccessor = mock(IAccessor.class);
return iAccessor;
}
}
}
我尝试使用@Autowired而不是Inject,但是遇到了同样的错误。
在我的课堂上我有
@Component
public class ISync {
@Inject
private IAccessor iAccessor;
public int someMethod(){
iAccessor.someOtherMethod(); //want to mock out
}
}
所以我想注入一个模拟值。但是我得到了一个依赖性错误
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 合格的bean类型 ' com.database.iAccessor1'可供选择: 预计至少有1个豆有资格成为autowire候选人。 依赖注释:{@ javax.inject.Inject()}
它通常适合我,但我不知道在这种特殊情况下这里有什么问题。 我想要解决我的解决方案的问题。
我已经尝试过了: Spring JUnit: How to Mock autowired component in autowired component 1)接受的答案是使用testContext.xml 我不使用任何xml。 2)我使用了@MockBean,但我开始遇到其他错误。
java.lang.NoSuchMethodError: org.mockito.Mockito.mockingDetails(Ljava /郎/对象;)Lorg /的Mockito / MockingDetails;
为了纠正它我把mockito-all 1.9.0的版本碰到了1.9.5(虽然我不知道为什么要试图找到它)。
java.lang.NoSuchMethodError: org.mockito.internal.util.MockUtil.getMockSettings(Ljava /郎/对象;)Lorg /的Mockito /模拟/ MockCreationSettings;
我通过将版本提升到1.0.19来实现它。但是被测试的类中的值仍然是空的。所以这对我没用。
我想知道原始解决方案的问题是什么?
答案 0 :(得分:0)
让您的班级ISync
在构造函数中接受IAccessor
。
然后,在测试中,您将能够将模拟IAccessor
传递给它,然后验证someOtherMethod()
模拟器上是否已调用IAccessor
。
答案 1 :(得分:0)
的功能名称
boolean found = arr1.stream().anyMatch(s -> s.contains("string2"));
一旦我将其更改为iAccessor1(),它就开始工作了。 制作它
@Bean
@Primary
public IAccessor iDataAccessor(){
IAccessor iAccessor = mock(IAccessor.class);
return iAccessor;
}