在我的程序中,我从一个可运行的程序中创建了一个断言——它的计算结果为假,但从未看到任何关于该断言的控制台输出。我想知道我的断言是否为假,但似乎 runnable 正在捕获所有抛出的断言?
下面是我可以编写的最简单的示例程序来演示。 (断言已启用。如果未启用断言,程序将表现不同,并打印两行而不是仅打印一行)。 程序的输出是。
即将断言为假
就是这样。在那之后,断言语句抛出并被某些东西捕获,我从来不知道。我想知道,我做错了什么?
import java.nio.ByteBuffer;
import java.util.concurrent.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import javax.swing.*;
class App
{
private static final ScheduledExecutorService sExecutor =
Executors.newSingleThreadScheduledExecutor();
// Main
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { createAndShowGUI(); } });
}
// Swing GUI
private static void createAndShowGUI()
{
// Just create a swing thing. Boring
JFrame frame = new JFrame("Title String");
JLabel label = new JLabel("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.getContentPane().setLayout(new FlowLayout());
frame.pack();
frame.setVisible(true);
// ********************************************
// INTERESTING CODE HERE. We schedule a runnable which assert's false
// but we never see a console assert error!
// ********************************************
sExecutor.schedule(new Runnable()
{ @Override public void run() { doAssertFalse(); }}, 0, TimeUnit.SECONDS);
}
public static void doAssertFalse()
{
System.out.println("About to assert False");
assert false;
System.out.println("Done asserting False");
}
}
最佳答案
这样做就可以了:
private static final ScheduledExecutorService sExecutor =
Executors.newSingleThreadScheduledExecutor();
// Main
public static void main(String[] args)
{
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
createAndShowGUI();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
} });
}
// Swing GUI
private static void createAndShowGUI() throws ExecutionException, InterruptedException {
// Just create a swing thing. Boring
JFrame frame = new JFrame("Title String");
JLabel label = new JLabel("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.getContentPane().setLayout(new FlowLayout());
frame.pack();
frame.setVisible(true);
// ********************************************
// INTERESTING CODE HERE. We schedule a runnable which assert's false
// but we never see a console assert error!
// ********************************************
ScheduledFuture<?> future = sExecutor.schedule(new Runnable() {
@Override
public void run() {
doAssertFalse();
}
}, 0, TimeUnit.SECONDS);
future.get();
}
public static void doAssertFalse()
{
System.out.println("About to assert False");
assert false;
System.out.println("Done asserting False");
}
请注意,我正在将 schedule 的结果保存到 ScheduledFuture 变量中。在您将来调用 get() 方法之前,不会返回异常。所有异常都包含在 ExecutionException 中抛出。
不幸的是,这会阻塞,所以另一种获取异常的方法是这样的:
// Swing GUI
private static void createAndShowGUI() {
// Just create a swing thing. Boring
JFrame frame = new JFrame("Title String");
JLabel label = new JLabel("Hello World");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label);
frame.getContentPane().setLayout(new FlowLayout());
frame.pack();
frame.setVisible(true);
// ********************************************
// INTERESTING CODE HERE. We schedule a runnable which assert's false
// but we never see a console assert error!
// ********************************************
sExecutor.schedule(new Runnable() {
@Override
public void run() {
try {
doAssertFalse();
} catch (Error e) {
e.printStackTrace();
}
}
}, 0, TimeUnit.SECONDS);
}
public static void doAssertFalse() {
System.out.println("About to assert False");
assert false;
System.out.println("Done asserting False");
}
请注意,我捕获的是错误,而不是异常。我这样做是因为断言抛出 java.lang.AssertionError,而不是 *Exception。
我很难在 Javadoc 中找到任何说明 ScheduledExecutorService 吞下异常的文档,除非您执行这些操作,但通过我的测试似乎是这样。
关于Java 断言从可运行对象调用时不发送到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14344901/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
当我在Rails控制台中按向上或向左箭头时,出现此错误:irb(main):001:0>/Users/me/.rvm/gems/ruby-2.0.0-p247/gems/rb-readline-0.4.2/lib/rbreadline.rb:4269:in`blockin_rl_dispatch_subseq':invalidbytesequenceinUTF-8(ArgumentError)我使用rvm来管理我的ruby安装。我正在使用=>ruby-2.0.0-p247[x86_64]我使用bundle来管理我的gem,并且我有rb-readline(0.4.2)(人们推荐的最少
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.
我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新rubygems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems