This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ProductRepository extends MongoRepository<Product, String>, ProductCustomRepository { | |
Product findForUpdateById(String id); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ProductCustomRepository { | |
Collection<Product> search(SearchQuery query); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SpringBootTest | |
@RunWith(SpringRunner.class) | |
public class ProductRepositoryUnitTest { | |
@Autowired | |
private ProductRepository productRepo; | |
@MockBean | |
private MongoTemplate mongoTemplate; | |
@Test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SpringBootTest | |
@RunWith(SpringRunner.class) | |
public class ProductRepositoryUnitTest { | |
@MockBean | |
private MongoConverter mongoConverter; | |
[...] | |
@Test | |
public void searchProductTest() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SpringBootTest | |
@RunWith(SpringRunner.class) | |
public class ProductRepositoryIntegrationTest { | |
@Autowired | |
private ProductRepository productRepo; | |
@Autowired | |
private MongoTemplate mongoTemplate; | |
@Test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
... |
OlderNewer