У меня небольшие проблемы, инъекция bean-компонента при запуске в режиме весенней загрузки все работает хорошо, но с Junit. У меня есть пример с RabbitTemplate
public class NettyServerRun {
@Autowired
private IDeviceClient deviceClientComponent;
@Autowired
private ServiceDeviceServer service;
@Autowired
private RabbitManager mqService;
private int port = 7650;
@PostConstruct
public void init() throws InterruptedException {
NioEventLoopGroup boosGroup = new NioEventLoopGroup();
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boosGroup, workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
final EventExecutorGroup group = new DefaultEventExecutorGroup(1500);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(workerGroup,new
RequestDecoderServer(deviceClientComponent));
pipeline.addLast(workerGroup,new ResponseEncoderServer());
pipeline.addLast(group,new AuthenticateHandler(service));
pipeline.addLast(group,new CommandResponseHandler(service));
pipeline.addLast(group,new DeviceDataHandler(mqService));
}
});
ChannelFuture f = bootstrap.bind(port).sync();
f.channel().closeFuture().sync();
}
@RunWith(SpringRunner.class)
@DataJpaTest
@Import(NettyServerRun.class)
@AutoConfigureTestDatabase(replace=Replace.ANY)
@ComponentScan("com.metier")
public abstract class DeviceServerTest {
@Autowired
protected TestEntityManager entityManager;
@Autowired
protected ServiceDeviceServer service;
@Autowired
protected TestRestTemplate template;
protected DeviceServerContext context;
protected Gson gson;
protected Nmea nmeaData;
@Value("${spring.profiles.active}")
private String activeProfile;
protected void persistList(List<AbstractEntity> list) {
list.forEach(entity -> entityManager.persist(entity));
}
public GpsCmdRsp buidGpsCmdRsp(Long gpsId) {
GpsCmdRsp reference = new GpsCmdRsp();
reference.setCommand("*TS01,188765,NAM#");
reference.setCompletedAt(buildHier());
reference.setGpsId(gpsId);
reference.setResponse("*TS01,353836057694499,013809281017,NAM:ODO50-
BLE#");
reference.setSuccess(false);
return reference;
}
public class DeviceDataHandlerTest extends DeviceServerTest {
@Autowired
private NettyServerRun nettyServerRun;
@Before
public void setUp() {
}
@Test
public void channelReadDeviceExistTest() {
String[] trame = {
"*TS01,351579056605817,003410140618,GPS:3;N46.758156;W71.134046;6;0;0.96,STT:c003;8001,MGR:957975,SAT:43;40;39#" };
EmbeddedChannel channel = new EmbeddedChannel(new DeviceDataHandler());
boolean ok = channel.writeInbound(trame);
assertThat(ok).isTrue();
}
}
Журнал ошибок
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.amqp.rabbit.core.RabbitTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1654) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1213) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1167) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:593) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
... 44 common frames omitted ##
Итак, как я уже сказал. если я запускаю только с Spring-Boot, инъекция RabbitTemplate работает нормально. Но, если бежать с Junit. Я пытаюсь добавить @SpringBootTest
java.lang.IllegalStateException: Configuration error: several statements found from @BootstrapWith for test class [com.AuthenticateHandlerTest]: [@ org.springframework.test.context.BootstrapWith (value = class org.springframework.boot.tocon. test.autoconfigure.orm .jpa.DataJpaTestContextBootstrapper), @ org.springframework.test.context.BootstrapWith (value = class org.springframework.boot.test.context.SpringBootTestContextBootstrapper)]
at org.springframework.test.context.BootstrapUtils.resolveExplicitTestContextBootstrapper (BootstrapUtils.java:166)
org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper (BootstrapUtils.java:127).
Из-за @ DataJpaTest