Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
Chapter 2 ■ Working with Classes and Dependencies As you can see, you just implemented the findByType and you used a private storage method that will retrieve some documents and their types. Also, you implemented the listAll method that directly uses the storage method. Note that this is a naïve example to start showing some of the Spring Framework features. Next, you need to test what you have done. So let’s create a unit test using JUnit (see Listing 2-5). Listing 2-5. MyDocumentsTest.java package com.apress.isf.java.test; import java.util.List; import org.junit.Test; import static org.junit.Assert.*; import com.apress.isf.java.model.Document; import com.apress.isf.java.model.Type; import com.apress.isf.java.service.MySearchEngine; import com.apress.isf.java.service.SearchEngine; public class MyDocumentsTest { private SearchEngine engine = new MySearchEngine(); @Test public void testFindByType() { Type documentType = new Type(); documentType.setName("WEB"); documentType.setDesc("Web Link"); documentType.setExtension(".url"); List documents = engine.findByType(documentType); assertNotNull(documents); assertTrue(documents.size() == 1); assertEquals(documentType.getName(), documents.get(0).getType().getName()); assertEquals(documentType.getDesc(), documents.get(0).getType().getDesc()); assertEquals(documentType.getExtension(), documents.get(0).getType().getExtension()); } } @Test public void testListAll() { List documents = engine.listAll(); assertNotNull(documents); assertTrue(documents.size() == 4); } 17
Chapter 2 ■ Working with Classes and Dependencies In Listing 2-5, you are going to test the two methods you implemented. As you can see in the code, you are creating just a simple document and asserting that it belongs to the Documents list that you are getting from calling the findByType method. Now, let’s run your test class using Gradle. Listing 2-6 shows the build.gradle file that you are going to use to run your unit test. Listing 2-6. build.gradle apply plugin: 'java' apply plugin: 'groovy' apply plugin: 'eclipse' apply plugin: 'idea' group = 'com.apress.isf' version = '1.0' repositories { mavenCentral() } dependencies { compile 'org.codehaus.groovy:groovy-all:2.2.0' compile 'org.springframework:spring-context:4.0.5.RELEASE' testCompile 'junit:junit:4.11' } test { testLogging { events 'started', 'passed' } } The following command will run the task: test. This will tell the Gradle tool that it needs to look for all the unit tests available in the code and execute them. $ gradle test After executing the above command, you should see something like the following output: :ch02:compileJava :ch02:compileGroovy :ch02:processResources UP-TO-DATE :ch02:classes :ch02:compileTestJava :ch02:processTestResources :ch02:testClasses :ch02:test com.apress.isf.java.test.MyDocumentsTest > testFindByType STARTED com.apress.isf.java.test.MyDocumentsTest > testFindByType PASSED 18
- Page 1 and 2: THE EXPERT’S VOICE ® IN SPRING
- Page 3 and 4: Contents at a Glance About the Auth
- Page 5 and 6: Introduction This book is an introd
- Page 7 and 8: Part I Spring Framework Basics The
- Page 9 and 10: Chapter 1 ■ Your First Spring App
- Page 11 and 12: Chapter 1 ■ Your First Spring App
- Page 13 and 14: Chapter 1 ■ Your First Spring App
- Page 15 and 16: Chapter 1 ■ Your First Spring App
- Page 17 and 18: Chapter 1 ■ Your First Spring App
- Page 19 and 20: Chapter 2 ■ Working with Classes
- Page 21: Chapter 2 ■ Working with Classes
- Page 25 and 26: Chapter 2 ■ Working with Classes
- Page 27 and 28: Chapter 2 ■ Working with Classes
- Page 29 and 30: Chapter 3 Applying Different Config
- Page 31 and 32: Chapter 3 ■ Applying Different Co
- Page 33 and 34: Chapter 3 ■ Applying Different Co
- Page 35 and 36: Chapter 3 ■ Applying Different Co
- Page 37 and 38: Chapter 3 ■ Applying Different Co
- Page 39 and 40: Chapter 3 ■ Applying Different Co
- Page 41 and 42: Spring Stereotypes Chapter 3 ■ Ap
- Page 43 and 44: Chapter 3 ■ Applying Different Co
- Page 45 and 46: Chapter 3 ■ Applying Different Co
- Page 47 and 48: Chapter 3 ■ Applying Different Co
- Page 49 and 50: Chapter 4 ■ Using Beans Scopes pu
- Page 51 and 52: Chapter 4 ■ Using Beans Scopes Th
- Page 53 and 54: Chapter 4 ■ Using Beans Scopes @A
- Page 55 and 56: Chapter 4 ■ Using Beans Scopes do
- Page 57 and 58: Chapter 5 ■ Working with Collecti
- Page 59 and 60: Chapter 5 ■ Working with Collecti
- Page 61 and 62: Chapter 5 ■ Working with Collecti
- Page 63 and 64: Chapter 5 ■ Working with Collecti
- Page 65 and 66: Chapter 6 ■ Using Resource Files
- Page 67 and 68: Chapter 6 ■ Using Resource Files
- Page 69 and 70: Chapter 6 ■ Using Resource Files
- Page 71 and 72: Chapter 6 ■ Using Resource Files
Chapter 2 ■ Working with Classes and Dependencies<br />
In Listing 2-5, you are going to test the two methods you implemented. As you can see in the code, you are<br />
creating just a simple document and asserting that it belongs to the Documents list that you are getting from calling<br />
the findByType method. Now, let’s run your test class using Gradle. Listing 2-6 shows the build.gradle file that you<br />
are going to use to run your unit test.<br />
Listing 2-6. build.gradle<br />
apply plugin: 'java'<br />
apply plugin: 'groovy'<br />
apply plugin: 'eclipse'<br />
apply plugin: 'idea'<br />
group = 'com.apress.isf'<br />
version = '1.0'<br />
repositories {<br />
mavenCentral()<br />
}<br />
dependencies {<br />
compile 'org.codehaus.groovy:groovy-all:2.2.0'<br />
compile 'org.springframework:spring-context:4.0.5.RELEASE'<br />
testCompile 'junit:junit:4.11'<br />
}<br />
test {<br />
testLogging {<br />
events 'started', 'passed'<br />
}<br />
}<br />
The following command will run the task: test. This will tell the Gradle tool that it needs to look for all the unit<br />
tests available in the code and execute them.<br />
$ gradle test<br />
After executing the above command, you should see something like the following output:<br />
:ch02:compileJava<br />
:ch02:compileGroovy<br />
:ch02:processResources UP-TO-DATE<br />
:ch02:classes<br />
:ch02:compileTestJava<br />
:ch02:processTestResources<br />
:ch02:testClasses<br />
:ch02:test<br />
com.apress.isf.java.test.MyDocumentsTest > testFindByType STARTED<br />
com.apress.isf.java.test.MyDocumentsTest > testFindByType PASSED<br />
18