Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package repository; import com.chevrier.legiondao.config.ConfigJpa; import com.chevrier.legiondao.entities.Lieu; import com.chevrier.legiondao.entities.Terrain; import com.chevrier.legiondao.repository.LieuRepository; import com.chevrier.legiondao.repository.TerrainRepository; import java.util.List; import junit.framework.Assert; import org.junit.After; import org.junit.AfterClass; import static org.assertj.core.api.Assertions.*; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.springframework.test.util.AssertionErrors.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = ConfigJpa.class) public class TerrainTest { @Autowired LieuRepository lieuRepository; @Autowired TerrainRepository terrainRepository; static private String NOM_TERRAIN = "sableTest"; public TerrainTest() { } @BeforeClass public static void setUpClass() { } @AfterClass public static void tearDownClass() { } @Before public void setUp() { lieuRepository.removeByLongitude(1000); terrainRepository.removeByNom(NOM_TERRAIN); } @After public void tearDown() { lieuRepository.removeByLongitude(1000); terrainRepository.removeByNom(NOM_TERRAIN); } @Test public void rechercheTerrainTest() { //Given Terrain terrain = new Terrain(NOM_TERRAIN, "", 1, 1); //When terrainRepository.save(terrain); Terrain terrainFromEM = terrainRepository.findByNom(NOM_TERRAIN); //Then assertThat(terrainFromEM).isNotNull(); } @Test public void creationLieuAvecTerrainTest() { Terrain terrain = new Terrain(NOM_TERRAIN, "pour test", 1, 1); terrainRepository.save(terrain); Lieu lieu = new Lieu(1000, 1000, terrain); lieu.setTerrainId(terrain); lieuRepository.save(lieu); Assert.assertNotNull(lieu); assertThat("some text").isNotEmpty().startsWith("some").hasSize(9); assertEquals(String.format("La production d'eau aurait d etre 1 au lieu de %s.", terrain.getProdEau()), 1, terrain.getProdEau()); } }