Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 7 ■ Testing Your Spring Application @Autowired private SearchEngine engine; @Autowired private Type webType; @IfProfileValue(name = "environment", values = "dev") @Test public void testUsingSpringTestWithCustomProfilesX() { try{ log.debug("Using Spring Test fixtures:"); List documents = engine.findByType(webType); assertNotNull(documents); assertTrue(documents.size() == 1); assertEquals(webType.getName(),documents.get(0).getType().getName()); assertEquals(webType.getDesc(),documents.get(0).getType().getDesc()); assertEquals(webType.getExtension(),documents.get(0).getType(). getExtension()); } documents = engine.listAll(); assertNotNull(documents); assertTrue(documents.size() == 4); }catch(Exception ex){ log.error(ex.getMessage()); } @IfProfileValue(name = "os.name", values = "Unix") @Test public void testUsingSpringTestWithCustomProfilesY() { try{ log.debug("Using Spring Test fixtures on Unix:"); //More Testing } } }catch(Exception ex){ log.error(ex.getMessage()); } Listing 7-7 shows two new annotations: @ProfileValueSourceConfiguration that points to your CustomProfile class and tells the unit test that it will be required in order to perform some business logic, and the @IfProfileValue annotation where you passed a name (key) and the value returned. If you run the test, you are going to see the following output: 2014-02-16 19:55:11,903 DEBUG [main] SearchEngineService created: com.apress.isf.spring.service. SearchEngineService@3b897b54 2014-02-16 19:55:11,993 DEBUG [main] Document DAO set: com.apress.isf.spring.data. DocumentRepository@2c24be25 2014-02-16 19:55:12,026 DEBUG [main] Using Spring Test fixtures: 2014-02-16 19:55:12,026 DEBUG [main] Start Params: Type Definition: Name: WEB 82

Chapter 7 ■ Testing Your Spring Application Description: Web Link Extension: .url 2014-02-16 19:55:12,026 DEBUG [main] Start Params: 2014-02-16 19:55:12,026 DEBUG [main] Start Params: 2014-02-16 19:55:12,026 DEBUG [main] End Result:[Lcom.apress.isf.java.model. Document;@b29b889 2014-02-16 19:55:12,027 DEBUG [main] End Result: [com.apress.isf.java.model. Document@60123018, com.apress.isf.java.model.Document@4a13a8ee, com.apress.isf.java.model. Document@50b7c740, com.apress.isf.java.model.Document@517a6426] 2014-02-16 19:55:12,027 DEBUG [main] End Result: [com.apress.isf.java.model. Document@517a6426] 2014-02-16 19:55:12,027 DEBUG [main] Start Params: 2014-02-16 19:55:12,027 DEBUG [main] Start Params: 2014-02-16 19:55:12,027 DEBUG [main] End Result:[Lcom.apress.isf.java.model. Document;@3a452494 2014-02-16 19:55:12,027 DEBUG [main] End Result: [com.apress.isf.java.model. Document@60123018, com.apress.isf.java.model.Document@4a13a8ee, com.apress.isf.java.model. Document@50b7c740, com.apress.isf.java.model.Document@517a6426] 2014-02-16 19:55:12,029 DEBUG [main] Using Spring Test fixtures on Unix: The two tests run because the correct value is returned in each. ■ ■Note If you removed the @ProfileValueSourceConfiguration, then the unit test will be based on the System.getProperty(propertyKey). This means that if you add the Environment variable os.name with value Unix, then the test method testUsingSpringTestWithCustomProfilesY will be executed. More Test Annotations The Spring Framework provides even more annotations to test with. Let’s see some examples. Listing 7-8 shows the @Timed and @Repeat annotations that can be used only when using the SpringJUnit4ClassRunner or JUnit support classes. Listing 7-8. MyDocumentsMoreAnnotationsTest.java (@Timed, @Repeat) package com.apress.isf.spring.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; 83

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

@Autowired<br />

private SearchEngine engine;<br />

@Autowired<br />

private Type webType;<br />

@IfProfileValue(name = "environment", values = "dev")<br />

@Test<br />

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

try{<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().<br />

getExtension());<br />

}<br />

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

assertNotNull(documents);<br />

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

}catch(Exception ex){<br />

log.error(ex.getMessage());<br />

}<br />

@IfProfileValue(name = "os.name", values = "Unix")<br />

@Test<br />

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

try{<br />

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

//More Testing<br />

}<br />

}<br />

}catch(Exception ex){<br />

log.error(ex.getMessage());<br />

}<br />

Listing 7-7 shows two new annotations: @ProfileValueSourceConfiguration that points to your CustomProfile<br />

class and tells the unit test that it will be required in order to perform some business logic, and the @IfProfileValue<br />

annotation where you passed a name (key) and the value returned. If you run the test, you are going to see the<br />

following output:<br />

2014-02-16 19:55:11,903 DEBUG [main] SearchEngineService created: com.apress.isf.spring.service.<br />

SearchEngineService@3b897b54<br />

2014-02-16 19:55:11,993 DEBUG [main] Document DAO set: com.apress.isf.spring.data.<br />

DocumentRepository@2c24be25<br />

2014-02-16 19:55:12,026 DEBUG [main] Using <strong>Spring</strong> Test fixtures:<br />

2014-02-16 19:55:12,026 DEBUG [main] Start Params: Type Definition:<br />

Name: WEB<br />

82

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

Saved successfully!

Ooh no, something went wrong!