We've found the "Fail Fast" principle crucial for improving maintainability of our large Fitnesse-based battery of tests. Slim's StopTestException is our saviour.
However, it's very cumbersome and counterproductive to catch and convert any possible exception to those custom StopExceptions. And this approach doesn't work outside of fixtures. Is there a way to tell fitnesse (preferably using Slim test system) to stop test on any error / exception?
Update: corresponding feature request https://github.com/unclebob/fitnesse/issues/935
Most of the exceptions coming from fixtures are possible to conveniently convert to the StopTestException by implementing the FixtureInteraction interface, e.g.:
public class StopOnException extends DefaultInteraction {
#Override
public Object newInstance(Constructor<?> constructor, Object... initargs) throws InvocationTargetException, InstantiationException, IllegalAccessException {
try {
return super.newInstance(constructor, initargs);
} catch (Throwable e) {
throw new StopTestException("Instantiation failed", e);
}
}
#Override
public Object methodInvoke(Method method, Object instance, Object... convertedArgs) throws InvocationTargetException, IllegalAccessException {
try {
return super.methodInvoke(method, instance, convertedArgs);
} catch (Throwable e) {
throw new StopTestException(e.getMessage(), e);
}
}
public static class StopTestException extends RuntimeException {
public StopTestException(String s, Throwable e) {
super(s, e);
}
}
}
Related
I created a custom exception called DepositeException and trying to access its custom exception message (errorMessage) in main method but it's throwing error. What might be the problem in following code.
void main() {
try {
depositAmount(-100);
} catch (e) {
print(e.errorMessage());
}
}
class DepositException implements Exception {
String errorMessage() {
return "you cannot enter amount less then 0";
}
}
void depositAmount(int amount) {
if (amount < 0) {
throw new DepositException();
}
}
This happen because ~~exception catching in Dart is unchecked~~ the exception type is unspecified so it will return Object (thanks to #jamesdlin for the correction).
To catch the custom exception, you need specify the type like this:
try{
depositAmount(-100);
} on DepositException catch (e){
print(e.errorMessage());
}
Reference:
How to create a custom exception and handle it in dart
I am new to dart and I am learning dart from youtube. And courses that I am following are of 2018. The programs that they created in their videos are not working. I am facing the below issue in all my programs. Anyone, please guide me that why the programs show errors while the programs are running properly in their videos. Is it happening due to an update in dart? or any other reason? Please help to fix this issue. Thanks!
The function 'errorMessage' isn't defined.
Try importing the library that defines 'errorMessage', correcting the name to the name of an existing function, or defining a function named 'errorMessage'.
class CustomException implements Exception {
String errorMessage() {
return ("Invalid Amount");
}
}
void AmountException(int amount) {
if (amount <= 0) {
throw new CustomException();
}
}
void main() {
try {
AmountException(0);
} catch (e) {
print(errorMessage());
}
}
You are not calling the errorMessage() message on the exception. Another problem is that your catch is set to handle all types of exceptions. Since Exception does not have the errorMessage() method, you cannot call it.
You should therefore specify the type of exception you want to catch which will allow you to call the errorMessage() method on the catched exception:
class CustomException implements Exception {
String errorMessage() {
return ("Invalid Amount");
}
}
void AmountException(int amount) {
if (amount <= 0) {
throw new CustomException();
}
}
void main() {
try {
AmountException(0);
} on CustomException catch (e) {
print(e.errorMessage());
}
}
I've tried to use a watchService as a Flux generator and it couldn't work, and I also tried some simple block like Thread.sleep in the Flux.create method and it could work.
I wonder why and what's the difference between these situations?
Code which could work,
#Test
public void createBlockSleepTest() throws InterruptedException {
Flux.create(sink->{
while (true) {
try {
for(int i=0;i<2;i++)
sink.next(num++);
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).log().subscribeOn(Schedulers.parallel(),false).log()
.subscribe(System.out::println);
Thread.sleep(100000L);
}
Code which couldn't work,
#Test
public void createBlockTest() throws IOException, InterruptedException {
WatchService watchService = fileSystem.newWatchService();
Path testPath = fileSystem.getPath("C:/testing");
Files.createDirectories(testPath);
WatchKey register = testPath.register(watchService, StandardWatchEventKinds.ENTRY_CREATE,StandardWatchEventKinds.ENTRY_MODIFY);
Files.write(testPath.resolve("test1.txt"),"hello".getBytes());
Thread.sleep(5000L);
Flux.create(sink->{
while (true) {
try {
WatchKey key = watchService.take();
System.out.println("-----------------"+key);
for(WatchEvent event:key.pollEvents()){
sink.next(event.context());
}
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).log().subscribeOn(Schedulers.parallel(),false).log()
.subscribe(System.out::println);
Files.write(testPath.resolve("test2.txt"),"hello".getBytes());
Thread.sleep(5000L);
Files.write(testPath.resolve("test3.txt"),"hello".getBytes());
Thread.sleep(10000L);
}
I've noticed in the reactor's reference there is a notice for blocking in the create method. But why Thread.sleep works?
create doesn’t parallelize your code nor does it make it asynchronous, even
though it can be used with asynchronous APIs. If you block within the create lambda,
you expose yourself to deadlocks and similar side effects. Even with the use of subscribeOn,
there’s the caveat that a long-blocking create lambda (such as an infinite loop calling
sink.next(t)) can lock the pipeline: the requests would never be performed due to the
loop starving the same thread they are supposed to run from. Use the subscribeOn(Scheduler, false)
variant: requestOnSeparateThread = false will use the Scheduler thread for the create
and still let data flow by performing request in the original thread.
Could anyone solve my puzzle?
This can be fixed by changing
while (true) {
try {
WatchKey key = watchService.take();
System.out.println("-----------------"+key);
for(WatchEvent event:key.pollEvents()){
sink.next(event.context());
}
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
to
while (true) {
try {
WatchKey key = watchService.take();
System.out.println("-----------------"+key);
for(WatchEvent event:key.pollEvents()){
sink.next(event.context());
}
key.reset();
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Thread.sleep(5000L) will only block for 5s, so the create will move on after that delay, whereas WatchService#take blocks indefinitely unless a new WatchKey registers (in this case a new file). Since the code that creates files is after the create, there is a deadlock situation.
I'm calling in a function to step (specflow),
Then(step's string)
and in step I throw an exception. I want to catch the exception with a different function and not in the step itself. Do someone know how to do it?
Thanks
This is not possible with SpecFlow.
SpecFlow interprets an exception in an step as error and stops the execution of the Scenario.
What you can do, is to catch the exception in your step and save it in a field of the binding class. Then in the second step you can check this field.
like this:
[Binding]
public class BindingClass
{
private Exception _exception;
[When("an exception is thrown")
public void ExceptionThrown()
{
try {
.... //your code that throws an exception
}
catch(Exception e)
{
_exception = e;
}
}
[Then("the exception has the message '(.*)'")]
public void ExceptionHasTheMessage(string message)
{
if (_exception != null)
{
Assert.Equal(_exception.Message, message);
}
}
}
I use the AfterStep hook available in SpecFlow.
The code looks like this:
[AfterStep]
public void AfterStep()
{
if(_scenarioContext.ScenarioExecutionStatus.ToString().Equals("TestError"))
{
Logger.LogScreenshot(exception, _scenarioContext.ScenarioInfo.Title);
}
}
This piece of code will catch your exception and remaining steps will be skipped.
After updating to the latest Android Things preview, my app is crashing when
setting a callback on by button GPIO. I have the following button callback defined:
private class ButtonCallback extends GpioCallback {
#Override
public boolean onGpioEdge(Gpio gpio) {
boolean isPressed = false;
try {
isPressed = gpio.getValue();
} catch (IOException e) {
Log.w(TAG, "Error", e);
}
if (isPressed) {
...
}
return true;
}
}
I am registering it with the GPIO in the application as follows:
Gpio button = ...;
try {
button.registerGpioCallback(new ButtonCallback());
} catch (IOException e) {
Log.w(TAG, "Error configuring GPIO pins", e);
}
When I run my app, I get an IncompatibleClassChangeError and the app crashes:
java.lang.IncompatibleClassChangeError: Superclass com.google.android.things.pio.GpioCallback of com.google.android.things.example.MainActivity$ButtonCallback is an interface (...)
This code was working before, why has this started happening after the update?
Starting in Preview 7, many of the Peripheral I/O interfaces were converted from
abstract classes to interfaces. This was done to better facilitate testability
in apps, as interfaces are easier to mock.
Be sure to update your app to use the Preview 7 SDK:
dependencies {
compileOnly 'com.google.android.things:androidthings:0.7-devpreview'
}
Then modify your callback to implement the interface instead:
private class ButtonCallback implements GpioCallback {
#Override
public boolean onGpioEdge(Gpio gpio) {
boolean isPressed = false;
try {
isPressed = gpio.getValue();
} catch (IOException e) {
Log.w(TAG, "Error", e);
}
if (isPressed) {
...
}
return true;
}
}
Review the Android Things API reference
to verify if any of the other APIs you are calling have changed.