repository.LieuTest.java Source code

Java tutorial

Introduction

Here is the source code for repository.LieuTest.java

Source

package repository;

/*
 * 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.
 */

import com.chevrier.legiondao.config.ConfigJpa;
import com.chevrier.legiondao.entities.Lieu;
import com.chevrier.legiondao.repository.LieuRepository;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
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;

/**
 *
 * @author nicolas
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ConfigJpa.class)
public class LieuTest {

    @Autowired
    LieuRepository repository;

    public LieuTest() {
    }

    @BeforeClass
    public static void setUpClass() {
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
        System.out.println("Lieu found with findAll():");
        System.out.println("-------------------------------");
        System.out.println(repository.findAll().toString());
    }

    @After
    public void tearDown() {
        repository.removeByLongitude(1000);
    }

    @Test
    public void insererLieu() {
        repository.save(new Lieu(1000, 1000));
        repository.save(new Lieu(1000, 1001));
        List<Lieu> allLieuWithLongitudeForTest = repository.findByLongitude(1000);
        allLieuWithLongitudeForTest.forEach(lieu -> System.out.println(lieu.toString()));
        Integer nbrLieuTest = allLieuWithLongitudeForTest.size();
        assertEquals(String.format("2 lieux auraient d tre cr au lieu de %s.", nbrLieuTest), 2,
                nbrLieuTest);
    }

    @Test
    public void getLieuByLongitudeAndLatitude() {
        repository.save(new Lieu(1000, 1001));
        Lieu lieuWithLongitudeAndLatitudeForTest = repository.findByLongitudeAndLatitude(1000, 1001);

        int longitude = lieuWithLongitudeAndLatitudeForTest.getLongitude();
        int latitude = lieuWithLongitudeAndLatitudeForTest.getLatitude();
        assertEquals(String.format("La longitude aurait d etre 1000 au lieu de %s.", longitude), 1000, longitude);
        assertEquals(String.format("La latitude aurait d etre 1001 au lieu de %s.", latitude), 1001, latitude);

    }

}