我必须做些什么来解决,尽管Springboot应用程序会像测试一样进行自动配置,但是测试类无法解决自动装配的依赖关系?当然,我没有实例化自己的对象。有简单的方法吗?
课程:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {PostClient.class, NumberplateClientCommands.class})
public class NumberplateClientCommandsTest {
private StandardMethodTargetRegistrar registrar = new StandardMethodTargetRegistrar();
private ConfigurableCommandRegistry registry = new ConfigurableCommandRegistry();
private Map<String, MethodTarget> commands;
@Autowired
public PostClient postClient;
@Before
public void setUp() {
ApplicationContext context =
new AnnotationConfigApplicationContext(NumberplateClientCommands.class);
registrar.setApplicationContext(context);
registrar.register(registry);
}
@Test
public void shouldAddTwoIntegers() {
commands = registry.listCommands();
MethodTarget methodTarget = commands.get("sum");
assertThat(methodTarget, notNullValue());
Assertions.assertThat(methodTarget.getGroup()).isEqualTo(
"Numberplate Client Commands");
assertThat(methodTarget.getHelp(), Is.is("Add up to sum."));
assertThat(methodTarget.getMethod(), is(
ReflectionUtils.findMethod(NumberplateClientCommands.class, "sum", int.class,
int.class)));
assertThat(methodTarget.getAvailability().isAvailable(), is(true));
assertEquals(3, ReflectionUtils.invokeMethod(methodTarget.getMethod(),
methodTarget.getBean(), 1, 2));
}
@Test
public void shouldSayHi() {
commands = registry.listCommands();
MethodTarget methodTarget = commands.get("say-hi");
assertThat(methodTarget, notNullValue());
Assertions.assertThat(methodTarget.getGroup()).isEqualTo(
"Numberplate Client Commands");
assertThat(methodTarget.getHelp(), Is.is("Saying hi to a given person's name."));
assertThat(methodTarget.getMethod(), is(
ReflectionUtils.findMethod(NumberplateClientCommands.class, "sayHi", String.class)));
assertThat(methodTarget.getAvailability().isAvailable(), is(true));
assertEquals("Hi, Nadine", ReflectionUtils.invokeMethod(methodTarget.getMethod(),
methodTarget.getBean(), "Nadine"));
}
@Test
public void shouldPostCamImage2kafka() throws FileNotFoundException {
commands = registry.listCommands();
MethodTarget methodTarget = commands.get("one");
NumberPlateUtility np = new NumberPlateUtility();
HttpStatus response = postClient.postNumberPlate(np.completeImage());
assertThat(response, is(HttpStatus.OK));
}
}
例外是:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'numberplateClientCommands': Unsatisfied dependency expressed through field 'postClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'dev.semo.npgen.service.PostClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我分享了my repository on GitHub,如果有帮助的话。其他问题和答案显示了非常具体的解决方案,这些解决方案通常取决于某些@Repository或其他类似MVC的组件,接口或复杂的控制器,而我没有。
答案 0 :(得分:0)
之所以发生这种情况,是因为您在测试的setUp(before)方法中创建了另一个应用程序上下文,并在那里仅选择一个配置文件(NumberplateClientCommands)。
我建议您尝试这样的事情:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {PostClient.class, NumberplateClientCommands.class})
public class NumberplateClientCommandsTest {
...
@Autowired
public ApplicationContext applicationContext;
@Before
public void setUp() {
registrar.setApplicationContext(applicationContext);
registrar.register(registry);
}
@Test
public void shouldAddTwoIntegers() {
...
}