Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

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

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

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

Saved successfully!

Ooh no, something went wrong!