我是Mockito的新手.
鉴于下面的类,我如何使用Mockito来验证someMethod
在调用之后是否被调用了一次foo
?
public class Foo
{
public void foo(){
Bar bar = new Bar();
bar.someMethod();
}
}
Run Code Online (Sandbox Code Playgroud)
我想进行以下验证通话,
verify(bar, times(1)).someMethod();
Run Code Online (Sandbox Code Playgroud)
在哪里bar
是一个模拟的实例Bar
.
在实践中,是能够更好地返回像一个空列表此:
return Collections.emptyList();
Run Code Online (Sandbox Code Playgroud)
或者像这样:
return new ArrayList<Foo>();
Run Code Online (Sandbox Code Playgroud)
或者这完全取决于你要对返回的列表做什么?
假设我有一个利用Executor
框架的应用程序
Executors.newSingleThreadExecutor().submit(new Runnable(){
@Override
public void run(){
// do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
当我在调试器中运行此应用程序时,将使用以下(默认)名称创建一个线程:Thread[pool-1-thread-1]
.正如您所看到的,这不是非常有用,据我所知,该Executor
框架没有提供一种简单的方法来命名创建的线程或线程池.
那么,如何为线程/线程池提供名称呢?例如,Thread[FooPool-FooThread]
.
java executorservice threadpool runnable managedthreadfactory
我糊涂了.在绊倒这个线程之后,我试图弄清楚如何格式化具有该格式的倒数计时器hh:mm:ss
.
这是我的尝试 -
//hh:mm:ss
String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
Run Code Online (Sandbox Code Playgroud)
所以,当我尝试一个类似的值3600000ms
,我得到01:59:00
,这是错误的,因为它应该是01:00:00
.显然我的逻辑有问题,但此刻,我看不出它是什么!
有人可以帮忙吗?
编辑 -
固定它.这是格式化毫秒hh:mm:ss
格式的正确方法-
//hh:mm:ss
String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))));
Run Code Online (Sandbox Code Playgroud)
问题是这个TimeUnit.MINUTES.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))
.应该是这样的TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))
.
在Java中,我希望能够执行Windows命令.
有问题的命令是netsh
.这将使我能够设置/重置我的IP地址.
请注意,我不想执行批处理文件.
我想直接执行这些命令,而不是使用批处理文件.这可能吗?
这是我实现的未来参考解决方案:
public class JavaRunCommand {
private static final String CMD =
"netsh int ip set address name = \"Local Area Connection\" source = static addr = 192.168.222.3 mask = 255.255.255.0";
public static void main(String args[]) {
try {
// Run "netsh" Windows command
Process process = Runtime.getRuntime().exec(CMD);
// Get input streams
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// Read command standard output
String s;
System.out.println("Standard output: ");
while …
Run Code Online (Sandbox Code Playgroud) 有哪些推荐的方法可以实现线程安全的延迟初始化?例如,
// Not thread-safe
public Foo getInstance(){
if(INSTANCE == null){
INSTANCE = new Foo();
}
return INSTANCE;
}
Run Code Online (Sandbox Code Playgroud) 以下是build.gradle文件的内容:
apply plugin: 'java'
archivesBaseName = 'foo-bar'
version = '1.0'
sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6
sourceSets.main.resources.exclude 'foo.jks'
compileJava{
println project.sourceCompatibility
println project.targetCompatibility
println sourceCompatibility
println targetCompatibility
}
Run Code Online (Sandbox Code Playgroud)
以下是运行Gradle jar任务的结果:
[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks:
[sts] :jar
[sts] -----------------------------------------------------
1.6
1.6
1.6
1.6
:compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
:processResources UP-TO-DATE
:classes
:jar
BUILD SUCCESSFUL
Total time: 1 mins 3.072 secs
[sts] -----------------------------------------------------
[sts] …
Run Code Online (Sandbox Code Playgroud) 当我使用JDK 7在eclipse indigo中编译我的项目时,我得到以下错误对话框
使用以下堆栈跟踪
Exception in thread "main" java.lang.VerifyError: Expecting a stackmap frame at branch target 32 in method ... at offset 0
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
我在这里发现了一个相关的bug 并使用了建议的使用jvm选项的解决方法,-XX:-UseSplitVerifier
尽管它有效,但这个bug仍然让我感到困惑.
有谁知道为什么会这样,为什么解决方法......有效?
该项目使用JDK 6进行编译.
我已经按照官方的安装说明,这里适用于Windows XP.但有时当我执行时mvn --version
,我收到错误消息,
'mvn'不被认为是内部或外部命令
我甚至重新启动了我的机器几次,但操作系统并不总能识别maven命令.但是当我执行时cd %M2_HOME%
,或者cd %M2%
它将我带到安装目录.我还确保将用户变量添加M2
到Path
系统变量中.
那么,是什么给出的?
我正试图找出如何在按下按钮时停止运行SwingWorker线程.我一直在四处寻找,我在解决如何做到这一点时遇到了一些麻烦.目前这就是我所拥有的:
new MySwingWorkerClass(args).execute();
Run Code Online (Sandbox Code Playgroud)
我正在创建一个按钮,我想用它来停止线程:
button = new JButton("Stop");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
// Stop the swing worker thread
}
});
Run Code Online (Sandbox Code Playgroud)
我已经环顾四周寻找答案,到目前为止我已经设法找到了取消方法.我不明白如何使用它来阻止我的摇摆工作者.我尝试了以下但它不起作用:
SwingWorker.cancel(true);
Run Code Online (Sandbox Code Playgroud) java ×10
eclipse ×2
apache ×1
collections ×1
empty-list ×1
gradle ×1
java-7 ×1
junit ×1
jvm ×1
maven ×1
mockito ×1
runnable ×1
runtime.exec ×1
swing ×1
swingworker ×1
threadpool ×1
time-format ×1
unit-testing ×1
windows ×1
windows-xp ×1