2011-08-10 12 views
5

私はSpring Webアプリケーションを持っていますので、私のコントローラにunittestを作成したいと思います。私はテストをセットアップするためにSpringを使用せず、コントローラと一緒にMockitoモックオブジェクトを使用することに決めました。Mockito Testcaseは注釈を無視します

私はMaven2とsurefireプラグインを使ってテストをビルドして実行します。これは私のpom.xml

 <!-- Test --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-test</artifactId> 
      <version>${spring.framework.version}</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.junit</groupId> 
      <artifactId>com.springsource.org.junit</artifactId> 
      <version>4.5.0</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.mockito</groupId> 
      <artifactId>mockito-all</artifactId> 
      <version>1.9.0-rc1</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

私のセットアップ私のコンパイラと確実なプラグインから、このようなものです:

<build> 
    <pluginManagement> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <verbose>true</verbose> 
        <compilerVersion>1.6</compilerVersion> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.4.3</version> 
      </plugin> 

私のテストクラスは次のようになります。

@RunWith(MockitoJUnitRunner.class) 
public class EntityControllerTest { 

private EntityController entityController; 

private DataEntityType dataEntityType = new DataEntityTypeImpl("TestType"); 

@Mock 
private HttpServletRequest httpServletRequest; 

@Mock 
private EntityFacade entityFacade; 

@Mock 
private DataEntityTypeFacade dataEntityTypeFacade; 

@Before 
public void setUp() { 
    entityController = new EntityController(dataEntityTypeFacade, entityFacade); 
} 

@Test 
public void testGetEntityById_IllegalEntityTypeName() { 
    String wrong = "WROOONG!!"; 
    when(dataEntityTypeFacade.getEntityTypeFromTypeName(wrong)).thenReturn(null); 
    ModelAndView mav = entityController.getEntityById(wrong, httpServletRequest); 
    assertEquals("Wrong view returned in case of error", ".error", mav.getViewName()); 
} 

注釈をすべての周り: - )

しかし、コマンドラインからビルドすると、行にNullPointerExceptionが表示されます。(dataEntityTypeF acade.getEntityTypeFromTypeName(間違っている))。then返す(null); dataEntityTypeFacadeオブジェクトがnullであるためです。 Eclipseでテストケースを実行すると、すべてがうまくいっていて、モックオブジェクトがインスタンス化され、@Beforeで注釈が付けられたメソッドが呼び出されます。

コマンドラインから実行すると、私の注釈が一見無視されるのはなぜですか?

/エヴァ

+0

「コマンドラインからの建物」とは、あなたがMavenを意味していますビルドや何か他の? –

答えて

5

あなたが求めている:基底クラスやテストランナーで

MockitoAnnotations.initMocks(testClass); 

をここに言及として:http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#9

+1

私はこの質問を投稿するのを忘れていました:私は@RunWith(MockitoJUnitRunner.class)をテストクラス定義の上に持っています。私が理解する方法では、initMocsを呼び出す必要はありません。 –

+0

はい、これが私たちのために働く唯一の方法でした。 –

+0

私はドキュメントを読んで、それが動作するにはinitMocks()行が必要なように聞こえます。太字の「重要」になっています。セクション。それは、あなたが「ランナー」をそれと共に使用するかもしれないと言います。違いがランナーの有無に関係なくわかりません。しかし、必ずこの線が必要です。 IMO - @Mockアノテーションは、手動でモックを設定するほど良くはありません。 –