From 9cd66f68249050c569001550b96bd8ae04bf3b07 Mon Sep 17 00:00:00 2001 From: "Simanta.Sarma" Date: Thu, 3 Nov 2022 10:37:07 +0530 Subject: [PATCH 1/2] Updated to Java 17, Spring Boot 2.7.2, and JUnit 5 --- pom.xml | 8 +++-- src/main/resources/application-dev.yml | 3 +- .../Spring5RecipeAppApplicationTests.java | 5 ++-- .../controllers/ImageControllerTest.java | 10 +++---- .../controllers/IndexControllerTest.java | 10 +++---- .../controllers/IngredientControllerTest.java | 8 ++--- .../controllers/RecipeControllerTest.java | 6 ++-- .../CategoryCommandToCategoryTest.java | 10 +++---- .../CategoryToCategoryCommandTest.java | 10 +++---- .../IngredientCommandToIngredientTest.java | 12 ++++---- .../IngredientToIngredientCommandTest.java | 12 ++++---- .../converters/NotesCommandToNotesTest.java | 10 +++---- .../converters/NotesToNotesCommandTest.java | 10 +++---- .../converters/RecipeCommandToRecipeTest.java | 8 ++--- .../converters/RecipeToRecipeCommandTest.java | 8 ++--- ...itOfMeasureCommandToUnitOfMeasureTest.java | 10 +++---- ...itOfMeasureToUnitOfMeasureCommandTest.java | 10 +++---- .../springframework/domain/CategoryTest.java | 8 ++--- .../UnitOfMeasureRepositoryIT.java | 11 ++++--- .../services/ImageServiceImplTest.java | 11 +++---- .../services/IngredientServiceImplTest.java | 10 +++---- .../services/RecipeServiceIT.java | 7 +---- .../services/RecipeServiceImplTest.java | 30 ++++++++++--------- .../UnitOfMeasureServiceImplTest.java | 8 ++--- 24 files changed, 118 insertions(+), 117 deletions(-) diff --git a/pom.xml b/pom.xml index da0c4a8..1e48467 100755 --- a/pom.xml +++ b/pom.xml @@ -14,17 +14,21 @@ org.springframework.boot spring-boot-starter-parent - 2.1.0.RELEASE + 2.7.2 UTF-8 UTF-8 - 1.8 + 17 + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-data-jpa diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 57a9a01..5c4073f 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -9,6 +9,7 @@ spring: database-platform: org.hibernate.dialect.MySQL5InnoDBDialect database: mysql show-sql: true + defer-datasource-initialization: true # properties: # javax: # persistence: @@ -16,4 +17,4 @@ spring: # create-source: metadata # scripts: # action: create -# create-target: guru_database_create.sql \ No newline at end of file +# create-target: guru_database_create.sql diff --git a/src/test/java/guru/springframework/Spring5RecipeAppApplicationTests.java b/src/test/java/guru/springframework/Spring5RecipeAppApplicationTests.java index 775dc5f..ecdca7f 100755 --- a/src/test/java/guru/springframework/Spring5RecipeAppApplicationTests.java +++ b/src/test/java/guru/springframework/Spring5RecipeAppApplicationTests.java @@ -1,11 +1,10 @@ package guru.springframework; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; -@RunWith(SpringRunner.class) + @SpringBootTest public class Spring5RecipeAppApplicationTests { diff --git a/src/test/java/guru/springframework/controllers/ImageControllerTest.java b/src/test/java/guru/springframework/controllers/ImageControllerTest.java index f745e6a..6b6be37 100755 --- a/src/test/java/guru/springframework/controllers/ImageControllerTest.java +++ b/src/test/java/guru/springframework/controllers/ImageControllerTest.java @@ -3,8 +3,8 @@ import guru.springframework.commands.RecipeCommand; import guru.springframework.services.ImageService; import guru.springframework.services.RecipeService; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.mock.web.MockHttpServletResponse; @@ -12,7 +12,7 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart; @@ -30,9 +30,9 @@ public class ImageControllerTest { MockMvc mockMvc; - @Before + @BeforeEach public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); controller = new ImageController(imageService, recipeService); mockMvc = MockMvcBuilders.standaloneSetup(controller) diff --git a/src/test/java/guru/springframework/controllers/IndexControllerTest.java b/src/test/java/guru/springframework/controllers/IndexControllerTest.java index 8f31456..b86ee4c 100755 --- a/src/test/java/guru/springframework/controllers/IndexControllerTest.java +++ b/src/test/java/guru/springframework/controllers/IndexControllerTest.java @@ -2,8 +2,8 @@ import guru.springframework.domain.Recipe; import guru.springframework.services.RecipeService; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -14,7 +14,7 @@ import java.util.HashSet; import java.util.Set; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -33,9 +33,9 @@ public class IndexControllerTest { IndexController controller; - @Before + @BeforeEach public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); controller = new IndexController(recipeService); } diff --git a/src/test/java/guru/springframework/controllers/IngredientControllerTest.java b/src/test/java/guru/springframework/controllers/IngredientControllerTest.java index 8a82548..a410dab 100755 --- a/src/test/java/guru/springframework/controllers/IngredientControllerTest.java +++ b/src/test/java/guru/springframework/controllers/IngredientControllerTest.java @@ -5,8 +5,8 @@ import guru.springframework.services.IngredientService; import guru.springframework.services.RecipeService; import guru.springframework.services.UnitOfMeasureService; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.http.MediaType; @@ -36,9 +36,9 @@ public class IngredientControllerTest { MockMvc mockMvc; - @Before + @BeforeEach public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); controller = new IngredientController(ingredientService, recipeService, unitOfMeasureService); mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); diff --git a/src/test/java/guru/springframework/controllers/RecipeControllerTest.java b/src/test/java/guru/springframework/controllers/RecipeControllerTest.java index 56c0d87..9aed39d 100755 --- a/src/test/java/guru/springframework/controllers/RecipeControllerTest.java +++ b/src/test/java/guru/springframework/controllers/RecipeControllerTest.java @@ -4,8 +4,8 @@ import guru.springframework.domain.Recipe; import guru.springframework.exceptions.NotFoundException; import guru.springframework.services.RecipeService; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.http.MediaType; @@ -31,7 +31,7 @@ public class RecipeControllerTest { MockMvc mockMvc; - @Before + @BeforeEach public void setUp() throws Exception { MockitoAnnotations.initMocks(this); diff --git a/src/test/java/guru/springframework/converters/CategoryCommandToCategoryTest.java b/src/test/java/guru/springframework/converters/CategoryCommandToCategoryTest.java index cc7298e..546c778 100755 --- a/src/test/java/guru/springframework/converters/CategoryCommandToCategoryTest.java +++ b/src/test/java/guru/springframework/converters/CategoryCommandToCategoryTest.java @@ -2,18 +2,18 @@ import guru.springframework.commands.CategoryCommand; import guru.springframework.domain.Category; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class CategoryCommandToCategoryTest { - public static final Long ID_VALUE = new Long(1L); + public static final Long ID_VALUE = Long.valueOf(1L); public static final String DESCRIPTION = "description"; CategoryCommandToCategory conveter; - @Before + @BeforeEach public void setUp() throws Exception { conveter = new CategoryCommandToCategory(); } diff --git a/src/test/java/guru/springframework/converters/CategoryToCategoryCommandTest.java b/src/test/java/guru/springframework/converters/CategoryToCategoryCommandTest.java index 1cf4445..b838d1f 100755 --- a/src/test/java/guru/springframework/converters/CategoryToCategoryCommandTest.java +++ b/src/test/java/guru/springframework/converters/CategoryToCategoryCommandTest.java @@ -2,21 +2,21 @@ import guru.springframework.commands.CategoryCommand; import guru.springframework.domain.Category; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; /** * Created by jt on 6/21/17. */ public class CategoryToCategoryCommandTest { - public static final Long ID_VALUE = new Long(1L); + public static final Long ID_VALUE = Long.valueOf(1L); public static final String DESCRIPTION = "descript"; CategoryToCategoryCommand convter; - @Before + @BeforeEach public void setUp() throws Exception { convter = new CategoryToCategoryCommand(); } diff --git a/src/test/java/guru/springframework/converters/IngredientCommandToIngredientTest.java b/src/test/java/guru/springframework/converters/IngredientCommandToIngredientTest.java index ec7c275..95cc00a 100755 --- a/src/test/java/guru/springframework/converters/IngredientCommandToIngredientTest.java +++ b/src/test/java/guru/springframework/converters/IngredientCommandToIngredientTest.java @@ -4,24 +4,24 @@ import guru.springframework.commands.UnitOfMeasureCommand; import guru.springframework.domain.Ingredient; import guru.springframework.domain.Recipe; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.math.BigDecimal; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class IngredientCommandToIngredientTest { public static final Recipe RECIPE = new Recipe(); public static final BigDecimal AMOUNT = new BigDecimal("1"); public static final String DESCRIPTION = "Cheeseburger"; - public static final Long ID_VALUE = new Long(1L); - public static final Long UOM_ID = new Long(2L); + public static final Long ID_VALUE = Long.valueOf(1L); + public static final Long UOM_ID = Long.valueOf(2L); IngredientCommandToIngredient converter; - @Before + @BeforeEach public void setUp() throws Exception { converter = new IngredientCommandToIngredient(new UnitOfMeasureCommandToUnitOfMeasure()); } diff --git a/src/test/java/guru/springframework/converters/IngredientToIngredientCommandTest.java b/src/test/java/guru/springframework/converters/IngredientToIngredientCommandTest.java index 40601af..f076c51 100755 --- a/src/test/java/guru/springframework/converters/IngredientToIngredientCommandTest.java +++ b/src/test/java/guru/springframework/converters/IngredientToIngredientCommandTest.java @@ -4,12 +4,12 @@ import guru.springframework.domain.Ingredient; import guru.springframework.domain.Recipe; import guru.springframework.domain.UnitOfMeasure; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.math.BigDecimal; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; /** * Created by jt on 6/21/17. @@ -19,13 +19,13 @@ public class IngredientToIngredientCommandTest { public static final Recipe RECIPE = new Recipe(); public static final BigDecimal AMOUNT = new BigDecimal("1"); public static final String DESCRIPTION = "Cheeseburger"; - public static final Long UOM_ID = new Long(2L); - public static final Long ID_VALUE = new Long(1L); + public static final Long UOM_ID = Long.valueOf(2L); + public static final Long ID_VALUE = Long.valueOf(1L); IngredientToIngredientCommand converter; - @Before + @BeforeEach public void setUp() throws Exception { converter = new IngredientToIngredientCommand(new UnitOfMeasureToUnitOfMeasureCommand()); } diff --git a/src/test/java/guru/springframework/converters/NotesCommandToNotesTest.java b/src/test/java/guru/springframework/converters/NotesCommandToNotesTest.java index 855f02e..67bef39 100755 --- a/src/test/java/guru/springframework/converters/NotesCommandToNotesTest.java +++ b/src/test/java/guru/springframework/converters/NotesCommandToNotesTest.java @@ -2,18 +2,18 @@ import guru.springframework.commands.NotesCommand; import guru.springframework.domain.Notes; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class NotesCommandToNotesTest { - public static final Long ID_VALUE = new Long(1L); + public static final Long ID_VALUE = Long.valueOf(1L); public static final String RECIPE_NOTES = "Notes"; NotesCommandToNotes converter; - @Before + @BeforeEach public void setUp() throws Exception { converter = new NotesCommandToNotes(); diff --git a/src/test/java/guru/springframework/converters/NotesToNotesCommandTest.java b/src/test/java/guru/springframework/converters/NotesToNotesCommandTest.java index bac0ac0..0170f41 100755 --- a/src/test/java/guru/springframework/converters/NotesToNotesCommandTest.java +++ b/src/test/java/guru/springframework/converters/NotesToNotesCommandTest.java @@ -2,21 +2,21 @@ import guru.springframework.commands.NotesCommand; import guru.springframework.domain.Notes; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; /** * Created by jt on 6/21/17. */ public class NotesToNotesCommandTest { - public static final Long ID_VALUE = new Long(1L); + public static final Long ID_VALUE = Long.valueOf(1L); public static final String RECIPE_NOTES = "Notes"; NotesToNotesCommand converter; - @Before + @BeforeEach public void setUp() throws Exception { converter = new NotesToNotesCommand(); } diff --git a/src/test/java/guru/springframework/converters/RecipeCommandToRecipeTest.java b/src/test/java/guru/springframework/converters/RecipeCommandToRecipeTest.java index bbacedb..5259dfb 100755 --- a/src/test/java/guru/springframework/converters/RecipeCommandToRecipeTest.java +++ b/src/test/java/guru/springframework/converters/RecipeCommandToRecipeTest.java @@ -6,10 +6,10 @@ import guru.springframework.commands.RecipeCommand; import guru.springframework.domain.Difficulty; import guru.springframework.domain.Recipe; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class RecipeCommandToRecipeTest { public static final Long RECIPE_ID = 1L; @@ -30,7 +30,7 @@ public class RecipeCommandToRecipeTest { RecipeCommandToRecipe converter; - @Before + @BeforeEach public void setUp() throws Exception { converter = new RecipeCommandToRecipe(new CategoryCommandToCategory(), new IngredientCommandToIngredient(new UnitOfMeasureCommandToUnitOfMeasure()), diff --git a/src/test/java/guru/springframework/converters/RecipeToRecipeCommandTest.java b/src/test/java/guru/springframework/converters/RecipeToRecipeCommandTest.java index 9bf0d75..c3c74aa 100755 --- a/src/test/java/guru/springframework/converters/RecipeToRecipeCommandTest.java +++ b/src/test/java/guru/springframework/converters/RecipeToRecipeCommandTest.java @@ -2,10 +2,10 @@ import guru.springframework.commands.RecipeCommand; import guru.springframework.domain.*; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class RecipeToRecipeCommandTest { @@ -25,7 +25,7 @@ public class RecipeToRecipeCommandTest { public static final Long NOTES_ID = 9L; RecipeToRecipeCommand converter; - @Before + @BeforeEach public void setUp() throws Exception { converter = new RecipeToRecipeCommand( new CategoryToCategoryCommand(), diff --git a/src/test/java/guru/springframework/converters/UnitOfMeasureCommandToUnitOfMeasureTest.java b/src/test/java/guru/springframework/converters/UnitOfMeasureCommandToUnitOfMeasureTest.java index 820f5d5..8e66129 100755 --- a/src/test/java/guru/springframework/converters/UnitOfMeasureCommandToUnitOfMeasureTest.java +++ b/src/test/java/guru/springframework/converters/UnitOfMeasureCommandToUnitOfMeasureTest.java @@ -2,19 +2,19 @@ import guru.springframework.commands.UnitOfMeasureCommand; import guru.springframework.domain.UnitOfMeasure; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class UnitOfMeasureCommandToUnitOfMeasureTest { public static final String DESCRIPTION = "description"; - public static final Long LONG_VALUE = new Long(1L); + public static final Long LONG_VALUE = Long.valueOf(1L); UnitOfMeasureCommandToUnitOfMeasure converter; - @Before + @BeforeEach public void setUp() throws Exception { converter = new UnitOfMeasureCommandToUnitOfMeasure(); diff --git a/src/test/java/guru/springframework/converters/UnitOfMeasureToUnitOfMeasureCommandTest.java b/src/test/java/guru/springframework/converters/UnitOfMeasureToUnitOfMeasureCommandTest.java index 68a3257..cea508a 100755 --- a/src/test/java/guru/springframework/converters/UnitOfMeasureToUnitOfMeasureCommandTest.java +++ b/src/test/java/guru/springframework/converters/UnitOfMeasureToUnitOfMeasureCommandTest.java @@ -2,10 +2,10 @@ import guru.springframework.commands.UnitOfMeasureCommand; import guru.springframework.domain.UnitOfMeasure; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; /** * Created by jt on 6/21/17. @@ -13,11 +13,11 @@ public class UnitOfMeasureToUnitOfMeasureCommandTest { public static final String DESCRIPTION = "description"; - public static final Long LONG_VALUE = new Long(1L); + public static final Long LONG_VALUE = Long.valueOf(1L); UnitOfMeasureToUnitOfMeasureCommand converter; - @Before + @BeforeEach public void setUp() throws Exception { converter = new UnitOfMeasureToUnitOfMeasureCommand(); } diff --git a/src/test/java/guru/springframework/domain/CategoryTest.java b/src/test/java/guru/springframework/domain/CategoryTest.java index 11badc4..c10b9d5 100755 --- a/src/test/java/guru/springframework/domain/CategoryTest.java +++ b/src/test/java/guru/springframework/domain/CategoryTest.java @@ -1,9 +1,9 @@ package guru.springframework.domain; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Created by jt on 6/17/17. @@ -12,7 +12,7 @@ public class CategoryTest { Category category; - @Before + @BeforeEach public void setUp(){ category = new Category(); } diff --git a/src/test/java/guru/springframework/repositories/UnitOfMeasureRepositoryIT.java b/src/test/java/guru/springframework/repositories/UnitOfMeasureRepositoryIT.java index b934959..268ca0e 100755 --- a/src/test/java/guru/springframework/repositories/UnitOfMeasureRepositoryIT.java +++ b/src/test/java/guru/springframework/repositories/UnitOfMeasureRepositoryIT.java @@ -1,28 +1,27 @@ package guru.springframework.repositories; import guru.springframework.domain.UnitOfMeasure; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; 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.util.Optional; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Created by jt on 6/17/17. */ -@RunWith(SpringRunner.class) + @DataJpaTest public class UnitOfMeasureRepositoryIT { @Autowired UnitOfMeasureRepository unitOfMeasureRepository; - @Before + @BeforeEach public void setUp() throws Exception { } diff --git a/src/test/java/guru/springframework/services/ImageServiceImplTest.java b/src/test/java/guru/springframework/services/ImageServiceImplTest.java index a8e5c90..8b3c28c 100755 --- a/src/test/java/guru/springframework/services/ImageServiceImplTest.java +++ b/src/test/java/guru/springframework/services/ImageServiceImplTest.java @@ -2,8 +2,9 @@ import guru.springframework.domain.Recipe; import guru.springframework.repositories.RecipeRepository; -import org.junit.Before; -import org.junit.Test; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -12,7 +13,7 @@ import java.util.Optional; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; @@ -23,9 +24,9 @@ public class ImageServiceImplTest { ImageService imageService; - @Before + @BeforeEach public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); imageService = new ImageServiceImpl(recipeRepository); } diff --git a/src/test/java/guru/springframework/services/IngredientServiceImplTest.java b/src/test/java/guru/springframework/services/IngredientServiceImplTest.java index 49d53b1..4ebcade 100755 --- a/src/test/java/guru/springframework/services/IngredientServiceImplTest.java +++ b/src/test/java/guru/springframework/services/IngredientServiceImplTest.java @@ -9,14 +9,14 @@ import guru.springframework.domain.Recipe; import guru.springframework.repositories.RecipeRepository; import guru.springframework.repositories.UnitOfMeasureRepository; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import java.util.Optional; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.*; @@ -39,9 +39,9 @@ public IngredientServiceImplTest() { this.ingredientCommandToIngredient = new IngredientCommandToIngredient(new UnitOfMeasureCommandToUnitOfMeasure()); } - @Before + @BeforeEach public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); ingredientService = new IngredientServiceImpl(ingredientToIngredientCommand, ingredientCommandToIngredient, recipeRepository, unitOfMeasureRepository); diff --git a/src/test/java/guru/springframework/services/RecipeServiceIT.java b/src/test/java/guru/springframework/services/RecipeServiceIT.java index f8702b9..7255e9f 100755 --- a/src/test/java/guru/springframework/services/RecipeServiceIT.java +++ b/src/test/java/guru/springframework/services/RecipeServiceIT.java @@ -5,20 +5,16 @@ import guru.springframework.converters.RecipeToRecipeCommand; import guru.springframework.domain.Recipe; import guru.springframework.repositories.RecipeRepository; -import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; -import static org.junit.Assert.assertEquals; - +import static org.junit.jupiter.api.Assertions.assertEquals; /** * Created by jt on 6/21/17. */ -@RunWith(SpringRunner.class) @SpringBootTest public class RecipeServiceIT { @@ -37,7 +33,6 @@ public class RecipeServiceIT { RecipeToRecipeCommand recipeToRecipeCommand; @Transactional - @Test public void testSaveOfDescription() throws Exception { //given Iterable recipes = recipeRepository.findAll(); diff --git a/src/test/java/guru/springframework/services/RecipeServiceImplTest.java b/src/test/java/guru/springframework/services/RecipeServiceImplTest.java index d16897c..0059377 100755 --- a/src/test/java/guru/springframework/services/RecipeServiceImplTest.java +++ b/src/test/java/guru/springframework/services/RecipeServiceImplTest.java @@ -7,8 +7,8 @@ import guru.springframework.domain.Recipe; import guru.springframework.exceptions.NotFoundException; import guru.springframework.repositories.RecipeRepository; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -16,9 +16,11 @@ import java.util.Optional; import java.util.Set; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; +import static org.springframework.test.util.AssertionErrors.assertNotNull; /** * Created by jt on 6/17/17. @@ -36,9 +38,9 @@ public class RecipeServiceImplTest { @Mock RecipeCommandToRecipe recipeCommandToRecipe; - @Before + @BeforeEach public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); + MockitoAnnotations.openMocks(this); recipeService = new RecipeServiceImpl(recipeRepository, recipeCommandToRecipe, recipeToRecipeCommand); } @@ -58,16 +60,16 @@ public void getRecipeByIdTest() throws Exception { verify(recipeRepository, never()).findAll(); } - @Test(expected = NotFoundException.class) + @Test public void getRecipeByIdTestNotFound() throws Exception { - Optional recipeOptional = Optional.empty(); - - when(recipeRepository.findById(anyLong())).thenReturn(recipeOptional); - - Recipe recipeReturned = recipeService.findById(1L); - - //should go boom + try{ + Optional recipeOptional = Optional.empty(); + when(recipeRepository.findById(anyLong())).thenReturn(recipeOptional); + Recipe recipeReturned = recipeService.findById(1L); + }catch(Exception e){ + assertThat(e, instanceOf(NotFoundException.class)); + } } @Test diff --git a/src/test/java/guru/springframework/services/UnitOfMeasureServiceImplTest.java b/src/test/java/guru/springframework/services/UnitOfMeasureServiceImplTest.java index d055bd5..8b5696e 100755 --- a/src/test/java/guru/springframework/services/UnitOfMeasureServiceImplTest.java +++ b/src/test/java/guru/springframework/services/UnitOfMeasureServiceImplTest.java @@ -4,15 +4,15 @@ import guru.springframework.converters.UnitOfMeasureToUnitOfMeasureCommand; import guru.springframework.domain.UnitOfMeasure; import guru.springframework.repositories.UnitOfMeasureRepository; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import java.util.HashSet; import java.util.Set; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; public class UnitOfMeasureServiceImplTest { @@ -23,7 +23,7 @@ public class UnitOfMeasureServiceImplTest { @Mock UnitOfMeasureRepository unitOfMeasureRepository; - @Before + @BeforeEach public void setUp() throws Exception { MockitoAnnotations.initMocks(this); From d408d23e9db7d8a78e0df78d35203dbf4d26e9ae Mon Sep 17 00:00:00 2001 From: "Simanta.Sarma" Date: Thu, 3 Nov 2022 10:46:49 +0530 Subject: [PATCH 2/2] Updated to Java 17, Spring Boot 2.7.2, and JUnit 5 --- src/main/resources/application-prod.yml | 1 + src/main/resources/application.properties | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 70e95c7..76c915d 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -9,3 +9,4 @@ spring: database-platform: org.hibernate.dialect.MySQL5InnoDBDialect database: mysql show-sql: false + defer-datasource-initialization: true diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e45c501..7d80715 100755 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,4 @@ -logging.level.guru.springframework=debug \ No newline at end of file +logging.level.guru.springframework=debug +spring.h2.console.enabled=true +spring.datasource.url=jdbc:h2:mem:mydb +spring.jpa.defer-datasource-initialization=true \ No newline at end of file