Java tutorial
package nc.noumea.mairie.appock.core.impl; /*- * #%L * Logiciel de Gestion des approvisionnements et des stocks des fournitures administratives de la Mairie de Nouma * %% * Copyright (C) 2017 Mairie de Nouma, Nouvelle-Caldonie * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program. If not, see * <http://www.gnu.org/licenses/gpl-3.0.html>. * #L% */ import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.junit.Assert; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.transaction.annotation.Transactional; import nc.noumea.mairie.appock.core.utility.DateUtil; import nc.noumea.mairie.appock.entity.Config; import nc.noumea.mairie.appock.repositories.ConfigRepository; import nc.noumea.mairie.appock.service.impl.AppockBaseTest; import nc.noumea.mairie.appock.services.ConfigService; @ContextConfiguration(locations = { "/META-INF/spring/applicationContext-test.xml" }) public class ConfigServiceImplTest extends AppockBaseTest { @Autowired ConfigService configService; @Autowired ConfigRepository configRepository; @PersistenceContext(unitName = "persistenceUnit") private EntityManager appockEntityManager; @Test @Transactional("appockTransactionManager") public void testRecuperationTitreApplicationEtGestionCache() { String titreApplication = "Mon premier titre"; createAndSaveConfig(Config.TITRE_APPLICATION, titreApplication); Config config = configRepository.findFirstByCle(Config.TITRE_APPLICATION); Assert.assertNotNull(config); Assert.assertEquals(config.getValeur(), titreApplication); Assert.assertEquals(configService.getValeur(Config.TITRE_APPLICATION), titreApplication); titreApplication = "Mon second titre"; config.setValeur(titreApplication); configRepository.save(config); config = configRepository.findFirstByCle(Config.TITRE_APPLICATION); Assert.assertNotNull(config); Assert.assertEquals(config.getValeur(), titreApplication); } @Test public void testGetValeurIntSansException() { String valeurIntErreur = "abc"; createAndSaveConfig("TEST_INT_ERREUR", valeurIntErreur); Assert.assertNull(configService.getValeurInt("TEST_INT_ERREUR", false)); } @Test(expected = RuntimeException.class) public void testGetValeurIntException() { String valeurIntErreur = "abc"; createAndSaveConfig("TEST_INT_ERREUR", valeurIntErreur); configService.getValeurInt("TEST_INT_ERREUR", true); } @Test public void testGetValeurInt() { Integer valeurInt = 12; createAndSaveConfig("TEST_INT", valeurInt.toString()); Assert.assertEquals(configService.getValeurInt("TEST_INT", true), valeurInt); } @Test public void testGetValeurDateSansException() { String valeurDateErreur = "abc"; createAndSaveConfig("TEST_DATE_ERREUR", valeurDateErreur); Assert.assertNull(configService.getValeurDate("TEST_DATE_ERREUR", false)); } @Test(expected = RuntimeException.class) public void testGetValeurDateException() { String valeurDateErreur = "abc"; createAndSaveConfig("TEST_DATE_ERREUR", valeurDateErreur); configService.getValeurDate("TEST_DATE_ERREUR", true); } @Test public void testGetValeurDate() { String valeurDate = "12/12/2016"; createAndSaveConfig("TEST_DATE", valeurDate); Assert.assertEquals(configService.getValeurDate("TEST_DATE", true), DateUtil.parseLocalDate(valeurDate)); } }