This page was exported from - JPinup Export date: Thu Jun 1 6:06:30 2017 / +0000 GMT Spring - JUnit Integration Quando si procede allo sviluppo di un'applicazione è importante riuscire a predisporre dei test specifici per le funzionalità che si andranno a implementare. Questo modello è chiamato Test Driven Development ed è molto utile per predisporre il progetto a test di regressione automatici, anche effettuati da strumenti disponibili per il continuous integration (ex: Jenkins). In Java, per predisporre questi test possiamo far uso di JUnit, già discusso in un articolo precedente. In questo articolo vediamo come sfruttare le potenzialità di Spring per predisporre dei test e attivare dei test specifici per ambiente. L'esempio mostra come configurare i bean tramite XML, facendo riferimento anche all'alternativa Java. - Creare un progetto Maven con il seguente POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>it.jpinup</groupId> <artifactId>Spring-test</artifactId> <version>1.0.0</version> <build> <sourceDirectory>src/main/java</sourceDirectory> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/test</directory> </resource> </resources> </build> <repositories> <repository> <id>jboss-public-repository-group</id> <name>JBoss Public Repository Group</name> <url>http://repository.jboss.org/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> <updatePolicy>never</updatePolicy> </releases> <snapshots> <enabled>true</enabled> <updatePolicy>daily</updatePolicy> </snapshots> </repository> </repositories> Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 1/6 | This page was exported from - JPinup Export date: Thu Jun 1 6:06:30 2017 / +0000 GMT <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.4.RELEASE</version> </dependency> </dependencies> </project> 2) Creare package it.jpinup.service, con la classe seguente all'interno package it.jpinup.service; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class AuthenticationService { private String username; private String password; @PostConstruct public void init(){ System.out.println("Start"); } public boolean login(String username,String password){ if (username.equals(this.username) && password.equals(this.password)){ return true; } return false; } @PreDestroy Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 2/6 | This page was exported from - JPinup Export date: Thu Jun 1 6:06:30 2017 / +0000 GMT public void destroy(){ System.out.println("End"); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } 3) Creare all'interno di it/jpinup/config (risorsa) il seguente file di configurazione, dove sono definiti due profili, uno per l'ambiente di sviluppo e l'altro per l'ambiente di produzione. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <beans profile="svi"> <bean class="it.jpinup.service.AuthenticationService"> <property name="username" value="test"></property> <property name="password" value="test"></property> </bean> </beans> <beans profile="prod"> <bean class="it.jpinup.service.AuthenticationService"> <property name="username" value="username"></property> <property name="password" value="password"></property> </bean> </beans> </beans> 4) Creare la classe di test, che chiameremo SpringTest. Notare @ RunWith, annotation necessaria per dare a Spring la responsabilità di occuparsi di avviare i test e gestire i Bean. @ContextConfiguration: indica il file di configurazione dei bean Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 3/6 | This page was exported from - JPinup Export date: Thu Jun 1 6:06:30 2017 / +0000 GMT @ ActiveProfiles: indica il profilo attivo, in questo caso "SVI" package it.jpinup.config; import static org.junit.Assert.assertTrue; import it.jpinup.service.AuthenticationService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:it/jpinup/config/ SpringTest-config.xml") @ActiveProfiles("svi") public class SpringTest { @Autowired private AuthenticationService AuthenticationService; @Test public void testLoginSvi() { assertTrue(AuthenticationService.login("test", "test")); } @Test public void testLoginProd() { assertTrue(AuthenticationService.login("username", "password")); } } 5) Avviare il test e osservare che il primo test ha esito positivo mentre il secondo negativo. 6) Provare ad attivare il profilo "prod", ottenendo il risultato contrario. Se si vuole configurare il bean del service tramite Java Config (sfruttando @Configuration), sostituiamo il file xml con due file Java: - SviConfig.java - ProdConfig.java SviConfig.java package it.jpinup.config; import it.jpinup.service.AuthenticationService; Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 4/6 | This page was exported from - JPinup Export date: Thu Jun 1 6:06:30 2017 / +0000 GMT import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration @Profile("svi") public class SviConfig { @Bean public AuthenticationService authenticationService(){ AuthenticationService authenticationService = new AuthenticationService(); authenticationService.setUsername("test"); authenticationService.setPassword("test"); return authenticationService; } } ProdConfig package it.jpinup.config; import it.jpinup.service.AuthenticationService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration @Profile("prod") public class ProdConfig { @Bean public AuthenticationService authenticationService(){ AuthenticationService authenticationService = new AuthenticationService(); authenticationService.setUsername("username"); authenticationService.setPassword("password"); return authenticationService; } } Il test diventerà package it.jpinup.config; import static org.junit.Assert.assertTrue; import it.jpinup.service.AuthenticationService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 5/6 | This page was exported from - JPinup Export date: Thu Jun 1 6:06:30 2017 / +0000 GMT import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes={SviConfig.class,ProdConfig.class}) @ActiveProfiles("svi") public class SpringTest { @Autowired private AuthenticationService AuthenticationService; @Test public void testLoginSvi() { assertTrue(AuthenticationService.login("test", "test")); } @Test public void testLoginProd() { assertTrue(AuthenticationService.login("username", "password")); } } Output as PDF file has been powered by [ Universal Post Manager ] plugin from www.ProfProjects.com | Page 6/6 |