如何在使用Mockito和Dagger 2

时间:2016-11-09 10:58:50

标签: android mockito android-espresso dagger-2

我有一个类String.java,它有一个方法

public String getTermsandCondition(Context context) {
String versionName = SystemUtils.getAppVersionName(context);
return "version:"+ versionName;
}

所以我在我的MainActivity.Java中使用此方法,例如string.getTermsandCondition(this)

当我在MainActivityTest.Java中使用同样的方法时,就像这样 Mockito.when(getString.getTermsandCondition(copyRight.getActivity())).thenReturn("Term&Conditions");

但我在匹配时遇到错误 onView(withId(R.id.message)).check(matches(withText("Term&Conditions")));

所以我的问题是如何从MainActivityTest.Java

中的getString.getTermsandCondition(copyRight.getActivity())传递上下文
public class MainActivityTest{

@Inject
CopyrightString getString;


@Rule
public IntentsTestRule<MainActivity> copyRight = new IntentsTestRule<>(MainActivity.class, true, false);

@Before
public void setUp() {
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    Application app
            = (Application) instrumentation.getTargetContext().getApplicationContext();
    CopyrightTestComponent component = (CopyrightTestComponent) app.component();
    component.inject(this);
}

@Test
public void ensureMsgShows() {
    Mockito.when(getString.getTermsandCondition(copyRight.getActivity())).thenReturn("Term&Conditions");
    copyRight.launchActivity(new Intent());
    onView(withId(R.id.message)).check(matches(withText("Term&Conditions")));
}

@Test
public void ensureAcceptClick() {
    copyRight.launchActivity(new Intent());
    onView(withText("Accept")).perform(click());
    intended((hasComponent(NextActivity.class.getName())));
}
}

这是我的MainActivity.Java

public class MainActivity extends AppCompatActivity {


@Inject
CopyrightString string ;


 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_copyright);
 ((Application) getApplication()).component().inject(this);
 ((TextView) findViewById(R.id.message)).setText(string.getTermsandCondition(this));

 }

这是我的CopyrightString.Java

 @Singleton
 public class CopyrightString {

 public String getTermsandCondition(Context context) {
    String versionName = SystemUtils.getAppVersionName(context);
    return "version:"+ versionName;
}

@Module
public class CopyrightStringModule {

@Provides
@Singleton
CopyrightString provideGetTerm$Condition(){
    return new CopyrightString();
   }
}


@Singleton
@Component(modules = {CopyrightStringModule.class})

public  interface MyAppComponent{

void inject (MainActivity activity);
}


@Singleton
@Component(modules = {MockCopyrightStringModule.class})

public interface CopyrightTestComponent extends MyAppComponent {
void inject(MainActivityTest activityTest);
}

public class Application extends Application {


@Singleton
@Component(modules = {CopyrightStringModule.class})

public interface appComponent extends MyAppComponent {

}

private final MyAppComponent component = createComponent();

protected MyAppComponentcreateComponent() {
    return DaggerMyAppComponent.builder()
            .copyrightStringModule(new CopyrightStringModule())
            .build();
}

public MyAppComponent component() {
    return component;
}


@Singleton
@Component(modules = {MockCopyrightStringModule.class})

public interface CopyrightTestComponent extends MyAppComponent {
void inject(MainActivityTest activityTest);
}

@Module
public class MockCopyrightStringModule {

@Provides
@Singleton
CopyrightString provideGetTerm$Condition(){

    return Mockito.mock(CopyrightString.class);
   }
}

0 个答案:

没有答案