この記事では、スプリングブーツをキュウリ(BDD)に統合する方法を紹介し、それを共有します。詳細は次のとおりです。
1.次のように新しいスプリングブートプロジェクト構造を作成します。
2。POM依存関係を追加します
<?xml version = "1.0" encoding = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance <http://www.w3.org/2001 xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.0.sdsd"> <modelversion> 4.0.0 </modelversion> <グループ<グループ</groupid> <artifactid> spring-boot-cucumber </artifactid> <バージョン> 0.0.1-snapshot </version> <packaging> jar </packaging> <name> spring-boot-cucumber </name> <説明> spring boot and cucumber </description> <parent> <parent> <parent> boot </groupid> <artifactid> spring-boot-starter-parent </artifactid> <バージョン> 1.5.6.Release </version> <RecationPath/> <! - リポジトリからの親を探してください - > </parent> <Properties> <cucumber.version> 1.2.4 </cucumber.version> <project.build. <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> 1.7 </java.version> </properties> <dependencies> <shipencies> <dependency> <グループ<Dependency> GroupId> info.cukes </groupid> <artifactid> cucumber-java </artifactid> <version> $ {cucumber.version} </version> </dependency> <sependency> <sheprency> <shiplency> <shipid> </dependency> <dependency> groupid> info.cukes </groupid> <artifactid> cucumber-spring </artifactid> <bersion> $ {cucumber.version} </version> </dependency> <ependency> info.cukes </groupid> <artifactid> </artifactid> <バージョン> $ {cucumber.version} </version> <exclusions> <exclusion> <explusion> <groupid> junit </groupid> <artifactid> junit </artifactid> </exclusion> </exclusions> </dependency> <依存症<scope> test </scope> </dependency> </dependencies> <bulting> <blubins> <groupid> org.springframework.boot </groupid> <artifactid> artifactid> <configuration> <source> 1.7 </source> <groupid> org.codehaus.mojo </groupid> <artifactid> exec-maven-plugin </artifactid> <configuration> <source> 1.7 </source> <target> 1.7 </target> </configuration> <executions> <execution> <phas> integration-test </fase> <goal> <goal> <goal> <goal> <goal> <goal <classpathscope> test </classpathscope> <mainclass> com.chhliu.test.cucumbertest.java </mainclass> <arguments> <astourt> - プラグイン</arging> <引数> pretty </argupe> <引数> - glue </argute> <引数> src/テスト/</</execuments> </contionment </プラグイン> </build> </project>2。サービスインターフェイスと実装クラスを書き込みます
パッケージcom.chhliu.service; / ** *シミュレーションログイン * @author chhliu * */ public interface userinfoservicei {boolean login(string username、string password、string cundimpassword); }パッケージcom.chhliu.service; org.springframework.stereotype.serviceをインポートします。 @Service( "UserInFoservice")パブリッククラスUserInfoserviceはuserininfoservicei {public boolean login(string username、string password、string confirmpassword){return( "chhliu")&& password.equals( "123456") }} 3.機能ファイルを書き込みます
#Language:Zh-cn# "zh-cn":{# "But": "*| but <"、# ":"*| and <| and <| and <| and <| and <| a sut "、#" ":"*| so <"、#" 「バックグラウンド」、# "シナリオ":「シナリオ_outline ":「シナリオアウトライン|スクリプトアウトライン"、# "examples"、 "examples"、# "dign": "*| if <| if <| if <| if <| if <"#} @bank関数:銀行でのバランスのバランスのバランスを正確に掲載します。残高が500,000の場合、シナリオ:銀行は次の場合にお金を引き出し、「Chhliu」でログインし、入力したパスワードは「123456」です。 4。テストクラスを書きます
パッケージcom.chhliu.test; Import org.junit.runner.runwith; import cucumber.api.cucumberoptions; import cucumber.api.junit.cucumber; /*** @runwith(cucumber.class)これはランナーであり、キュウリを使用してテストを実行することを指します* @cucumberoptionsは、プロジェクトで実行される機能のディレクトリを指定します* @cucumberoptions形式を指定します。プロジェクトが実際のプロジェクトで * * * * * * *進行中に * *を実行したときにステップ定義ファイルを見つけるディレクトリを指定するために使用されるために、テストプロジェクトは複数の機能ファイルで構成され、各機能ファイルも複数のシナリオで構成されている場合があります。デフォルトでは、 *各実行は、すべての機能ですべてのシナリオを実行することです。これにより、テストスクリプトが通常の状況で一度実行される可能性があり、テスト結果を待つのに非常に長い時間がかかります。 *ただし、実際のプロセスでは、テストケースは優先度によって区別されます。たとえば、Smoketest、回帰テストなど。または、レベルが重要であるなど、特に小さなユースケースがある場合があります。これらのユースケースは、システムにホワイトページがないか404ページを監視するために長期操作が必要です。 *したがって、すべてのシナリオを区別する必要があります。この時点で、Cucumberでタグ *を使用し、タグをタグに直接追加して、機能、シナリ、またはシナリオアウトラインキーワードで機能またはシナリオにプレフィックス @を付けて任意の数のタグを追加します。 "Pretty"}、feature = "src/test/resources")パブリッククラスCucumbertest {} 5.テストクラスを実行し、テスト出力の未定義の手順を改善します
パッケージcom.chhliu.test; javax.annotation.resourceをインポートします。 Import org.junit.assert; com.chhliu.service.userinfoserviceiをインポートします。 import cucumber.api.java.zh_cn.if; import cucumber.api.java.zh_cn.when; import cucumber.api.java.zh_cn.so; Public Class Cucumber Integration Spring {@Resource(name = "UserInfoService")Private UserInfoServiceiサービス;プライベート文字列ユーザー名;プライベート文字列パスワード。 private string cundilepassword; @if( "^:$ with/"([^/"]*)/" $ ")public void i login _ with _(string arg1)throws throws {this.username = arg1;} @if("^:入力したパスワードis/"([^/"]*)/"$")パスワードが入力されたパスワードが入力された{string } @when( "^:パスワードも/"([^/"]*)/" $ ")public voidパスワードは_(string arg1)スローが投げられる{this.confirmpassword = arg1;} @then("^:bank bank balance is/"([^/"]*)/"$" show show agrabable is([^/"]*)を表示service.login(username、password、cundilepassword); if(islogin){system.out.println( "ログインに正常に!クエリ残高は次のとおりです。"+arg1); assert.assertequals( "500000"、arg1); }}} 6.テスト手順に注釈のサポートを追加します
@runwith(springjunit4classrunner.class)@contextconfiguration //このアノテーションが追加されていない場合、Beanを注入し、@SpringBoottest //この注入が追加されない場合、Beanは公共クラスの統合統合{} {}を見つけることはありません。 7。テスト結果
2つのシナリオ(2が合格)
11ステップ(11件の合格)
0M0.091S
8。統合ポイント
Spring Bootをキュウリと統合する場合、Spring BootはXMLizationを提唱するため、注意が必要なポイントがあります。そのため、従来の方法では、CucumberがClassPathの下のCucumber.xml構成ファイルを読み取り、Beanを初期化します。 Springと統合した後、この方法は使用できません。 @contextConfigurationアノテーションを使用して、クラスの読み込みを実装する必要があります。構成ファイルをロードする必要がある場合は、次のように使用できます。
@contextconfiguration(locations = {"classpath:applicationcontext.xml"})注釈を使用して統合する場合は、以下を使用してください。
@contextconfiguration(classes = springbootcucumberapplication.class)
または直接
@contextconfiguration
特別なメモ:@contextConfigurationアノテーションを追加する必要があります。そうしないと、豆の注入が失敗します。
次に、この状況がソースコードに起因する理由を見てみましょう。
このパートに関与するコードは、キュウリのスプリングパッケージの下のSpringFactoryクラスにあります。次のクラスを見てみましょう。
public void start(){// cucumberテスト開始方法(stepclasswithspringcontext!= null){// @contextconfigurationアノテーションが使用されない場合、これはnull testcontextmanager = new cucumbertestcontextmanager(stepclasswithspringcontext); } else {//それ以外の場合は、stepclasswithspringcontextはnullになり、次のブランチに入ります(beanfactory == null){beanfactory = createfallbackContext(); if(beanfactory == null || isNewContextCreated()){beanFactory = testContextManager.getBeanFactory(); for(class <?> stepclass:stepclasses){RegisterStepClassBeanDefinition(BeanFactory、StepClass); }} gluecodecontext.instance.start(); } createfallbackContextメソッドに従ってみましょう。
private configurablElistablebeanFactory createfallbackContext(){configureableapplicationContext ApplicationContext; if(getClass()。getClassLoader()。getResource( "cucumber.xml") } else {// cucumber.xmlが構成されていない場合、new genericApplicationContext = new genericApplicationContext(); } applicationContext.registershutdownhook(); configureablElistablebeanfactory beanfactory = applicationContext.getBeanFactory(); beanfactory.registerscope(gluecodescope.name、new gluecodescope()); for(class <?> stepclass:stepclasses){RegisterStepClassBeanDefinition(BeanFactory、StepClass); } return beanFactory; }最後に、genericApplicationContextクラスについて話しましょう。このクラスは、Beanのタイプ、次にNewInstanceインスタンスに基づいています。ただし、他のクラスがこのクラスに注入されるため、注入されたクラスを新しいインスタンスで初期化することはできないため、注入が故障し、ヌルポインターが報告されます。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。