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:
And, in case of an error, the result will be like this:
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:
2. We edit pom.xml. Add plugins:
It is also important that there is the ability to edit templates to more finely configure the generation of events.
Add dependencies:
3. The code of the test methods looks like this:
You can verify operation by running the mvn test command.
Links:
AssertJ - http://joel-costigliola.github.io/assertj/
Working example - SOAPTester
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-plugin 3.0.0 ${project.basedir}/src/generated false Plugin to download WDSL descriptions
com.googlecode.maven-download-plugin maven-download-plugin 1.1.0 Download Mayak wget validate http://speller.yandex.net/services/spellservice?WSDL ${project.build.directory}/resources/wsdl spellservice.xml true Plugin for generating client code according to WDSL description
org.apache.cxf cxf-codegen-plugin 3.1.3 generate-sources generate-sources ${project.basedir}/src/generated ${project.build.directory}/resources/wsdl/spellservice.xml -client wsdl2java Plugin for generating assertions based on classes from WDSL
org.assertj assertj-assertions-generator-maven-plugin 2.0.0 generate-assertions net.yandex.speller.services.spellservice.SpellResult
net.yandex.speller.services.spellservice.SpellError
true org.assertj ${project.basedir}/src/generated true src/main/resources/templates/ class.txt method.txt object.txt primirives.txt wrappers.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.mojo build-helper-maven-plugin 1.9.1 add source generate-sources add source $ {project.basedir} / src / generated
Add dependencies:
Required Dependencies
org.testng testng 6.9.8 test junit junit org.assertj assertj-core 3.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