25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 7 ■ Testing Your <strong>Spring</strong> Application<br />

import org.springframework.test.annotation.Repeat;<br />

import org.springframework.test.annotation.Timed;<br />

import org.springframework.test.context.ContextConfiguration;<br />

import org.springframework.test.context.junit4.<strong>Spring</strong>JUnit4ClassRunner;<br />

import com.apress.isf.java.model.Document;<br />

import com.apress.isf.java.model.Type;<br />

import com.apress.isf.java.service.SearchEngine;<br />

@RunWith(<strong>Spring</strong>JUnit4ClassRunner.class)<br />

@ContextConfiguration("classpath:META-INF/spring/mydocuments-context.xml")<br />

public class MyDocumentsMoreAnnotationsTest {<br />

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

@Autowired<br />

private SearchEngine engine;<br />

@Autowired<br />

private Type webType;<br />

@Timed(millis=2000)<br />

@Test<br />

public void testUsing<strong>Spring</strong>TimedAnnotationTest() throws InterruptedException {<br />

log.debug("Using <strong>Spring</strong> Test fixtures:");<br />

List documents = engine.findByType(webType);<br />

assertNotNull(documents);<br />

assertTrue(documents.size() == 1);<br />

assertEquals(webType.getName(),documents.get(0).getType().getName());<br />

assertEquals(webType.getDesc(),documents.get(0).getType().getDesc());<br />

assertEquals(webType.getExtension(),documents.get(0).getType().getExtension());<br />

Thread.sleep(500);<br />

}<br />

documents = engine.listAll();<br />

assertNotNull(documents);<br />

assertTrue(documents.size() == 4);<br />

}<br />

@Repeat(10)<br />

@Test<br />

public void testUsing<strong>Spring</strong>RepeatedAnnotationTest() {<br />

log.debug("This message should be printed 10 times..");<br />

}<br />

If you run the test (see Listing 7-8) with<br />

gradle :ch07:test<br />

84

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

Saved successfully!

Ooh no, something went wrong!