2017-05-05 4 views
1

サンプルテストケースを作成しました。ここでvoidインスタンスメソッドを模擬したいと思います。私は驚いています。私のテストケースは、expectLastCallメソッドを呼び出さずに渡しています。私が知りたいのは、expectLastCallの呼び出しはインスタンスのvoidメソッドをモックしている間は不要ですか?インスタンスのvoidメソッドのmockingが 'expectLastCall'メソッドを呼び出さずに動作しています

StringUtil.java

package com.sample.util; 

import com.sample.model.MethodNotImplementedException; 

public class StringUtil { 
    public String toUpperAndRepeatStringTwice(String str) { 
     String upperCase = str.toUpperCase(); 
     sendStringToLogger(upperCase); 
     return upperCase + upperCase; 
    } 

    public void sendStringToLogger(String str){ 
     throw new MethodNotImplementedException(); 
    } 
} 

StringUtilTest.java

package com.sample.util; 

import static org.junit.Assert.assertEquals; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.powermock.api.easymock.PowerMock; 
import org.powermock.core.classloader.annotations.PrepareForTest; 
import org.powermock.modules.junit4.PowerMockRunner; 

@RunWith(PowerMockRunner.class) 
@PrepareForTest({ StringUtil.class }) 
public class StringUtilTest { 

    @Test 
    public void toUpperAndRepeatStringTwice() { 
     StringUtil stringUtil = PowerMock.createPartialMock(StringUtil.class, "sendStringToLogger"); 

     String str = "HELLO PTR"; 
     stringUtil.sendStringToLogger(str); 
     //PowerMock.expectLastCall().times(1); 
     PowerMock.replayAll(); 

     String result = stringUtil.toUpperAndRepeatStringTwice("hello ptr"); 

     assertEquals(result, "HELLO PTRHELLO PTR"); 
    } 
} 
+0

答えはこちら[ここ](http://stackoverflow.com/questions/22831523/easymock-void-method) –

+0

ちょっと不思議なこと:あなたはここでPowerMockを使っていますか? Mockitoは、PowerMockに頼らずに、あなたの 'StringUtil'を偽装したりスパイしたりすることができるはずです...ここではユースケースがないのですか? –

+0

PowerMockとEasyMockを使用しています。ちょうど実験する –

答えて

3

expectLastCallは必要ありません。 EasyMockとPowerMockレイヤーについても同様です。だからあなたは正しい。

これは、一部のユーザーにとって分かりやすくするために使用されています。それは前の方法がある種のランダムな呼び出しではなく、期待であることが明らかになるからです。しかしそれは要件よりもスタイルの問題です。

デフォルトであるため、time(1)も必要ありません。

ところで、回答hereは間違っていると私はそれに応じてコメントしました。

+0

**ベリファイ**の電話をしても? – GhostCat

+0

はい。違いはありません。記録モードでvoidメソッドを呼び出すと、記録されます。 – Henri

+0

私は数年前からEasyMockを使用していました。今日は 'times()'や 'andThrow()'のような "follow on"メソッドを呼び出す必要があるときに 'expectLastCall()'だけが必要であることを知りました。そのレッスンを教えて、今日のゲストをGhostcat-upvote-partyにしましょう! – GhostCat

関連する問題