-
Notifications
You must be signed in to change notification settings - Fork 638
@RabbitListenerTest doesn't work anymore #969
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Works for me with Boot-2.1.4 and its transitive Spring AMPQ, which is indeed @RunWith(SpringRunner.class)
@SpringBootTest
public class BootRabbitListenerTestApplicationTests {
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private Queue queue1;
@Autowired
private RabbitListenerTestHarness harness;
@Test
public void testTwoWay() throws Exception {
assertEquals("FOO", this.rabbitTemplate.convertSendAndReceive(this.queue1.getName(), "foo"));
RabbitListenerTestHarness.InvocationData invocationData =
this.harness.getNextInvocationDataFor("foo", 10, TimeUnit.SECONDS);
assertNotNull(invocationData);
assertThat(invocationData.getArguments()[0], equalTo("foo"));
assertThat(invocationData.getResult(), equalTo("FOO"));
}
@TestConfiguration
@RabbitListenerTest(spy = false, capture = true)
public static class Config {
@Bean
public Queue queue1() {
return new AnonymousQueue();
}
@RabbitListener(id = "foo", queues = "#{queue1.name}")
public String foo(String foo) {
return foo.toUpperCase();
}
}
} I have nothing more in my project though, just only this test class and an empty so, something fishy is in your project. Would be great to have simple variant of that so we can reproduce and play with on our side. Thanks for understanding. |
Hmm, I think I found the root cause. @Configuration
@Profile("integration-test")
@EnableRabbit
public class TestAmqpConfiguration {
private static final Logger LOGGER = Logger.getLogger(TestAmqpConfiguration.class.getName());
static final String TRANSACTIONS_LISTENER = "transactions";
@Bean
public MessageRecoverer messageRecoverer(AmqpTemplate amqpTemplate) {
// will send the error details to a queue named `error.${queues.transactions}`
return new RepublishMessageRecoverer(amqpTemplate);
}
@RabbitListener(id = TRANSACTIONS_LISTENER, queues = "${queues.transactions}", returnExceptions = "true")
public void receive(SalesTransaction transaction) {
LOGGER.log(Level.INFO, "Received tx: {0}", transaction);
}
@Configuration
@ConditionalOnBean(name = "rabbitListenerContainerFactory")
public static class TestConfigurationInitializer {
private final AbstractRabbitListenerContainerFactory rabbitListenerContainerFactory;
private final PlatformTransactionManager txManager;
public TestConfigurationInitializer(AbstractRabbitListenerContainerFactory rabbitListenerContainerFactory,
PlatformTransactionManager txManager) {
this.rabbitListenerContainerFactory = rabbitListenerContainerFactory;
this.txManager = txManager;
}
@PostConstruct
void init() {
LOGGER.log(Level.INFO,
"Setting tx manager {0} on the rabbitListenerContainerFactory {1}",
new Object[]{txManager, rabbitListenerContainerFactory});
rabbitListenerContainerFactory.setTransactionManager(txManager);
rabbitListenerContainerFactory.setChannelTransacted(Boolean.TRUE);
}
}
} As the profile name says, it's used to perform an integration test at runtime in our test environments, so I've thought I should reuse it so the listener defined here was invoked. The problem seems to be caused by the fact that this Is there a way to make this kind of setup work? |
Doesn't look like you need that On the other hand it is not clear, what and how you want to reuse (too much custom code), so, I wouldn't rely on that external configuration, but just copy/pasted whatever I need in my current test for better isolation and to avoid some conflicts in the future when that class may be modified and it will affect your one as well. Doesn't look like a problem of the Framework, but more design flaw in the target project. Thanks for understanding. |
Indeed, your solution worked, removing Sorry for the noise. |
Affects Version(s): 2.1.5
I've just upgraded to Spring AMQP 2.1.5, triggered automatically by upgrading to Spring Boot 2.1.4 (and Spring Framework 5.1.6), and now my integration test isn't working anymore.
With
spring.main.allow-bean-definition-overriding=true
:Seeing that this was caused by PR #915 for issue #914, which was asking for tests to work without bean definition overriding enabled, I switched that to
false
, but that also fails:The text was updated successfully, but these errors were encountered: