Захотелось поделиться с вами моим способом тестирования веб-сервисов.
Принцип такой:
1. Создаем maven проект.
2. Настраиваем его так, чтобы с каждым запуском выполнялось следующее:
2.1. загружалось WSDL описание сервиса по ссылке
2.2. генерировался код клиента на основе WSDL описания
2.3. генерировался код ассертов для классов, участвующих в проверках, в том числе тех, которые были сгенерированы на предыдущем этапе
3. Пишем тесты.
4. Добавляем проект в jenkins, который и запускает само тестирование.
Нам понадобятся следующие инструменты: Java, maven, AssertJ, TestNG.
AssertJ — интересный фреймворк, который, помимо всего прочего, умеет генерировать асерты для конкретных классов. Это позволяет писать тесты так:
//Выполняем запрос к сервису 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("БРОШЮРА ошибка");
И, в случае ошибки, результат будет таким:
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) ...
Для своего примера, я выбрал один из публичных сервисов Яндекса — tech.yandex.ru/speller/doc/dg/concepts/api-overview-docpage
Приступим:
1. Создаем проект maven обычным способом, например так:
mvn archetype:generate -DgroupId=ru.x_noname.test -DartifactId=SOAPTester -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
2. Редактируем pom.xml. Добавляем плагины:
<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> <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> <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> <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>
Важно еще то, что имеется возможность редактирования шаблонов, для более тонкой настройки генерации асертов.
<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>
Добавляем зависимости:
<!-- необходимые зависимости --> <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. Код тестовых методов выглядит так:
@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 }
Проверить работу можно выполнив команду mvn test
Ссылки:
AssertJ — http://joel-costigliola.github.io/assertj/
Рабочий пример — SOAPTester
ссылка на оригинал статьи http://habrahabr.ru/post/269961/
Добавить комментарий