2016-03-29 8 views
0

TestNGを使用してTestケースを設定します。私が断言すべきことが何かに当たると、そのテストはとにかく進みます。私はリストと文字列としてそれらを比較しようとしました。実際は次のようになります。Testng 6.9.10 assertEquals()が失敗しないようにする

actual = [The license key was not valid., 
      The license key has expired and is no longer valid., 
      That email domain is not allowed., 
      The maximum number of allowed users for that license key has been reached. No other users may register unless a user is removed or the user limit for that key is increased., 
      That license key does not have a valid start date, the administrator must define an effective date for the license key.] 

私が使用しているコードは以下のとおりです。実際と予想ははっきりと異なりますが、テストは引き続き行われます。私はTestNG 6.9.10を使用しています。 assertEqualsでバグがある場所をどこからでも見つけることができませんでした。

import org.testng.annotations.*; 
import static org.testng.Assert.assertEquals; 
import static org.testng.Assert.assertTrue; 

@Test 
public void verifyLicencingErrorsTest() { 
    List<String> expected = Arrays.asList(
      "The license key was not valid.", 
      "The license key has expired and is no longer valid.", 
      "The license key was not valid.", 
      "The maximum number of allowed users for that license key has been reached. No other users may register unless a user is removed or the user limit for that key is increased.", 
      "That license key does not have a valid start date, the administrator must define an effective date for the license key." 
    ); 
    List<String> actual = createAccountPage.verifyLicencingErrors(); 

    try { 
     assertEquals(actual.toString(), expected.toString(), "The actual alerts did not match what was expected"); 
    } catch (Error e) { 
     verificationErrors.append(e.toString()); 
    } 
} 

私は必要なすべての情報を提供していると思います。もっと必要なら私に知らせてください。

編集:TestNG Assert documentationあたりとして追加輸入

+1

あなたの 'import'sとは何ですか?また、あなたはあなたのアサートをtry..catchの中に入れていますか?どうして?それは決して失敗しません! – SiKing

+0

@SiKingはコードブロックにインポートを追加しました。私は今参照してください。私はもともとこれをJUnitとTestNGでセットアップしました。 JUnitは例外をスローしてテストをすべて止めるので、try/catchを追加する必要がありました。この作業を正しく行うためには、すべてを変更する必要があるように見えます。返信してくれてありがとう! –

+0

JUnitまたはTestNGのようなフレームワークによってスローされた例外は、スイート全体を停止させることはまずありません。通常は、それらのすべてを実行する必要がありますが、最後にはテストの実行回数、失敗、およびスキップが報告されます。いずれにせよ、あなたが本当に必要であれば、少なくとも「例外」をキャッチしてはいけません(http://stackoverflow.com/questions/352780/when-to-catch-java-lang-error)。例外がスローされたかどうかをテストすることです。@Templateアノテーションの 'expectedExceptions'機能を使用することができます。 – Morfic

答えて

2

public static void assertEquals(java.lang.String actual, 
           java.lang.String expected, 
           java.lang.String message) 

Asserts that two Strings are equal. If they are not, an AssertionError, with the given message, is thrown. 
Parameters: 
actual - the actual value 
expected - the expected value 
message - the assertion error message 

問題は、あなたがこのように投げ、ErrorをキャッチしているということではありませんAssertionErrorもはやは、フレームワークに達し、テストはOKとみなされます。あなたがキャッチし、エラーをログに記録した場合、あなたが簡単にそれを見ることができます:

try { 
     assertEquals(actual.toString(), expected.toString(), "The actual alerts did not match what was expected"); 
    } catch (Error e) { 
     log.error("Caught an [" + e.getClass().getName() + "]", e); 
    } 

... yelds:

[ERROR] 2016-03-30 12:51:01,345 test.LauncherTest - Caught an [java.lang.AssertionError] 
java.lang.AssertionError: The actual alerts did not match what was expected expected [[The license key was not valid., The license key has expired and is no longer valid., The license key was not valid., The maximum number of allowed users for that license key has been reached. No other users may register unless a user is removed or the user limit for that key is increased., That license key does not have a valid start date, the administrator must define an effective date for the license key.]] but found [[The license key was not valid., The license key has expired and is no longer valid., That email domain is not allowed., The maximum number of allowed users for that license key has been reached. No other users may register unless a user is removed or the user limit for that key is increased., That license key does not have a valid start date, the administrator must define an effective date for the license key]] 
    at org.testng.Assert.fail(Assert.java:94) 
    at org.testng.Assert.failNotEquals(Assert.java:496) 
    at org.testng.Assert.assertEquals(Assert.java:125) 
    at org.testng.Assert.assertEquals(Assert.java:178) 
    at test.LauncherTest.verifyLicencingErrorsTest(LauncherTest.java:42) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:821) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108) 
    at org.testng.TestRunner.privateRun(TestRunner.java:773) 
    at org.testng.TestRunner.run(TestRunner.java:623) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:357) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:259) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1110) 
    at org.testng.TestNG.run(TestNG.java:1018) 
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) 
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) 
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175) 
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:125) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 

キャッチブロックを削除、または潜在的例外キャッチを洗練しながら、動作します期待通り:

java.lang.AssertionError: The actual alerts did not match what was expected 
Expected :[The license key was not valid., The license key has expired and is no longer valid., The license key was not valid., The maximum number of allowed users for that license key has been reached. No other users may register unless a user is removed or the user limit for that key is increased., That license key does not have a valid start date, the administrator must define an effective date for the license key.] 
Actual :[The license key was not valid., The license key has expired and is no longer valid., That email domain is not allowed., The maximum number of allowed users for that license key has been reached. No other users may register unless a user is removed or the user limit for that key is increased., That license key does not have a valid start date, the administrator must define an effective date for the license key] 
    <Click to see difference> 


    at org.testng.Assert.fail(Assert.java:94) 
    at org.testng.Assert.failNotEquals(Assert.java:496) 
    at org.testng.Assert.assertEquals(Assert.java:125) 
    at org.testng.Assert.assertEquals(Assert.java:178) 
    at test.LauncherTest.verifyLicencingErrorsTest(LauncherTest.java:41) 

=============================================== 
Custom suite 
Total tests run: 1, Failures: 1, Skips: 0 
=============================================== 
関連する問題