I am having difficulty using junit 4's expected annotation to look at exceptions. I can't compile the code because there's an unhandled exception.
Here's a simple example that creates the situation:
import static org.junit.Assert.*;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
public class Simple {
#Test(expected=UnsupportedEncodingException.class)
public void simpleTest(){
String a = "";
a.getBytes("UTF-123");
}
}
I get a compilation error saying "Unhandled exception type UnsupportedEncodingException"
This makes sense, and I can fix this by declaring that simpleTest throws UnsupportedEncodingException but I've seen lots of examples online where people don't do that (which would be nice, when writing lots of test cases).
Is there a way to configure the test case so that I don't have to explicitly declare what exceptions will be thrown?
As far as I know, UnsupportedEncodingException is a checked exception. So, compiler would expect a throws clause for a checked exception. I guess your code will work, if say you tried with an unchecked exception like ArithmeticException.
Related
I have the following Junit test
#Before
public void setup() {
UUTClass myObject = new UUTClass();
}
#After
public void teardown() {
// cleanup
}
#Test
public void testSomeMethod() {
myObject.invokeSomeMethod(); // This is throwing NPE from somewhere inside my UUTClass
}
I don't want to use expected=NullPointerException.class what I want is log the stack trace from my UUTClass. All I can see is a NullPointerException one liner in my test method.
I am using Log4j to log everything and I have got log4j.xml in the search path which does get picked up by Initializer. I can see my logger messages being printed for other items.
How to enable Junit to return/propagate the full stack trace for NPE that's been thrown by my UUTClass? I believe it's probably because the errors are redirected to error console and perhaps not being picked up by log4j (don't know if I explained that right).
As a workaround, I will probably use printStackTrace() for now. but ideally I would like to log it if possible.
My Intention is the first understand where in the design I have missed out conditions/constraints which resulted into this NPE and then build more tests to expect certain exceptions.
Regards,
I am wondering what the best practise approach is to throwing/handling exceptions between Actors and clients within the service fabric.
I noticed for example that when an exception was thrown from an Actor the client received a nested set of exceptions. The outer exception is of type System.AggregateException and merely indicates that "One or more errors occurred". But if you drill down to the actual inner exceptions, you see that the exception that was thrown from the Actor cannot be serialized.
Test method PoCSystem.Test.CommandHandlerTest.CommandHandler_When_ExpectExceptionThrown threw exception:
System.AggregateException: One or more errors occurred. ---> System.AggregateException: One or more errors occurred. --->
System.AggregateException: One or more errors occurred. ---> System.ServiceModel.FaultException`1[System.ServiceModel.ExceptionDetail]:
Type 'PoCActor.Interfaces.ConcurrencyException' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.
If you then mark the Exception with DataContract and DataMember attributes the Exception then complains that it is not compatible with serialization.
What is the best approach for error handling in the service fabric?
Consider using old-school .net serializability. From the "Exception" code snippet built in to visual studio:
[Serializable]
public class MyException : Exception
{
public MyException() { }
public MyException(string message) : base(message) { }
public MyException(string message, Exception inner) : base(message, inner) { }
protected MyException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context)
: base(info, context) { }
}
I am facing one problem while running the Junit scripts. I am getting the below error message.
I have three java classes under which i have commented all the #Test annotations from Class A and Class B but have four #Test annotations in Class C..but still it is showing the below error message.
Can anyone help me how to fix this issue?
Error: java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:169)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:104)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:355)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.Parameterized$TestClassRunnerForParameters.<init>(Parameterized.java:171)
at org.junit.runners.Parameterized.createRunnersForParameters(Parameterized.java:319)
at org.junit.runners.Parameterized.<init>(Parameterized.java:282)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
Can anyone please help here?
Ensure that all of your relevant test case objects are public and non-abstract, and that your test methods are annotated with #Test but are also public void and non-static.
See more at the JUnit Getting Started Guide.
Make sure you have this import:
import org.junit.Test;
instead of:
import org.junit.jupiter.api.Test;
As well as the following annotations on test class if your project is SpringBoot 2:
#SpringBootTest
#RunWith(SpringRunner.class)
I faced the same issue "Error: java.lang.Exception: No runnable methods" in my simple Spring-boot project. I resolved the issue by doing the following steps.
Make sure that the the test class is in src/test/java folder. In my project settings the Test class was in src/test folder.
Make sure that src/test/java is in Java Build Path. Go to Project->properties->Java Build Path ->source and add src/test/java folder.
Test class should be in same package as the source Java class with main method.
Make sure you import the right package :
import org.junit.runners.Parameterized;
and not the testng package
I'm talking about this error:
ERROR 1046: Type was not found or was not a compile-time constant: CustomType
Where CustomType is defined in a class in an .as file. I know you need to import that file in order to avoid this error, but my question is, is there any error handler for this error?
I mean, is there a way to say, for example:
onErrorHandler {
trace("You need to import the class!");
} else {
private var myObject:CustomType;
}
That error is a thrown at compile time, so no.
The import statement tells Flash what classes need to be compiled for your current class to function as expected. You can't write code to catch an error that is thrown at compile-time, because it doesn't compile and thus doesn't get executed.
It is the compiler's responsibility to catch these errors, not yours.
I externalized some strings to HOCON, on application.conf. I'm accessing the configuration values like this:
import play.api.Play.current
import play.api.Play.configuration
configuration.getString("foo.bar").get()
As early as possible, to fail fast in case of a missing key, like the docs say.
Now some of my tests that depend on configured objects are failing with a stacktrace that states:
Caused by: java.lang.RuntimeException: There is no started application
I assume this has to do with the configuration? How can I fix this? (tests are specs2)
Do you have a FakeApplication running? As stated in the documents: http://www.playframework.com/documentation/2.0/JavaTest before you run the test/ test method?
Example from the Wiki:
#Test
public void findById() {
running(fakeApplication(), new Runnable() {
public void run() {
Computer macintosh = Computer.find.byId(21l);
assertThat(macintosh.name).isEqualTo("Macintosh");
assertThat(formatted(macintosh.introduced)).isEqualTo("1984-01-24");
}
});
}
If this is not solving your issue, perhaps providing more information from the Stacktrace would help.
EDIT: Please tag your question carefully, it does not make sense to mention playframework AND playframework-2.0