尝试在IntelliJ Idea中运行一些Android JUnit测试时遇到问题.
我的项目是使用Gradle的Android库项目.当我运行我的测试时,IntelliJ会抱怨以下错误:
Class not found: "com.domain.app.ClassTest"
Run Code Online (Sandbox Code Playgroud)
但是ClassTest
存在于测试包中.
这是我的build.gradle:
apply plugin: 'android-library'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
}
}
dependencies {
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
compile 'com.android.support:support-v4:19.1.+'
compile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
}
android {
compileSdkVersion 18
buildToolsVersion "19.0.3"
defaultConfig {
versionName "1.0"
versionCode 1
targetSdkVersion 18
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src/main/java']
res.srcDirs = ['res']
}
androidTest {
java.srcDirs = ['src/test/java'] …
Run Code Online (Sandbox Code Playgroud) 我有一个Android项目(Gradle),我需要包含Joda Time和Commons IO库.这是我的Gradle文件:
apply plugin: 'android-library'
apply plugin: 'android-test'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.+'
}
}
dependencies {
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
androidTestCompile 'com.google.guava:guava:14.0.1',
'com.squareup.dagger:dagger:1.1.0',
'org.hamcrest:hamcrest-integration:1.1',
'org.hamcrest:hamcrest-core:1.1',
'org.hamcrest:hamcrest-library:1.1'
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
androidTestCompile 'org.mockito:mockito-all:1.9.5'
androidTestCompile('org.robolectric:robolectric:2.3') {
exclude module: 'classworlds'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-plugin-registry'
exclude module: 'maven-profile'
exclude module: 'maven-project'
exclude module: …
Run Code Online (Sandbox Code Playgroud) 我正在iOS 7+中的视图控制器之间实现自定义转换.特别是,我在视图控制器A和视图控制器B中有相同的按钮,唯一的区别在于它的位置(它在视图控制器B中略高一些).
我希望在从视图控制器A到视图控制器B的过渡中,来自A的按钮向上移动,使其在视图控制器B中的最终位置结束.
这是我的animateTransition:
方法:
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *containerView = [transitionContext containerView];
[containerView insertSubview:toViewController.view belowSubview:fromViewController.view];
UIButton *fromButton = (UIButton *)[containerView viewWithTag:1];
UIButton *toButton = (UIButton *)[containerView viewWithTag:2];
toButton.hidden = YES;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromButton.center = toButton.center;
} completion:^(BOOL finished) {
toButton.hidden = NO;
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
}];
}
Run Code Online (Sandbox Code Playgroud)
我看到的问题是,这toButton.center
不是正确的位置.就像AutoLayout约束还没有被应用(我得到与IB中相同的框架矩形).这导致在转换完成后按钮不会在正确的位置结束.
哪里出错?如何从呈现的视图控制器中获取正确的子视图最终帧?如果由于视图控制器B尚未出现而无法实现,那么我如何设置子视图动画才能实现此效果?
我正在使用OCMockito,我想在我的ViewController中测试一个使用NetworkFetcher对象和块的方法:
- (void)reloadTableViewContents
{
[self.networkFetcher fetchInfo:^(NSArray *result, BOOL success) {
if (success) {
self.model = result;
[self.tableView reloadData];
}
}];
}
Run Code Online (Sandbox Code Playgroud)
特别是,我想要模拟,fetchInfo:
以便它返回一个虚拟result
数组而不会访问网络,并验证该reloadData
方法是否被调用,UITableView
并且模型应该是它应该是什么.
由于此代码是异步的,我假设我应该以某种方式捕获块并从我的测试中手动调用它.
我怎么能做到这一点?
我有一个活动,向用户显示内的产品列表HorizontalScrollView
。我正在使用Robolectric测试产品列表较长时滚动视图是否显示箭头。
在我的活动中,我这样注册OnGlobalLayoutListener
:
ViewTreeObserver vto = productsScrollView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
productsScrollView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
maxScrollX = productsScrollView.getChildAt(0)
.getMeasuredWidth() - productsScrollView.getMeasuredWidth();
hideShowArrowsAsNeeded();
}
});
Run Code Online (Sandbox Code Playgroud)
当productsScrollView
视图层次结构更改时(即,我已经向其中添加了一些子视图),将调用此侦听器。我运行该应用程序,并且一切正常。
问题是我不知道如何测试该hideShowArrowsAsNeeded
方法是否能完成工作。在我的Robolectric测试中,我有以下内容:
@Test
public void testThatAHugeNumberOfProductsShowRightArrow() throws Exception {
Product product1 = new Product();
product1.setName("Product1");
Product product2 = new Product();
product2.setName("Product2");
...
ArrayList<Product> productsList = new ArrayList<Product>();
productsList.add(product1);
productsList.add(product2);
...
activity.drawProducts(productsList); // Here I add the views to the scroll view and onGlobalLayout is eventually called, but now …
Run Code Online (Sandbox Code Playgroud) android ×3
ios ×2
asynchronous ×1
gradle ×1
java ×1
jodatime ×1
junit ×1
ochamcrest ×1
ocmockito ×1
robolectric ×1
unit-testing ×1