Skip to content

Instantly share code, notes, and snippets.

View lhauspie's full-sized avatar

Logan HAUSPIE lhauspie

View GitHub Profile
@lhauspie
lhauspie / pom.xml
Created August 22, 2017 20:20
pom.xml file of a Spring Boot application with MongoDB
<?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>com.github.lhauspie</groupId>
<artifactId>mongodb-junit-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
@lhauspie
lhauspie / ProductRepositoryImpl.java
Last active August 22, 2017 21:17
implémentation des fonctionnalités custom sur les produits
public class ProductRepositoryImpl implements ProductCustomRepository {
public static final String CODE = "code";
public static final String LABEL = "label";
public static final String DESCRIPTION = "description";
public static final String PRICE = "price";
@Autowired
private MongoTemplate mongoTemplate;
@lhauspie
lhauspie / ProductRepository.java
Created August 22, 2017 21:18
Interface Spring Data Repository contenant les fonctionnalités de CRUD sur les produits
public interface ProductRepository extends MongoRepository<Product, String>, ProductCustomRepository {
Product findForUpdateById(String id);
}
@lhauspie
lhauspie / ProductCustomRepository.java
Created August 22, 2017 21:20
Interface Spring Data Custom Repository contenant les fonctionnalités custom sur les produits
public interface ProductCustomRepository {
Collection<Product> search(SearchQuery query);
}
@lhauspie
lhauspie / ProductRepositoryUnitTest.java
Created August 22, 2017 21:54
Premiere tentative de test unitaire de la couche Repository
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProductRepositoryUnitTest {
@Autowired
private ProductRepository productRepo;
@MockBean
private MongoTemplate mongoTemplate;
@Test
@lhauspie
lhauspie / ProductRepositoryUnitTest.log
Created August 22, 2017 21:59
Trace de log de la premiere tentative de test unitaire de la couche Repository
Caused by: java.lang.NullPointerException
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.<init>(MongoRepositoryFactory.java:73)
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean.getFactoryInstance(MongoRepositoryFactoryBean.java:104)
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean.createRepositoryFactory(MongoRepositoryFactoryBean.java:88)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:248)
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean.afterPropertiesSet(MongoRepositoryFactoryBean.java:117)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 93 more
@lhauspie
lhauspie / ProductRepositoryUnitTest.java
Created August 22, 2017 22:26
Deuxieme tentative de test unitaire de la couche Repository
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProductRepositoryUnitTest {
@MockBean
private MongoConverter mongoConverter;
[...]
@Test
public void searchProductTest() {
@lhauspie
lhauspie / MockedMongoConfig.java
Created August 22, 2017 22:45
Définition d'un configuration de test spécifique pour Mocker le MongoTemplate
@TestConfiguration
public class MockedMongoConfig {
@Bean
public MongoTemplate mongoTemplate() {
MongoMappingContext mappingContext = new MongoMappingContext();
MongoConverter mongoConverter = Mockito.mock(MongoConverter.class);
Mockito.when(mongoConverter.getMappingContext()).then(ignoredInvocation -> mappingContext);
@lhauspie
lhauspie / ProductRepositoryIntegrationTest.java
Created August 22, 2017 23:01
Test d’intégration du Custom Repository produit
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProductRepositoryIntegrationTest {
@Autowired
private ProductRepository productRepo;
@Autowired
private MongoTemplate mongoTemplate;
@Test
@lhauspie
lhauspie / ProductRepositoryIntegrationTest.log
Created August 22, 2017 23:13
Echec double lancement test d'intégration
ava.lang.AssertionError:
Expected size:<0> but was:<1> in:
<[Product(id=599cb8cde50e2262d67575c1, code=CODE, label=LABEL, description=DESCRIPTION, price=1234.0)]>
at com.github.lhauspie.mongodb.junit.demo.repository.ProductRepositoryIntegrationTest.searchProductTest(ProductRepositoryIntegrationTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
...