我尝试将junit,mokito和powermock设置在一起,但是当我运行测试时,我得到了ClassNotFoundException :(
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.7.22'
androidTestCompile 'org.mockito:mockito-core:2.7.22'
androidTestCompile "org.mockito:mockito-android:2.7.22"
testCompile 'org.robolectric:robolectric:3.3.2'
testCompile 'org.hamcrest:hamcrest-core:1.3'
testCompile 'org.powermock:powermock-core:1.6.6'
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
testCompile 'org.powermock:powermock-module-junit4-rule:1.6.6'
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
testCompile 'org.powermock:powermock-classloading-xstream:1.6.6'`
Run Code Online (Sandbox Code Playgroud)
我还尝试添加cglib:
testCompile 'cglib:cglib:3.1'
testCompile 'cglib:cglib-nodep:3.1'
Run Code Online (Sandbox Code Playgroud)
但没有缺乏.
任何人都可以分享工作配置或指出我的错误.
我运行测试时的堆栈跟踪:
Exception in thread "main" java.lang.NoClassDefFoundError: org/mockito/exceptions/Reporter
at sun.reflect.GeneratedSerializationConstructorAccessor5.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:48)
at org.powermock.reflect.internal.WhiteboxImpl.newInstance(WhiteboxImpl.java:251)
at org.powermock.reflect.Whitebox.newInstance(Whitebox.java:139)
at org.powermock.api.extension.reporter.AbstractMockingFrameworkReporterFactory.getInstanceForClassLoader(AbstractMockingFrameworkReporterFactory.java:41)
at org.powermock.api.extension.reporter.AbstractMockingFrameworkReporterFactory.create(AbstractMockingFrameworkReporterFactory.java:35)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.getMockingFrameworkReporter(JUnit4TestSuiteChunkerImpl.java:140)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:119)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498) …Run Code Online (Sandbox Code Playgroud) 我开发了基于android数据绑定库的应用程序:https://developer.android.com/topic/libraries/data-binding/index.html
class SignInViewModel extends BaseObservable {
@Bindable
public String getLogin() {
return login;
}
@Bindable
public String getPassword() {
return password;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想使用新库中的ViewModelProviders:https: //developer.android.com/topic/libraries/architecture/guide.html
SignInViewModel signInViewModel = ViewModelProviders.of(this).get(SignInViewModel.class);
Run Code Online (Sandbox Code Playgroud)
它如何结合?任何的想法?或者应该结合这两个库?
编辑
我改为:
class SignInViewModel extends ViewModel {
public ObservableField<String> login = new ObservableField<>("");
public ObservableField<String> password = new ObservableField<>("");
}
Run Code Online (Sandbox Code Playgroud)
现在编译,但问题是:它是正确的方式?
我有两种类型FooApi和FooModel:
class FooApi (var aId)
class FooModel(var mId)
Run Code Online (Sandbox Code Playgroud)
是一种简化以下FooModel基于FooApi列表过滤列表的功能的方法:
fun f(fooModelList: List<FooModel>, fooApiList: List<FooApi>) : List<FooModel> {
return fooModelList.filter { fooApiList.map { it.aId }.contains ( it.mId ) }
}
Run Code Online (Sandbox Code Playgroud) 如果我有如下布局,如何使用kotlin合成扩展访问视图:
文件:two_days_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="@+id/day1"
layout="@layout/day_row"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
android:id="@+id/day2"
layout="@layout/day_row"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
file:day_row.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/dayName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
如何访问dayName?我找了一些这样的:
day1.dayName.text = "xxx"
day2.dayName.text = "sss"
Run Code Online (Sandbox Code Playgroud)
我在Studio中看到我有权访问dayName但是dayName TextView引用了哪一个?
正常,如果我只有一个包含的布局,它工作正常.但现在我有多次包含相同的布局.
我当然可以这样做:
day1.findViewById(R.id.dayName).text = "xxx"
Run Code Online (Sandbox Code Playgroud)
但我正在寻找好的解决方案.:)
我试图弄清楚如何以最佳方式完成ViewModel中的Activity.我找到了使用LiveData对象并发出"信号"的一种方法.
我怀疑这个解决方案有一个开销.那么它是正确的解决方案还是我应该使用更准确?
所以举个例子:让我们假设在一个应用程序中是一个活动MainActivity和视图模型,如下所示:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val model = ViewModelProviders.of(this).get(MainViewModel::class.java)
model.shouldCloseLiveData.observe(this, Observer { finish() })
}
}
Run Code Online (Sandbox Code Playgroud)
并且作为MainActivity的伴侣是MainViewModel,如下所示:
class MainViewModel(app: Application) : AndroidViewModel(app) {
val shouldCloseLiveData = MutableLiveData<Void>()
fun someAction(){
shouldCloseLiveData.postValue(null)
}
}
Run Code Online (Sandbox Code Playgroud) We are trying to implement paging in Leanback VerticalGridSupportFragment with Architecture Components Paging Library. Leanback on it's own doesn't have any sort of out-of-box compatibility with Paging Library so we extended it's ObjectAdapter class and managed to implement append and clear operations quite easily but we are having a hard time trying to make modify operation work. During content modification operation, Paging Library's PagedList class computes the diff using AsyncPagedListDiffer which internally uses PagedStorageDiffHelper which is a package-private class and …
我需要将对象序列化为String并反序列化.
我在stackoverflow上重写sugestion并生成以下代码:
class Data implements Serializable {
int x = 5;
int y = 3;
}
public class Test {
public static void main(String[] args) {
Data data = new Data();
String out;
try {
// zapis
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(data);
out = new String(baos.toByteArray());
System.out.println(out);
// odczyt.==========================================
ByteArrayInputStream bais = new ByteArrayInputStream(out.getBytes());
ObjectInputStream ois = new ObjectInputStream(bais);
Data d = (Data) ois.readObject();
System.out.println("d.x = " + d.x);
System.out.println("d.y = " + d.y); …Run Code Online (Sandbox Code Playgroud) 我有 3 行代码可以从 OkHttp3 源获取正文:
val responseBody = response.peekBody(response.body()!!.contentLength())
val source = GzipSource(responseBody.source())
val body = Okio.buffer(source).readUtf8() //issue is that line
Run Code Online (Sandbox Code Playgroud)
在另一台计算机上,我收到错误消息:“使用 'buffer(Source): BufferedSource' 是一个错误。移动到扩展功能”
所以通过替换最后一行来修复它:
val body = source.buffer().readUtf8()
Run Code Online (Sandbox Code Playgroud)
现在在第一台计算机上的 bun 我有错误:“未解析的引用:缓冲区”所以我需要恢复该更改。
怎么了?根据错误消息我无法弄清楚。似乎是 gradle 配置的问题。但是什么?如何在两台计算机上编译代码。
我尝试为 google 制作视差滚动效果MapView并RecycleView使用CoordinatorLayour.
所以基于在网上找到的一些教程,我做了下面的代码。
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.gms.maps.MapView android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="200dp"
app:layout_behavior="net.lunavulpo.coordinatorlayouttest.MapViewBehavior"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_marginTop="200dp"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
我实现了CoordinatorLayout.Behavior:
public class MapViewBehavior extends CoordinatorLayout.Behavior<MapView> {
public MapViewBehavior(Context context, AttributeSet attrs) {
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, MapView child, View dependency) {
return true;
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, MapView child, View directTargetChild, View target, int nestedScrollAxes) {
return true;
}
@Override …Run Code Online (Sandbox Code Playgroud) android google-maps-android-api-2 android-design-library androiddesignsupport android-coordinatorlayout
我将当前的整体模块拆分为几个模块::app、:ui、:base。
我:ui使用模块中的类在模块中编写了测试:base。
class FooTest : BaseTest {
@Test
fun fooTest() {}
}
Run Code Online (Sandbox Code Playgroud)
soBaseTest在:base模块中,但 :ui 模块依赖于:base. 所以 AS 向我表明这是可以的。
在 :ui 模块的 gradle 文件中:
dependences {
implementation project(":base")
}
Run Code Online (Sandbox Code Playgroud)
但是当测试运行时我得到:
BaseTest: Unresolved reference: BaseTest
Run Code Online (Sandbox Code Playgroud)
我尝试添加:
testImplementation project(":base")
Run Code Online (Sandbox Code Playgroud)
或者
androidTestImplementation project(":base")
Run Code Online (Sandbox Code Playgroud)
但不能解决问题。:(
android ×7
java ×2
kotlin ×2
android-architecture-components ×1
android-tv ×1
data-binding ×1
filter ×1
junit ×1
junit4 ×1
leanback ×1
list ×1
mockito ×1
okio ×1
powermock ×1
string ×1