我有4个类让我们说A,B,C,D各自调用另一个类的方法.
现在我嘲笑了A类,想要使用mockito模拟一个方法
A a = Mockito.mock(A.class);
Run Code Online (Sandbox Code Playgroud)
并希望在递归方法调用上得到"foo"
a.getB().getC().getD() 应该回来 "foo"
我试过了
当(a.getB()GETC()GETD().)thenReturn( "foo" 的).
但得到了nullPointerException
然后我试了
doReturn( "富")时(a.getB()GETC()GETD()...).;
然后我得到了 org.mockito.exceptions.misusing.UnfinishedStubbingException:
我知道我可以创建B,C和D的对象,或者甚至可以写出类似的东西
B b = mock(B.class)或A.setB(new B())
等等.
但我不能一次性做到这一点?任何帮助,将不胜感激.
未能调用shutdown()线程执行程序将导致永不终止的应用程序.关闭ExecutorService的最佳做法是:
ExecutorService service = null;
try {
service = Executors.newSingleThreadExecutor();
// add tasks to thread executor
…
} finally {
if (service != null) service.shutdown();
}
Run Code Online (Sandbox Code Playgroud)
由于Java知道try-with-resources概念,如果我们能做到这一点会不会很好?
try (service = Executors.newSingleThreadExecutor())
{
// add tasks to thread executor
…
}
Run Code Online (Sandbox Code Playgroud) 我发现JvisualVM中有两个插件,一个是采样器,另一个是探测器.
我还发现他们有类似的用户界面,但结果有很大差异,那么对他们来说有什么不同呢?
为什么他们有很大的不同?
谁能解释一下:
bowBack()以退出函数bow()- 或)?这是我得到的输出 - 然后程序卡住了!
阿方斯:加斯顿向我鞠躬致敬!
加斯顿:阿尔方斯向我鞠躬致敬!
public class Deadlock {
static class Friend {
private final String name;
public Friend(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public synchronized void bow(Friend bower) {
System.out.format("%s: %s"
+ " has bowed to me!%n",
this.name, bower.getName());
bower.bowBack(this);
}
public synchronized void bowBack(Friend bower) {
System.out.format("%s: %s"
+ " has bowed back to me!%n",
this.name, bower.getName());
}
}
public static void …Run Code Online (Sandbox Code Playgroud) 我创建了一个"生成器"接口(用于方法引用,分别用于单元测试的模拟):
@FunctionalInterface
public interface Factory<R, T, X extends Throwable> {
public R newInstanceFor(T t) throws X;
}
Run Code Online (Sandbox Code Playgroud)
我创建的那样,因为我的第一个用例实际上必须抛出一些检查WhateverException.
但是我的第二个用例没有投掷X.
我能想出的让编译器满意的最好方法是:
Factory<SomeResultClass, SomeParameterClass, RuntimeException> factory;
Run Code Online (Sandbox Code Playgroud)
这编译,并做我需要的,但仍然是丑陋的.有没有办法保留单个接口,但在声明特定实例时不提供X?
据我了解,下面的代码应打印true,因为两者Stream并Iterator指向的第一个元素.
但是,当我运行以下代码时,它正在打印false:
final HashMap<String, String> map = new HashMap<>();
map.put("A", "B");
final Set<Map.Entry<String, String>> set = Collections.unmodifiableMap(map).entrySet();
Map.Entry<String, String> entry1 = set.iterator().next();
Map.Entry<String, String> entry2 = set.stream().findFirst().get();
System.out.println(entry1 == entry2);
Run Code Online (Sandbox Code Playgroud)
不同行为的原因是什么?
我正在寻找一种优雅的方式来创建一个依赖注入工厂.在我的例子中,工厂只需要调用一个参数构造函数.我发现这个答案概述了如何将a Function<ParamType, ClassToNew>用于此类目的.
但我的问题是:在我的情况下,我的ctor声明抛出一些检查异常.
我没有得到:使用对该构造函数的方法引用创建该函数不起作用.如:
import java.util.function.Function;
public class Mcve {
public Mcve(String s) throws Exception {
// whatever
}
public static void main(String[] args) {
Function<String, Mcve> mcveFactory = Mcve::new;
}
}
Run Code Online (Sandbox Code Playgroud)
告诉我"未处理的异常:java.lang.Exception" Mcve::new.虽然这段代码没有调用构造函数.
两个问题:
throws Exception到我main()并没有帮助)我想要对数组中的一些元素进行排序,但排除其他元素.
举一个简单的例子,一个包含整数的数组,我想对奇数进行排序,但将偶数保留在它们的位置.
到目前为止,我有以下内容:
public class MyClass {
public static void main(String args[]) {
int temp;
int array[] = {5, 3, 2, 8, 1, 4};
int[] sortedArray = new int[array.length];
for (int j = 0; j < array.length - 1; j++) {
for (int x = 0; x < array.length - 1; x++) {
if (array[x] > array[x + 1] && array[x] % 2 != 0 && array[x + 1] % 2 !=0) {
temp = array[x];
array[x] = array[x + …Run Code Online (Sandbox Code Playgroud) 我发现了一个建议使用的答案
var list = new ArrayList();
Run Code Online (Sandbox Code Playgroud)
我很惊讶在这里找到一个原始类型,我只是想知道:确实var使用<>"自动?
(与此同时,答案改为使用<String>,但我仍然对这里的"原则"感到好奇)
我看到其他的问题,比如这个,但它们都使用了钻石操作:
var list = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
现在我只想知道:是否会var改变我们应该(不)使用原始类型的方式?或者这个建议<>只是遗漏了不好的做法?
从java内存模型中,我们知道每个线程都有自己的线程堆栈,并且局部变量放在每个线程自己的线程堆栈中.
而其他线程无法访问这些局部变量.
那么在这种情况下,我们应该同步局部变量?