服务类用@Service又名FooServiceImpl注释,这使其符合自动装配的条件。为什么在单元测试期间这个类没有被拾取和自动装配?@Component
@Service
public class FooServiceImpl implements FooService {
@Override
public String reverse(String bar) {
return new StringBuilder(bar).reverse().toString();
}
}
@RunWith(SpringRunner.class)
//@SpringBootTest
public class FooServiceTest {
@Autowired
private FooService fooService;
@Test
public void reverseStringShouldReverseAnyString() {
String reverse = fooService.reverse("hello");
assertThat(reverse).isEqualTo("olleh");
}
}
Run Code Online (Sandbox Code Playgroud)
测试未能加载应用程序上下文,
2018-02-08T10:58:42,385 INFO Neither @ContextConfiguration nor @ContextHierarchy found for test class [io.github.thenilesh.service.impl.FooServiceTest], using DelegatingSmartContextLoader
2018-02-08T10:58:42,393 INFO Could not detect default resource locations for test class [io.github.thenilesh.service.impl.FooServiceTest]: no resource found for suffixes {-context.xml}.
2018-02-08T10:58:42,394 INFO …Run Code Online (Sandbox Code Playgroud) 我有一个基本的 SpringBoot 应用程序。使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包为可执行 JAR 文件。
我有这个服务:
@Service
public class TdkRestApiService {
...
}
Run Code Online (Sandbox Code Playgroud)
我想测试:
@ContextConfiguration(classes={TdkApplicationConfig.class, TdkDevelopmentConfig.class})
@RunWith(SpringRunner.class)
public class TdkRestApiServiceTests {
/**
* The object being tested.
*/
@Autowired
TdkRestApiService tdkRestApiService;
@Test
public void getCallbacksByDeviceTypeTest () throws IOException {
tdkRestApiService.getCallbacksByDeviceType("2", "3");
}
}
Run Code Online (Sandbox Code Playgroud)
但我有一个错误:
ERROR o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@dd3b207] to prepare test instance [com.tdk.backend.service.TdkRestApiServiceTests@6db9f5a4]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.tdk.backend.service.TdkRestApiServiceTests': Unsatisfied dependency expressed through field 'tdkRestApiService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 REST API,其中有一个接口定义了由 4 个不同类实现的方法列表,并且将来有可能添加更多类。
当我收到来自客户端的 HTTP 请求时,URL 中包含一些信息,这些信息将确定需要使用哪个实现。
在我的控制器中,我希望端点方法包含一个 switch 语句,用于检查 URL 路径变量,然后使用适当的实现。
我知道我可以定义具体的实现并将其注入到控制器中,然后在 switch 语句中的每种特定情况下插入我想要使用的实现,但这看起来不太优雅或可扩展,原因有两个:
我现在必须实例化所有服务,即使我只需要使用一项服务。
该代码似乎可以更简洁,因为我实际上是使用相同的参数调用接口中定义的相同方法,虽然在示例中这并不是真正的问题,但在实现列表增长的情况下。 .. 案例和冗余代码的数量也是如此。
有没有更好的方案来解决此类情况呢?我正在使用 SpringBoot 2 和 JDK 10,理想情况下,我想实现最现代的解决方案。
我目前的方法
@RequestMapping(Requests.MY_BASE_API_URL)
public class MyController {
//== FIELDS ==
private final ConcreteServiceImpl1 concreteService1;
private final ConcreteServiceImpl2 concreteService2;
private final ConcreteServiceImpl3 concreteService3;
//== CONSTRUCTORS ==
@Autowired
public MyController(ConcreteServiceImpl1 concreteService1, ConcreteServiceImpl2 concreteService2,
ConcreteServiceImpl3 concreteService3){
this.concreteService1 = concreteService1;
this.concreteService2 = concreteService2;
this.concreteService3 = concreteService3;
}
//== REQUEST MAPPINGS ==
@GetMapping(Requests.SPECIFIC_REQUEST)
public ResponseEntity<?> handleSpecificRequest(@PathVariable String source,
@RequestParam …Run Code Online (Sandbox Code Playgroud)