vendredi 29 janvier 2016

Why am I getting too few invocations back from this Spock test?

I'm new to the Spock testing framework, and I'm trying to do some testing for an android project I'm working on. These objects I'm currently testing are PJO, so they can be tested with regular Spock. For some reason I keep getting 0 invocations on the isAlive method called on one of my objects, but I know for a fact that it's invoked, sense I've literally run it in a debugger and it gets called. So I'm hoping someone can lead me to knowing what I'm doing wrong.

here's the code I'm testing:

 public void start(int startIndex, boolean overrideDownloadOnCellNetwork){
        this.downloadIndex = startIndex;
        this.overrideDownloadOnCellNetwork = overrideDownloadOnCellNetwork;
        if(checkConnectionType(overrideDownloadOnCellNetwork)){
            this.startTrackDownload();
        }
    }

    // I should simplify this at some point.
    private boolean checkConnectionType(boolean overrideDownloadOnCellNetwork) {
        ConnectivityManager connManager = (ConnectivityManager) masterService.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo connection = connManager.getActiveNetworkInfo();
        if(connection == null){
           broadcaster.broadcastError(DownloadEvent.DownloadEventType.ERROR_NO_NETWORK, downloadIndex);
            this.masterService.stop();
            return false;
        } else if(connection.getType() == ConnectivityManager.TYPE_MOBILE && (userModel.isCellDownloadsAllowed() || overrideDownloadOnCellNetwork)){
            return true;
        } else if (connection.getType() == ConnectivityManager.TYPE_WIFI){
            return true;
        } else {
            broadcaster.broadcastError(DownloadEvent.DownloadEventType.ERROR_NO_WIFI_CELL_DOWNLOAD_NOT_ALLOWED, downloadIndex);
            this.masterService.stop();
            return false;
        }
    }


    private void startTrackDownload(){
        if(trackList.size() > 0) {
            if (!downloadThread.isAlive()) {
                downloadThread.start();
            }
            downloadThread.downloadTrack(downloadIndex, trackList.size(), bookModel.getBookID(), userModel.getSessionID());
        }
    }

and here's my test code: Note downloadThead is a Mock of the actual class, which extends java.lang.Thread

def "should check if download thread is alive"(){
    given:
    def mockConnectionManager = Mock(ConnectivityManager)
    def mockNetworkInfo = Mock(NetworkInfo)
    masterService.getContext().getSystemService(Context.CONNECTIVITY_SERVICE) >> mockConnectionManager
    mockConnectionManager.getActiveNetworkInfo() >> mockNetworkInfo
    mockNetworkInfo.getType() >> ConnectivityManager.TYPE_MOBILE

    when:
    downloader.start(0, true)

    then:
    1 * downloadThread.isAlive()
}

Any help would be greatly appreciated. I've tried this in every configuration I can think of... and it always has this same issue.

Aucun commentaire:

Enregistrer un commentaire