2014-07-23 22 views
6

HomePresenter sınıfımın "onCreate (IHomeView iHomeView)" yöntemini çalıştırdığımda "setTabs()" ifadesinin çağrıldığını test etmeye çalışıyorum. "SetTabs()", IHomeView'da bir arabirim yöntemidir. Bunu test etmek için, ben Mockito kullanıyorum ama homeView.setTabs() için çağırdığınızda bir IllegalArgumentException olsunMockito ile IllegalArgumentException

Benim burada kodu koymak

, çok basittir:

Benim basit bir test:

import junit.framework.Assert; 

import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.robolectric.RobolectricTestRunner; 
import org.robolectric.annotation.Config; 

import static org.fest.assertions.api.Assertions.assertThat; 
import static org.mockito.Mockito.mock; 
import static org.mockito.Mockito.times; 
import static org.mockito.Mockito.verify; 
import static org.robolectric.util.FragmentTestUtil.startFragment; 

@RunWith(RobolectricTestRunner.class) 
public class HomeFragmentTest { 

    @Test 
    public void testOnCreateHomePresenter() { 
     IHomeView homeViewMock = mock(IHomeView.class); 

     HomePresenter homePresenter = new HomePresenter(); 
     homePresenter.onCreate(homeViewMock); 

     verify(homeViewMock, times(1)).setTabs(); 
    } 
} 

IHomeView.class

public interface IHomeView { 

    public void setTabs(); 

} 

HomePresenter.java

public class HomePresenter implements IHomePresenter { 

private IHomeView mHomeView; 

public void onCreate(IHomeView homeView) { 
    mHomeView = homeView; 

    mHomeView.setTabs(); 
} 
} 

HomeFragment -> IHomeView build.gradle

Ayrıca, setTabs() içindeki tüm yöntemler yorum yapmak çalıştık
compile 'com.android.support:appcompat-v7:20.0.+' 
compile 'com.android.support:support-v4:20.+' 
compile 'com.crashlytics.android:crashlytics:1.+' 
compile 'net.hockeyapp.android:HockeySDK:3.0.2' 
compile 'com.squareup.retrofit:retrofit:1.6.1' 
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0' 
compile 'com.squareup.okhttp:okhttp:2.0.0' 
compile 'com.github.castorflex.smoothprogressbar:library:0.5.2' 
compile 'com.astuetz:pagerslidingtabstrip:1.0.1' 
compile 'net.hockeyapp.android:HockeySDK:3.0.2' 
compile('org.roboguice:roboguice:3.0-alpha-2') { 
    exclude module: 'asm' 
} 

compile('fr.avianey:facebook-android-api:[email protected]') { 
    exclude group: 'com.google.android', module: 'support-v4' 
} 

// ================== TESTING LIBRARIES ====================== 
androidTestCompile 'junit:junit:4.10' 
androidTestCompile 'org.robolectric:robolectric:2.3' 
androidTestCompile 'com.squareup:fest-android:1.0.+' 
androidTestCompile 'org.bouncycastle:bcprov-jdk15on:1.50' 
androidTestCompile 'com.jakewharton:butterknife:5.1.0' 
androidTestCompile 'org.mockito:mockito-core:1.9.5' 
androidTestCompile 'com.google.dexmaker:dexmaker:1.0' 
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0' 

de

public class HomeFragment extends RoboFragment implements IHomeView { 

@Inject 
HomePresenter mHomePresenter; 
@InjectView(R.id.tv1) 
TextView textView; 
@InjectView(R.id.progessbar) 
SmoothProgressBar progressBar; 
@InjectView(R.id.pager) 
private ViewPager mViewPager; 
@InjectView(R.id.tabs) 
private PagerSlidingTabStrip tabs; 

private TabAdapter mAdapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.fragment_home, container, 
      false); 

    if (rootView == null) 
     throw new IllegalStateException("Can't inflate the view"); 

    return rootView; 
} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    Logger.logWarning("=>onViewCreated()"); 

    progressBar.setProgressiveStartActivated(true); 

    mHomePresenter.onCreate(this); 
} 


@Override 
public void setTabs() { 
    mAdapter = new TabAdapter(getChildFragmentManager(), getResources()); 
    mViewPager.setAdapter(mAdapter); 

    int marginBetweenPages = getMarginBetweenPages(); 
    mViewPager.setPageMargin(marginBetweenPages); 
    tabs.setViewPager(mViewPager); 
} 
} 

bağımlılıklar uygular, ancak çalışmıyor. Mockito 1.9.5 ve Robolectric 2.3 kullanıyorum. Ben Android Studio 0.8.1 ve testi çalıştırmak için komut kullanın:

./gradlew test 

bence test çok basit, ama işe alamadım. Birisi yanlış yaptığımı söyleyebilir mi?

java.lang.IllegalArgumentException 
    at com.google.dexmaker.mockito.InvocationHandlerAdapter.invoke(InvocationHandlerAdapter.java:49) 
    at com.sun.proxy.$Proxy31.setTabs(Unknown Source) 
    at com.peerade.ave.presenters.HomePresenter.onCreate(HomePresenter.java:24) 
    at com.peerade.ave.tests.HomeFragmentTest.testOnCreateHomePresenter(HomeFragmentTest.java:56) 

Gradle test Yürüten 1 testleri yürütülürken bitmiş:

Ayrıca ben gradle çıkışı içerir.

Çok teşekkürler!

SABİT!

androidTestCompile'dan dexmaker ve dexmaker-mockito'yu kaldırırsanız, bu bir çekicilik gibi çalışır. son sürümlerine Mockito ve Dexmaker yükseltirken, Benim durumumda https://code.google.com/p/dexmaker/issues/detail?id=4

+0

Testiniz kodunun doğru görünüyor. Test sınıfınızın "ithalat" sektini ve aldığınız hata mesajını da gönderebilir misiniz? –

+0

Sadece daha fazla bilgi @ManuelFranco dahil. – juanjo

+0

Bu olduğunda ART kullanıyor muydunuz? Benzer bir istisna var, ama sadece dexmaker ve dexmaker-mockito’yu kaldırmak düzeltmediyse, başka problemler de yaratıyor. Dexmaker'ı kaldırabilirim ve hala aynı erorr'a sahip olabilirim, ama dexmaker-mockito olmadan daha fazla sorunla karşılaşıyorum. Daha fazla bilgi için: https://github.com/mockito/mockito/issues/71 – billmag

cevap

7

sorunu giderilmiştir: Burada çözüm buldu androidTestCompile 'org.mockito:mockito-core:1.10.5' androidTestCompile 'com.crittercism.dexmaker:dexmaker:1.4' androidTestCompile 'com.crittercism.dexmaker:dexmaker-mockito:1.4' androidTestCompile 'com.crittercism.dexmaker:dexmaker-dx:1.4'