Skip to content

Instantly share code, notes, and snippets.

@lhauspie
Created August 22, 2017 21:54
Show Gist options
  • Save lhauspie/86134a1d37f26f5589318a1c3aa2a76c to your computer and use it in GitHub Desktop.
Save lhauspie/86134a1d37f26f5589318a1c3aa2a76c to your computer and use it in GitHub Desktop.
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
public void searchProductTest() {
// définition du comportement de la couche SGBD
Query query = new Query();
query.addCriteria(Criteria.where("code").regex("MON_CODE"));
Mockito.when(mongoTemplate.find(Matchers.eq(query), Matchers.eq(Product.class)))
.then(ignoredInvocation -> Arrays.asList(new Product("ID", "CODE", "LABEL", "DESCRIPTION", 1234D)));
// appel de la couche Repository à tester
SearchQuery searchQquery = new SearchQuery("MON_CODE", null, null, null, null);
Collection<Product> products = productRepo.search(searchQquery);
// verification du résultat
Assertions.assertThat(products).hasSize(1);
Assertions.assertThat(products).containsExactly(new Product("ID", "CODE", "LABEL", "DESCRIPTION", 1234D));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment