Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Spring Lab Manual

Spring Lab Manual

Published by Teamlease Edtech Ltd (Amita Chitroda), 2022-08-30 11:43:05

Description: Spring Lab Manual

Search

Read the Text Version

JAVA Spring boot FULL STACK DEVELOPMENT

Objectives of this Lab Manual This lab manual includes - Exercises for hands on practice Guidelines to complete the exercise Learning Outcome

How to install Spring?

Problem What will you do? Statement Create a Spring Boot Project with IntelliJ IDEA

Solution What will be the steps Steps to get the output? • Step-1: Install IntelliJ IDEA on the local machine via this link() • Step-2 : Create a Spring Boot Project in Spring Initializr

Solution What will be the steps Steps to get the output? Step-3 : Import Spring Boot Project in IntelliJ IDEA. After successfully installing IntelliJ IDEA go to the File > Open as seen in the below image.

Solution What will be the steps Steps to get the output? Step 4: Here you have to choose the project that you have created in step 2.

Learning Outcome After performing this Add teexxt ehrecries:e- You’ll be able to install Spring boot and create a project

Write a unit test case by using Mockito and Junit

Problem What will you do? Statement Write a code of spring boot using Junit and Mockito.

Submission What will be the steps Guidelines to get the output? • Step-1: Refer to the lab exercise “How to Create a Spring Boot Project with IntelliJ IDEA” and create a Spring Boot project. • Step 2: Include the following dependency, which is stated below:\\ a. Spring Web b. MySQL Database c. Lombok d. Spring Data JPA

Submission What will be the steps Guidelines to get the output? Step 3: Produce the files and packages as shown in the image below. The whole file structure for this project is shown below.

Submission What will be the steps Guidelines to get the output? Step-4: Within the entity package. The process involves writing a straightforward POJO class inside the Person.java file. Step-5: Create a simple interface and name the interface as PersonRepo. This interface is going to extend the JpaRepository. Step-6: Inside the package create one class named as PersonService. Step-7: Inside the package create one class named as PersonController.

Submission What will be the steps Guidelines to get the output? Step 8: Create the following packages and the classes as shown in the below image. (Inside the green color box)

Submission What will be the steps Guidelines to get the output? Step-9:Inside the test > repo package create one class named as PersonRepoTest. Step-10: Inside the test > services package create one class named as PersonServiceTest.

Code Code for Step-1: <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd\"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.4</version> <relativePath/> <!-- lookup parent from repository -->

Code Code for Step-1: </parent> <groupId>com.demo</groupId> <artifactId>BootDemoApp</artifactId> <version>0.0.1-SNAPSHOT</version> <name>BootDemoApp</name> <description>BootDemoApp</description> <properties> <java.version>16</java.version> </properties>

Code Code for Step-1: <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>

Code Code for Step-1: <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>

Code Code for Step-1: <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId>

Code Code for Step-4 : package com.demo.entites; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.Entity; import javax.persistence.Id; @Entity @Data @NoArgsConstructor @AllArgsConstructor

Code Code for Step-4: public class Person { @Id private Integer personId; private String personName; private String personCity; }

Code Code for Step-5: package com.demo.repo; import com.demo.entites.Person; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; public interface PersonRepo extends JpaRepository<Person, Integer> { @Query( \"SELECT CASE WHEN COUNT(s) > 0 THEN TRUE ELSE FALSE END FROM Person s WHERE s.personId = ?1\") Boolean isPersonExitsById(Integer id);

Code Code for Step-5: public class Person { @Id private Integer personId; private String personName; private String personCity; }

Code Code for Step 6: package com.demo.services; import com.demo.entites.Person; import com.demo.repo.PersonRepo; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PersonService { @Autowired private PersonRepo repo; public List<Person> getAllPerson()

Code Code for Step 6: { return this.repo.findAll(); } public PersonService(PersonRepo repo) { // this keyword refers to current instance this.repo = repo; } }

Code Code for Step-7: package com.demo.controllers; import com.demo.services.PersonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class PersonController { @Autowired private PersonService personService;

Code Code for Step-7: @GetMapping(\"/persons\") public ResponseEntity<?> getAllPersons() { return ResponseEntity.ok(this.personService.getAllPerson()); } }

Code Code for Step-9: package com.demo.repo; import com.demo.entites.Person; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; @SpringBootTest class PersonRepoTest {

Code Code for Step-9: @Autowired private PersonRepo personRepo; @Test void isPersonExitsById() { Person person = new Person(1001, \"Amiya\", \"Bhubaneswar\"); personRepo.save(person); Boolean actualResult = personRepo.isPersonExitsById(1001); assertThat(actualResult).isTrue(); } }

Code Code for Step-10: package com.demo.services; import static org.mockito.Mockito.verify; import com.demo.repo.PersonRepo; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class)

Code Code for Step-10: class PersonServiceTest { @Mock private PersonRepo personRepo; private PersonService personService; @BeforeEach void setUp() { this.personService = new PersonService(this.personRepo); } @Test void getAllPerson() {

Code Code for Step-10: personService.getAllPerson(); verify(personRepo).findAll(); } }

Learning Outcome After performing this Add teexxt ehrecries:e- You will be able to declare a class, its field, and methods successfully.

Happy Learning!


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook