2

それでも今は JUnit を使用していますが、EasyMock に出会いました。両方とも同じ目的であることを理解しています。私の理解は正しいですか?

EasyMock が Junit より優れている点は何ですか?

設定しやすいのはどっち?

EasyMock には制限がありますか?

学ぶのを手伝ってください

4

3 に答える 3

7

When I explain unit testing I like to describe them as a list of phases:

  • Test Setup : Define and Create all the Data and Objects you need for the tests
  • Expectations : Say what methods and parameters you expect to be executed during the test
  • Test : The actual behavior/method call you want to test
  • Assertions : Statements that make sure the outcome of the test were successful
  • Test Tear down : Destroy any side effects that occurred during the test

jUnit is a Unit Testing Framework and provides all but the Expectations phase of testing. Alternatives in the Java space include:

  • TestNG
  • jtest
  • jBehave (sort of)
  • jDave (sort of)

Other Language equivalents include:

  • PHP - phpUnit
  • Ruby - Test::Unit
  • Flash - FlexUnit

The concept of mocking is what added the new phase of Expectations, and since jUnit saw most of it's major development prior to the mocking movement, those features were not incorporated into the core, and a set of tools to fill that gap in the java space have opened up. Those libraries include

  • EasyMock
  • jMock
  • jMockIt

All of these libraries are compliments to any of the above Unit Testing frameworks I listed, including jUnit. They add the ability to define mock objects. Mock objects get "expectations" assigned to them, which are then asserted in the Assertions phase. Each Mock library accomplishes this slightly differently, but the major models are

  • Record Replay - EasyMock
  • Expectations - jMock, jMockIt

I personally am a fan of the Expectations approach, which is more declarative, and less error prone, because it requires less methods to be called by the implementor, but that is a stylistic preference not a technical one.

Other Languages (since they came to the unit testing world later than java) don't have this seperation for the most part. The Unit Testing Library and Mock Library are one and the same. This is the case in phpunit, rspec. I imagine jUnit will not be incorporating this natively any time soon, since there is already such a rich set of alternative mock libraries available.

于 2010-04-23T02:54:53.883 に答える
3

それらは同じものではありません

JUnit はxUnitテスト フレームワークです。テスト スイートをループし、自動化された各単体テストを実行して結果を記録するテスト ランナーがあります。

EasyMock はモック オブジェクト フレームワークです。セットアップが難しい共同作業者をダミー/偽物/モックに置き換えて、テストしようとしている動作に集中できるようにするために使用されます。
たとえば、私の SUT.AuditCustomers() が DAO.GetCustomer(databasePath) を呼び出す場合、私のテストではメソッド AuditCustomers() に焦点を当てたいので、代わりにデータベースから顧客を読み取らない mockDAO を使用します。既知の/ハードコードされた顧客オブジェクトを簡単にテストできます。これには、GetCustomer のバグが原因で AuditCustomers() のテストが失敗しないという利点もあります。

于 2010-04-22T05:03:37.673 に答える
0

確かではありませんが、JUnit と EasyMock は互いに排他的ではなく、連携して動作するはずです。JUnit のアイデアは、特定のメソッドをテストすることです。したがって、JUnit テストが他のクラス メソッドにまったく依存しないようにするために、他のクラスのダミー インスタンスをそのメソッドに挿入する必要があります。JUnit でモック オブジェクトを提供するというこの目的は、EasyMock や他の同様のモック オブジェクト作成者によって提供されます。スプリングを使用してダミーの実装を JUnit に挿入する場合にも、同様の考え方が使用されます。

EasyMock は有望に思えますが、Spring またはその他のプロキシ オブジェクト ジェネレーターがシナリオに適合するかどうかを評価する必要があります。

于 2010-04-22T05:03:50.060 に答える