From c4094c0ad8b0635f16c9dd2bb749da42616d9c0d Mon Sep 17 00:00:00 2001 From: Alexander Basov Date: Mon, 27 Apr 2015 17:53:15 +0300 Subject: [PATCH] Mocked EmailService in EmailVerificationServiceTest --- pom.xml | 5 + .../org/genesys2/unit/ContentServiceTest.java | 93 ++++++++++++++++++- .../unit/EmailVerificationServiceTest.java | 11 ++- 3 files changed, 105 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index e72d9e007..feb6cd2d3 100644 --- a/pom.xml +++ b/pom.xml @@ -479,6 +479,11 @@ pgr-ontology-model 0.7.1-SNAPSHOT + + org.mockito + mockito-all + 2.0.2-beta + diff --git a/src/test/java/org/genesys2/unit/ContentServiceTest.java b/src/test/java/org/genesys2/unit/ContentServiceTest.java index e208b617d..d6ce762f9 100644 --- a/src/test/java/org/genesys2/unit/ContentServiceTest.java +++ b/src/test/java/org/genesys2/unit/ContentServiceTest.java @@ -3,7 +3,10 @@ package org.genesys2.unit; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.velocity.app.VelocityEngine; +import org.genesys2.server.model.impl.ActivityPost; +import org.genesys2.server.model.impl.Article; import org.genesys2.server.persistence.domain.ActivityPostRepository; +import org.genesys2.server.persistence.domain.ArticleRepository; import org.genesys2.server.service.ContentService; import org.genesys2.server.service.HtmlSanitizer; import org.genesys2.server.service.impl.ContentServiceImpl; @@ -11,6 +14,7 @@ import org.genesys2.server.service.impl.OWASPSanitizer; import org.genesys2.server.test.JpaDataConfig; import org.genesys2.server.test.PropertyPlacholderInitializer; import org.genesys2.spring.config.HazelcastConfig; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -20,12 +24,19 @@ import org.springframework.cache.support.NoOpCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; +import org.springframework.data.domain.PageRequest; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import java.util.ArrayList; +import java.util.List; +import java.util.Locale; + import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = ContentServiceTest.Config.class, initializers = PropertyPlacholderInitializer.class) @@ -66,11 +77,32 @@ public class ContentServiceTest { @Autowired private ActivityPostRepository postRepository; + @Autowired + private ArticleRepository articleRepository; + @Before public void setUp() { - + //create activity post for test contentService.createActivityPost("title of activity post", "body of activity post"); + //create article for test + List
articleList = new ArrayList<>(); + Article article = new Article(); + article.setLang("en"); + article.setSlug("smtp.email-verification"); + article.setClassPk(contentService.ensureClassPK(Article.class)); + articleList.add(article); + contentService.save(articleList); + + } + + @After + public void teardown() { + //delete activity post + contentService.deleteActivityPost(contentService.lastNews().get(0).getId()); + + //delete articles + articleRepository.deleteAll(); } @Test @@ -86,9 +118,66 @@ public class ContentServiceTest { public void lastNewsTest() { LOG.info("Start test-method lastNewsTest"); - contentService.lastNews(); + List postList = contentService.lastNews(); + + assertTrue(postList.size() == 1); + assertTrue(postList.get(0).getTitle().equals("title of activity post")); + assertTrue(postList.get(0).getBody().equals("body of activity post")); LOG.info("Test lastNewsTest is passed!"); } + @Test + public void listArticlesTest() { + LOG.info("Start test-method listArticlesTest"); + + List
articleList = contentService.listArticles(new PageRequest(0, 6)).getContent(); + assertNotNull("ArticleList must be not null", articleList); + assertTrue(!articleList.isEmpty()); + + LOG.info("Test listArticlesTest is passed!"); + } + + @Test + public void listArticlesByLangTest(){ + LOG.info("Start test-method listArticlesByLangTest"); + + List
articleList = contentService.listArticlesByLang("ru", new PageRequest(0, 6)).getContent(); + assertTrue(articleList.isEmpty()); + + articleList = contentService.listArticlesByLang("en", new PageRequest(0, 6)).getContent(); + assertTrue(!articleList.isEmpty()); + + LOG.info("Test listArticlesByLangTest is passed!"); + } + + @Test + public void saveTest(){ + LOG.info("Start test-method saveTest"); + + List
articleList = contentService.listArticles(new PageRequest(0, 6)).getContent(); + assertTrue(articleList.size() == 1); + + List
articleListForSave = new ArrayList<>(); + Article article = new Article(); + article.setLang("en"); + article.setSlug("smtp.email-verification"); + article.setClassPk(contentService.ensureClassPK(Article.class)); + articleListForSave.add(article); + contentService.save(articleListForSave); + + assertTrue(contentService.listArticles(new PageRequest(0, 6)).getContent().size() == 2); + + LOG.info("Test saveTest is passed!"); + } + + @Test + public void getGlobalArticleTets(){ + LOG.info("Start test-method getGlobalArticleTets"); + + assertNotNull(contentService.getGlobalArticle("smtp.email-verification", Locale.ENGLISH)); + + LOG.info("Test getGlobalArticleTets is passed!"); + } + } diff --git a/src/test/java/org/genesys2/unit/EmailVerificationServiceTest.java b/src/test/java/org/genesys2/unit/EmailVerificationServiceTest.java index 3e332db4a..eafbe3ccf 100644 --- a/src/test/java/org/genesys2/unit/EmailVerificationServiceTest.java +++ b/src/test/java/org/genesys2/unit/EmailVerificationServiceTest.java @@ -21,6 +21,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.CacheManager; import org.springframework.cache.support.NoOpCacheManager; @@ -64,7 +65,7 @@ public class EmailVerificationServiceTest { @Bean public EMailService eMailService() { - return new EMailServiceImpl(); + return Mockito.mock(EMailServiceImpl.class); } @Bean @@ -108,6 +109,9 @@ public class EmailVerificationServiceTest { } } + @Autowired + private EMailService eMailService; + @Autowired private VerificationTokenRepository verificationTokenRepository; @@ -197,8 +201,9 @@ public class EmailVerificationServiceTest { public void sendVerificationEmailTest() { LOG.info("Start test-method sendVerificationEmailTest"); + Mockito.doNothing().when(eMailService).sendMail("title for oneArticle", "

Tho BOdy of oneArticleTho BOdy of oneArticleTho BOdy of twoArticleTho BOdy of twoArticle