📜 ⬆️ ⬇️

Another way to test web services using AssertJ

I wanted to share with you my way of testing web services.

The principle is as follows:

1. Create a maven project.
2. We configure it so that with each launch the following is done:
2.1. WSDL description of the service was loaded by reference
2.2. client code generated based on WSDL description
2.3. the assertion code was generated for the classes involved in the checks, including those that were generated in the previous step
3. We write tests.
4. Add a project to jenkins, which starts the testing itself.
')
We will need the following tools: Java, maven, AssertJ, TestNG.

AssertJ is an interesting framework that, among other things, can generate asserts for specific classes. This allows you to write tests like this:

//    CheckTextRequest r = new CheckTextRequest(); r.setText("     "); CheckTextResponse resp = port.checkText(r); SpellError sError = resp.getSpellResult().getError().get(0); //  c    soft.assertThat(sError ) .as(" SpellError") .hasCode(1) .hasCol(24) .hasLen(7) .hasPos(24) .hasRow(0) .hasWord("555") .hasOnlyS(" "); 

And, in case of an error, the result will be as follows:

 The following assertion failed: 1) [ SpellError] Expecting word of: <net.yandex.speller.services.spellservice.SpellError@61ca2dfa> to be: <555> but was: <> at org.assertj.SoftAssertions.assertAll(SoftAssertions.java:32) at ru.x_noname.test.SOAPTest$Listener.afterInvocation(SOAPTest.java:69) ... 

For my example, I chose one of Yandex’s public services - tech.yandex.ru/speller/doc/dg/concepts/api-overview-docpage

Let's start:

1. Create a maven project in the usual way, like this:

 mvn archetype:generate -DgroupId=ru.x_noname.test -DartifactId=SOAPTester -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 

2. Edit pom.xml. Add plugins:

Plugin for cleaning generated classes
 <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.0.0</version> <configuration> <filesets> <fileset> <directory>${project.basedir}/src/generated</directory> <followSymlinks>false</followSymlinks> </fileset> </filesets> </configuration> </plugin> 


Plugin for downloading WDSL descriptions
 <plugin> <groupId>com.googlecode.maven-download-plugin</groupId> <artifactId>maven-download-plugin</artifactId> <version>1.1.0</version> <executions> <execution> <id>Download Mayak</id> <goals> <goal>wget</goal> </goals> <phase>validate</phase> <configuration> <url>http://speller.yandex.net/services/spellservice?WSDL</url> <outputDirectory>${project.build.directory}/resources/wsdl</outputDirectory> <outputFileName>spellservice.xml</outputFileName> <skipCache>true</skipCache> </configuration> </execution> </executions> </plugin> 


Plugin for generating client code according to WDSL description
 <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin</artifactId> <version>3.1.3</version> <executions> <execution> <id>generate-sources</id> <phase>generate-sources</phase> <configuration> <sourceRoot>${project.basedir}/src/generated</sourceRoot> <wsdlOptions> <wsdlOption> <wsdl>${project.build.directory}/resources/wsdl/spellservice.xml</wsdl> <extraargs> <extraarg>-client</extraarg> </extraargs> </wsdlOption> </wsdlOptions> </configuration> <goals> <goal>wsdl2java</goal> </goals> </execution> </executions> </plugin> 


Plugin for generating asserts based on classes from WDSL
 <plugin> <groupId>org.assertj</groupId> <artifactId>assertj-assertions-generator-maven-plugin</artifactId> <version>2.0.0</version> <executions> <execution> <goals> <goal>generate-assertions</goal> </goals> </execution> </executions> <configuration> <classes> <param>net.yandex.speller.services.spellservice.SpellResult</param> <param>net.yandex.speller.services.spellservice.SpellError</param> </classes> <hierarchical>true</hierarchical> <entryPointClassPackage>org.assertj</entryPointClassPackage> <targetDir>${project.basedir}/src/generated</targetDir> <generateSoftAssertions>true</generateSoftAssertions> <!--        .     <templates> <templatesDirectory>src/main/resources/templates/</templatesDirectory> <softEntryPointAssertionClass>class.txt</softEntryPointAssertionClass> <softEntryPointAssertionMethod>method.txt</softEntryPointAssertionMethod> <objectAssertion>object.txt</objectAssertion> <wholeNumberAssertion>primirives.txt</wholeNumberAssertion> <wholeNumberWrapperAssertion>wrappers.txt</wholeNumberWrapperAssertion> </templates> --> </configuration> </plugin> 


It is also important that there is the ability to edit templates for more fine-tuning of the generation of acerts.

Plugin includes generated classes in project
 <plugin>
     <groupId> org.codehaus.mojo </ groupId>
     <artifactId> build-helper-maven-plugin </ artifactId>
     <version> 1.9.1 </ version>
     <executions>
         <execution>
             <id> add-source </ id>
             <phase> generate-sources </ phase>
             <goals>
                 <goal> add-source </ goal>
             </ goals>
             <configuration>
                 <sources>
                     <source> $ {project.basedir} / src / generated </ source>
                 </ sources>
             </ configuration>
         </ execution>
     </ executions>
 </ plugin>


Add dependencies:

Required dependencies
 <!--   --> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.9.8</version> <scope>test</scope> <exclusions> <exclusion> <groupId>junit</groupId> <artifactId>junit</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>3.2.0</version> </dependency> 



3. The code of test methods looks like this:
 @Test(description = " ") public void t1_positive() throws Exception { CheckTextRequest r = new CheckTextRequest(); r.setText("  "); CheckTextResponse resp = port.checkText(r); SpellResult res = resp.getSpellResult(); soft.assertThat(res).as(" SpellResult").hasNoError(); } @Test(description = " ") public void t2_negative() throws Exception { CheckTextRequest r = new CheckTextRequest(); r.setText("     "); CheckTextResponse resp = port.checkText(r); SpellResult res = resp.getSpellResult(); SpellError sError = res.getError().get(0); soft.assertThat(sError).as(" SpellError") .hasCode(1) .hasCol(24) .hasLen(7) .hasPos(24) .hasRow(0) .hasWord("") .hasOnlyS(""); // has... -    AssertJ } 

You can check the work by running the command mvn test

References:

AssertJ - http://joel-costigliola.imtqy.com/assertj/
Working Example - SOAPTester

Source: https://habr.com/ru/post/269961/


All Articles