1+ package com .baeldung .springamqpsimple ;
2+
3+
4+ import org .junit .Test ;
5+ import org .junit .runner .RunWith ;
6+ import org .springframework .amqp .rabbit .core .RabbitTemplate ;
7+ import org .springframework .beans .factory .annotation .Autowired ;
8+ import org .springframework .boot .test .context .SpringBootTest ;
9+ import org .springframework .boot .test .mock .mockito .MockBean ;
10+ import org .springframework .boot .test .web .client .TestRestTemplate ;
11+ import org .springframework .http .HttpStatus ;
12+ import org .springframework .http .ResponseEntity ;
13+ import org .springframework .test .context .ActiveProfiles ;
14+ import org .springframework .test .context .junit4 .SpringRunner ;
15+
16+ import static org .junit .Assert .assertEquals ;
17+ import static org .mockito .Mockito .verify ;
18+
19+ @ RunWith (SpringRunner .class )
20+ @ ActiveProfiles ("test" )
21+ @ SpringBootTest (webEnvironment =SpringBootTest .WebEnvironment .RANDOM_PORT )
22+ public class MessageControllerTest {
23+
24+ @ Autowired
25+ private TestRestTemplate restTemplate ;
26+
27+ @ MockBean
28+ private RabbitTemplate rabbitTemplate ;
29+
30+ @ Test
31+ public void whenPostingMessage_thenMessageIsCreated () {
32+ final String message = "Hello World!" ;
33+ ResponseEntity <Void > responseEntity = restTemplate .postForEntity ("/messages" , message , Void .class );
34+
35+ assertEquals (HttpStatus .CREATED , responseEntity .getStatusCode ());
36+ }
37+
38+ @ Test
39+ public void whenPostingMessage_thenMessageIsSentToBroker () {
40+ final String message = "Hello World!" ;
41+ restTemplate .postForEntity ("/messages" , message , Void .class );
42+
43+ verify (rabbitTemplate ).convertAndSend (SpringAmqpConfig .queueName , message );
44+ }
45+ }
0 commit comments