Another way to test web services using AssertJ

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

The principle is this:

1. Create a maven project.
2. We configure it so that with each launch the following is performed:
2.1. WSDL service description was loaded by reference
2.2. client code was generated based on WSDL description
2.3. the assertion code was generated for the classes involved in the tests, including those that were generated in the previous step
3. We write tests.
4. Add the project to jenkins, which runs the testing itself.

We will need the following tools: Java, maven, AssertJ, TestNG.

AssertJ is an interesting framework that, among other things, is able to generate events 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 like this:

The following assertion failed:
1) [Проверка SpellError] 
Expecting word of:
  
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 the Yandex public services - tech.yandex.ru/speller/doc/dg/concepts/api-overview-docpage

Let's start :

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

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

2. We edit pom.xml. Add plugins:

Plugin for cleaning generated classes
maven-clean-plugin3.0.0${project.basedir}/src/generatedfalse


Plugin to download WDSL descriptions
com.googlecode.maven-download-pluginmaven-download-plugin1.1.0Download Mayakwgetvalidatehttp://speller.yandex.net/services/spellservice?WSDL${project.build.directory}/resources/wsdlspellservice.xmltrue


Plugin for generating client code according to WDSL description
org.apache.cxfcxf-codegen-plugin3.1.3generate-sourcesgenerate-sources${project.basedir}/src/generated${project.build.directory}/resources/wsdl/spellservice.xml-clientwsdl2java


Plugin for generating assertions based on classes from WDSL
org.assertjassertj-assertions-generator-maven-plugin2.0.0generate-assertionsnet.yandex.speller.services.spellservice.SpellResult
            net.yandex.speller.services.spellservice.SpellError
        trueorg.assertj${project.basedir}/src/generatedtruesrc/main/resources/templates/class.txtmethod.txtobject.txtprimirives.txtwrappers.txt
                            -->


It is also important that there is the ability to edit templates to more finely configure the generation of events.

The plugin includes generated classes in the project
org.codehaus.mojobuild-helper-maven-plugin1.9.1add sourcegenerate-sourcesadd source$ {project.basedir} / src / generated
                


Add dependencies:

Required Dependencies
org.testngtestng6.9.8testjunitjunitorg.assertjassertj-core3.2.0



3. The code of the 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 verify operation by running the mvn test command.

Links:

AssertJ - http://joel-costigliola.github.io/assertj/
Working example - SOAPTester

Also popular now: