【发布时间】:2026-02-08 11:30:01
【问题描述】:
在Car 类构造函数中,我使用@Value 注解将值作为参数注入。
@Service
public class Car {
public Car(@Value("${app.fabric.kafka.topics.sometopic}") final String topic) {
}
...
}
GarageService 类声明并使用Car 实例。
@Component
@AllArgsConstructor
public class GarageService {
private @NonNull final Car;
...
}
在GarageServiceTest 类中,我使用@InjectMock 实例化测试对象并使用@Mock 注入另一个实例。
@ContextConfiguration
public class GarageServiceTest {
@Mock private CarService carService;
@InjectMock private GarageService;
...
}
这个类中的测试给出了一个异常:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error when creating bean 'GarageService' defined in file [.../GarageService.class]:
Unsatisfied dependency expressed through constructor parameter 1,
...
nested exception is org.springframework.beans.factory.BenCreationException:
Error creating bean with name 'Car' defined in file [.../Car.class]: Unexpected exception during bean
creation; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder
app.fabric.kafka.topics.sometopic' in value "${app.fabric.kafka.topics.sometopic}".
问题如何解决这个异常?我的理解是,在那个测试类中,我需要找到一种方法将${app.fabric.kafka.topics.sometopic}" 值注入 bean `Car'。
【问题讨论】:
-
这应该是一个 unit 测试还是更全面的基于 Spring 的功能测试?你在混合风格。
-
这只是从更复杂的代码中提取的高级内容。它的单元测试。它在一个依赖于另一个实例的类中测试方法。
-
所以?使用构造函数注入的主要优点之一是您可以调用
new ServiceUnderTest(mockDependency, string)。
标签: java spring mockito junit5