banner



How To Use Soap Input And Output In Junit Test Cases To Test Web Service

Before I've published a blog mail service on now to create a RESTful Web Service(JAX-RS) to accept JSON payload with user contour details and how to save user profile details into a MySQL database using Java Hide framework. In this blog post I am going to share with you how to test(using JUnit and Mockito) it's Service Layer Implementation course, which is responsible for storing user profile details. Beneath, is a curt intermission downwards on things we are going to cover:

  • Add Mockito Framework for Unit of measurement tests mockito-core to our project pom.xml file
  • Add Bound Framework spring-test and jump-context dependencies
  • Create Bound ContextConfiguration Java course to specify base packages for component scanning
  • Create UsersServiceImplTest grade, Autowire service class, mock objects, phone call saveUser method and Assert expected results.

Ok, let's begin.

Add together mockito-core, spring-test and spring-context  to pom.xml

Open https://mvnrepository.com and search each of the beneath libraries Maven repository. Below are the ones I have copied into my pom.xml:

<dependency>     <groupId>org.mockito</groupId>     <artifactId>mockito-core</artifactId>     <version>2.eight.47</version>     <telescopic>test</scope> </dependency> <dependency>     <groupId>org.springframework</groupId>     <artifactId>jump-test</artifactId>     <version>iv.3.vii.RELEASE</version>     <scope>test</scope> </dependency> <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-context</artifactId>     <version>four.3.7.RELEASE</version> </dependency>

The to a higher place dependencies should allow you to autowire Spring Beans into your Test class and Mock objects and their behaviour.  After calculation the above to my pom.xml file, the complete pom.xml of my RESTful Web Services app looks like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-case"          xsi:schemaLocation="http://maven.apache.org/POM/four.0.0 http://maven.apache.org/maven-v4_0_0.xsd">     <modelVersion>4.0.0</modelVersion>     <groupId>com.appsdeveloperblog.ws</groupId>     <artifactId>HibernateExamples</artifactId>     <packaging>war</packaging>     <version>1.0-SNAPSHOT</version>     <name>HibernateExamples Maven Webapp</name>     <url>http://maven.apache.org</url>     <dependencies>                  <!-- https://mvnrepository.com/artifact/junit/junit -->         <dependency>             <groupId>junit</groupId>             <artifactId>junit</artifactId>             <version>four.12</version>             <telescopic>test</scope>         </dependency>         <!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->         <dependency>             <groupId>org.mockito</groupId>             <artifactId>mockito-core</artifactId>             <version>2.8.47</version>             <telescopic>exam</scope>         </dependency>          <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.bundles/jaxrs-ri -->         <dependency>             <groupId>org.glassfish.bailiwick of jersey.bundles</groupId>             <artifactId>jaxrs-ri</artifactId>             <version>2.25</version>         </dependency>                  <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-moxy -->         <dependency>             <groupId>org.glassfish.jersey.media</groupId>             <artifactId>jersey-media-moxy</artifactId>             <version>2.25</version>         </dependency>              <!-- Hibernate Dependencies -->         <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->         <dependency>             <groupId>org.hide</groupId>             <artifactId>hide-core</artifactId>             <version>v.2.8.Last</version>         </dependency>          <!-- https://mvnrepository.com/artifact/org.hide/hibernate-validator -->         <dependency>             <groupId>org.hibernate</groupId>             <artifactId>hide-validator</artifactId>             <version>v.four.0.Terminal</version>         </dependency>                  <!-- MySQL Dependencies -->         <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->         <dependency>             <groupId>mysql</groupId>             <artifactId>mysql-connector-java</artifactId>             <version>v.ane.41</version>         </dependency>                  <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-beans</artifactId>             <version>iv.3.7.RELEASE</version>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-test</artifactId>             <version>4.three.7.RELEASE</version>             <telescopic>test</scope>         </dependency>         <dependency>             <groupId>org.springframework</groupId>             <artifactId>spring-context</artifactId>             <version>4.3.7.RELEASE</version>         </dependency>         <dependency>             <groupId>org.hibernate</groupId>             <artifactId>hibernate-entitymanager</artifactId>             <version>4.iii.1.Terminal</version>         </dependency>      </dependencies>     <build>         <finalName>HibernateExamples</finalName>     </build> </project>              

Create Spring ContextConfiguration Course

To be able to @Autowire classes into my Test course, I demand to create a new Coffee course with @Configuration notation and specify the base packet name which will be used by @ComponentScan to notice all the Spring Beans which tin can be autowired. This class needs to be created in a package under the Tests folder in your project.

packet com.appsdeveloperblog.ws;  import org.springframework.context.annotation.ComponentScan; import org.springframework.context.note.Configuration;  @Configuration @ComponentScan(basePackages = {"com.appsdeveloperblog.ws"}) public class TestConfiguration { }

The UsersServiceImpl grade

Please note the use of @Service("usersService") above the class proper name. The value you specify in @Service annotation should match the value you lot specify in @Qualifier("usersService") when yous are automobile wiring the Spring Bean in your Exam grade. Thus the values of @Service("usersService") and @Qualifier("usersService") in my case match.

