25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 18 ■ <strong>Spring</strong> and Groovy<br />

class MyDocumentsTest {<br />

private static final Logger log = LoggerFactory.getLogger(MyDocumentsTest.class)<br />

private final RECORDS = 6<br />

private final ID = "1acbb68a-a859-49c9-ac88-d9e9322bac55"<br />

private final ctx = new GenericGroovyApplicationContext("classpath:META-INF/groovy/<br />

mydocuments.groovy")<br />

@Test<br />

@Ignore<br />

void testJDBC(){<br />

log.info "Testing JDBC..."<br />

assertNotNull ctx<br />

def documentFacade = ctx.getBean("documentFacade")<br />

assertNotNull documentFacade<br />

int size = documentFacade.allDocuments.size()<br />

assertEquals RECORDS,size<br />

}<br />

assertNotNull documentFacade.findDocumentById(ID)<br />

@Test<br />

public void testProducer(){<br />

log.debug("Testing RabbitMQ Producer...");<br />

def documentFacade = ctx.getBean("documentFacade")<br />

def rabbitmqProducer = ctx.getBean("rabbitmqProducer")<br />

assertNotNull(rabbitmqProducer)<br />

assertNotNull(documentFacade)<br />

}<br />

}<br />

documentFacade.allDocuments.each{<br />

rabbitmqProducer.send it<br />

}<br />

sleep 5000<br />

Listing 18-7 shows the unit test. In this case, you wrote the unit test using Groovy, but you can use Java and<br />

still make the use of the Groovy scripts. So, what is the difference between this unit test versus the ones you did in<br />

previous chapters? The first big difference is that all your bean definitions are using the <strong>Spring</strong> DSL, and the second<br />

is that you are using the GenericGroovyApplicationContext class (that will load the groovy DSL files) instead of the<br />

ClassPathXmlApplicationContext class that loads only XML files.<br />

Run this test with<br />

gradle :ch18:test<br />

You will have the same output as in Chapter 16 because you are using the same bean descriptions and doing<br />

RabbitMQ messaging (don’t forget to have the RabbitMQ up and running).<br />

Take a moment and do a last review of the unit test and the <strong>Spring</strong> DSL. You will find that you got rid of all the<br />

XML verbosity and simplified the bean definitions, thanks to the Groovy programing language.<br />

258

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!