检查是否在另一个方法中调用了一个方法

use*_*029 3 java junit jmock

在java中有没有办法检查某个方法是否在另一个方法中被调用?我正在测试一个类和我遇到播放声音问题的方法,实际上没有办法获取播放的音频文件(内部类中的私有属性)而不更改代码.然而,该方法播放声音的方式是调用播放单个声音的方法(playSadMusic,playHappyMusic等).这些方法在我必须为其创建模拟对象的接口中.我有点坚持我将如何测试这个.有什么想法吗?关于我如何测试这个以外的任何其他想法,而不是检查某个方法是否被调用是受欢迎的.

我正在使用JMock 2.6.0和JUnit 4

音频接口

public interface StockTickerAudioInterface {

    public abstract void playHappyMusic();

    public abstract void playSadMusic();

    public abstract void playErrorMusic();
}
Run Code Online (Sandbox Code Playgroud)

花药界面我必须创建一个模拟

public interface StockQuoteGeneratorInterface {
    public abstract StockQuoteInterface getCurrentQuote() throws Exception;

    public abstract String getSymbol();

    public abstract void setSymbol(String symbol);

    public abstract StockQuoteGeneratorInterface createNewInstance(String symbol);

}
Run Code Online (Sandbox Code Playgroud)

正在测试的课程

public class StockQuoteAnalyzer {
    private StockTickerAudioInterface audioPlayer = null;
    private String symbol;
    private StockQuoteGeneratorInterface stockQuoteSource = null;

    private StockQuoteInterface lastQuote = null;
    private StockQuoteInterface currentQuote = null;


    public StockQuoteAnalyzer(String symbol,
        StockQuoteGeneratorInterface stockQuoteSource,
        StockTickerAudioInterface audioPlayer)
        throws InvalidStockSymbolException, NullPointerException,
        StockTickerConnectionError {
        super(); 

    // Check the validity of the symbol.
        if (StockTickerListing.getSingleton().isValidTickerSymbol(symbol) == true){
            this.symbol = symbol;
        } else {
        throw new InvalidStockSymbolException("Symbol " + symbol
                + "not found.");
        }
        if (stockQuoteSource == null) {
             throw new NullPointerException(
                "The source for stock quotes can not be null");
        }
        this.stockQuoteSource = stockQuoteSource;
        this.audioPlayer = audioPlayer;
    }
    public double getChangeSinceLast() {
        double retVal = 0.0;
        if (this.lastQuote != null) {
            double delta = this.currentQuote.getLastTrade() - this.lastQuote.getLastTrade();
            retVal = 100 * (delta / this.lastQuote.getLastTrade());
           }
           return retVal;
    }

    public double getChangeSinceYesterday() {
        double delta = (this.currentQuote.getLastTrade() - this.currentQuote
            .getClose());
        return 100 * (delta / this.currentQuote.getClose());

    }

    public void playAppropriateAudio() {
        if ((this.getChangeSinceYesterday() > 2)
            || (this.getChangeSinceLast() > 0.5)) {
            audioPlayer.playHappyMusic();
    }

        if ((this.getChangeSinceYesterday() < -2)
            || (this.getChangeSinceLast() < -0.5)) {
            audioPlayer.playSadMusic();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

Joh*_*now 7

如果使用,Mockito您可以使用verify()检查方法的调用次数.像这样使用它:

verify(mockedObject, times(1)).methodToValidate();
Run Code Online (Sandbox Code Playgroud)

您可以检查是否methodToValidate()使用特定字符串调用ei verify(mockedObject, times(1)).methodToValidate("a specific value"); 或者你可以anyString()像这样使用它:verify(mockedObject, times(1)).methodToValidate(anyString());.

除非使用指定的参数调用此方法,否则测试将失败

阅读更多关于验证的信息.

UPDATE

由于您编辑的帖子表明您正在使用jMock,因此快速googeling向我展示了使用jMock及其expect方法可以实现类似的行为.它的用法如下:

mockedObject.expects(once()).method("nameOfMethod").with( eq("An optional paramter") );
Run Code Online (Sandbox Code Playgroud)

通过阅读jMocks 入门页面可以找到更详细的说明.


san*_*hat 2

child()假设你有一个被调用的方法parent()

public void parent() {
  child();
}
Run Code Online (Sandbox Code Playgroud)

child()获取调用它的最后一个方法,您可以使用StackTraceElement

public void child() {
  StackTraceElement[] traces = Thread.currentThread().getStackTrace();
  boolean check = false;
      for(StackTraceElement element : traces) {
         if(check) {
            System.out.println("Calling method - " + element.getMethodName());
         }
         if(element.getMethodName().equals("child")) {
        check = true;
         }
      }
}
Run Code Online (Sandbox Code Playgroud)