Beneath is my version of the Service class which I am testing with the UsersServiceImplTest class the code of which I am also going to paste below.

package com.appsdeveloperblog.ws.service.impl;  import com.appsdeveloperblog.ws.io.dao.Database; import com.appsdeveloperblog.ws.io.entity.UserProfileEntity; import com.appsdeveloperblog.ws.service.UsersService; import com.appsdeveloperblog.ws.shared.dto.UserProfileDto; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service;    @Service("usersService") public grade UsersServiceImpl implements UsersService {      Database database;      public UserProfileDto saveUser(UserProfileDto userDto) {                  UserProfileDto returnValue = zip;                  UserProfileEntity userEntity = new UserProfileEntity();         BeanUtils.copyProperties(userDto, userEntity);                  // Connect to database          try {             this.database.openConnection();             UserProfileEntity storedUserEntity = this.database.saveUserProfile(userEntity);             if(storedUserEntity != nix && storedUserEntity.getId()>0)             {                 returnValue = new UserProfileDto();                 BeanUtils.copyProperties(storedUserEntity, returnValue);             }         }  finally {             this.database.closeConnection();         }                  render returnValue;     }  }

Create the Test form, Autowire Service Form and Mock objects

Because we are creating a examination class for UsersServiceImpl course,the proper name of our test form volition be UsersServiceImplExam.

In the code below please note that nosotros are going to exam the saveUser() method of UsersService. This is why we add the @Autowired and the @InjectMocks on UsersService object proclamation and so we simply mock all other objects and their methods.

Also, please notice the utilize of @Qualifier("usersService"). Inside of the @Qualifier annotation we need to specify the value which we have specified in the @Service annotation in the UsersServiceImpl class above.

Below is a complete UsersServiceImplTest grade which is testing the saveUser() method of UsersServiceImpl class.

packet com.appsdeveloperblog.ws.service.impl;  import com.appsdeveloperblog.ws.TestConfiguration; import com.appsdeveloperblog.ws.io.dao.Database; import com.appsdeveloperblog.ws.io.entity.UserProfileEntity; import com.appsdeveloperblog.ws.service.UsersService; import com.appsdeveloperblog.ws.shared.dto.UserProfileDto; import org.junit.*; import org.junit.runner.RunWith; import static org.mockito.ArgumentMatchers.any; import org.mockito.InjectMocks; import org.mockito.Mock; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.when; import org.mockito.MockitoAnnotations; import org.springframework.beans.mill.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.examination.context.junit4.SpringJUnit4ClassRunner;   @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration( classes = TestConfiguration.class ) public class UsersServiceImplTest {      @Mock     Database database;      @Mock     UserProfileEntity userProfileEntity;       @Autowired     @InjectMocks     @Qualifier("usersService")     UsersService usersService;      @Before     public void setUp() {         MockitoAnnotations.initMocks(this);     }      @Test     public void testSaveUser() {         Organization.out.println("testing saveUser");           //Mocking Database open and shut methods         doNothing().when(database).openConnection();         doNothing().when(database).closeConnection();          // Mocking userProfileEntity methods         when( userProfileEntity.getFirstName() ).thenReturn( "Sergey" );         when( userProfileEntity.getLastName()).thenReturn( "Kargopolov" );         when( userProfileEntity.getId() ).thenReturn( new Long(one) );                  // Mocking database saveUserProfile method         when( database.saveUserProfile( whatsoever(UserProfileEntity.form) ) ).thenReturn( userProfileEntity );          // Create sample UserProfileDto         UserProfileDto userProfileDto = new UserProfileDto();         userProfileDto.setFirstName( "Sergey" );         userProfileDto.setLastName( "Kargopolov" );          // Call saveUser method         UserProfileDto consequence = usersService.saveUser( userProfileDto );               // Assert expected results         Assert.assertNotNull( event );         Assert.assertEquals( userProfileDto.getFirstName() , result.getFirstName() );         Assert.assertEquals( userProfileDto.getLastName() , result.getLastName() );     } }

And this is information technology. Following the example in this weblog mail you lot can create Unit tests with JUnit and Mockito for whatsoever of your Service layer classes in the project.

To larn more most how to use JUnit and Mockito frameworks to test your Restful Web Services cheque out these video courses and books below:

Testing Your Lawmaking with JUnit

JUnit and Mockito Crash Course

Learn how to utilize JUnit and Mockito and Unit Test in like shooting fish in a barrel steps.


icon

Mockito Tutorial : Learn mocking with 25 Junit Examples

Larn unit testing and mocking with 25 Junit Examples


icon

How To Use Soap Input And Output In Junit Test Cases To Test Web Service,

Source: https://www.appsdeveloperblog.com/test-restful-web-service-junit-mockito/

Posted by: rosaalent1945.blogspot.com

0 Response to "How To Use Soap Input And Output In Junit Test Cases To Test Web Service"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel