dupirefr/dupire.francois+pro@gmail.com [BAEL-1981] Spring data jpa dates (#4795)
* [BAEL-1981] Article entity and repository + tests * [BAEL-1981] Removing unnecessary fields * [BAEL-1981] spring-data-jpa module creation
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import com.baeldung.domain.Article;
|
||||
import com.baeldung.repository.ArticleRepository;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest
|
||||
public class ArticleRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ArticleRepository repository;
|
||||
|
||||
@Test
|
||||
public void givenImportedArticlesWhenFindAllByPublicationDateThenArticles1And2Returned()
|
||||
throws Exception {
|
||||
List<Article> result = repository.findAllByPublicationDate(
|
||||
new SimpleDateFormat("yyyy-MM-dd").parse("2018-01-01")
|
||||
);
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertTrue(result.stream()
|
||||
.map(Article::getId)
|
||||
.allMatch(id -> Arrays.asList(1, 2).contains(id))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImportedArticlesWhenFindAllByPublicationTimeBetweenThenArticles2And3Returned()
|
||||
throws Exception {
|
||||
List<Article> result = repository.findAllByPublicationTimeBetween(
|
||||
new SimpleDateFormat("HH:mm").parse("15:15"),
|
||||
new SimpleDateFormat("HH:mm").parse("16:30")
|
||||
);
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertTrue(result.stream()
|
||||
.map(Article::getId)
|
||||
.allMatch(id -> Arrays.asList(2, 3).contains(id))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenImportedArticlesWhenFindAllWithCreationDateTimeBeforeThenArticles2And3Returned() throws Exception {
|
||||
List<Article> result = repository.findAllWithCreationDateTimeBefore(
|
||||
new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2017-12-15 10:00")
|
||||
);
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertTrue(result.stream()
|
||||
.map(Article::getId)
|
||||
.allMatch(id -> Arrays.asList(2, 3).contains(id))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user