Unit Tests Run Twice in IntelliJ - junit

I'm new to IntelliJ. I have some unit tests written in JUnit. For some reason, each tests runs twice. For the life of me, I can't figure out why. Can someone please provide some pointers in regards to why this might be happening. I would really like to get this down to a predicatable state.
Thanks

For example if you have a method annotated both with #Before and #Test annotations it will run twice:
#Test
#Before
public void testConnection()
{
// ...
}

I ran into an apparent (but only apparent) case of this as well. I was logging something at the beginning of the test and then the log message was showing up twice in the console, which made it look like the test was running twice.
The problem was that I had log4j misconfigured. My root logger was like this:
log4j.rootLogger=INFO, stdout
But my package-specific logger was like this:
log4j.logger.com.mycompany.myapp=TRACE, stdout
Log4j additivity resulted in the double-log.
The fix was simply to update my package-specific logger as follows:
log4j.logger.com.mycompany.myapp=TRACE
Hope this helps somebody out. This was aggravating.

Related

JUnit Test is not returning the full StackTrace from UUT Class

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,

How can you unit test Apache Cache Timer routes?

Using Apache Camel 2.9.1
How do I unit test something like the following?
public class MyRoute extends RouteBuilder {
#Override
public void configure() throws Exception {
from("timer.something?delay=0?repeatCount=1")
// do some stuff
.to("{{some.endpoint}}")
.end()
from("timer.somethingelse?delay=3000&period=1000")
// do some stuff
.to("{{some.other.endpoint}}")
.end
}
}
What is exactly that you want to unit test here?
Because it's clueless to unit test the timer component (I mean to unit test if it's triggered or not; and if its properties works as it should be): Camel team has done that already.
What is logical to unit test here is the "// do some stuff" part, which you'd do by mocking the endpoints. Your first route will be fired automaticly, while the second will with initial delay. You'll have to wait that much at least to assert anything. In these kind of cases I usually read the endpoint properties from a properties files like
from("timer:somethingelse?{{2nd.timer.properties}}")
and that can be set to
2nd.timer.properties=delay=3000&period=1000 //in prod
2nd.timer.properties=delay=0 //during tests
So that one is triggered at startup as well. Hope that helps,
Gergely
You can also use advice with in your unit test, and replace the from endpoint uri in the route during testing, and for example use a direct endpoint, then you can send a message to the direct endpoint to trigger the route to run.
See details at the Camel docs about testing
http://camel.apache.org/testing
http://camel.apache.org/advicewith.html
And there is also NotifyBuilder which can be used for "black box testing" where you may assert that X messages was processed etc
http://camel.apache.org/notifybuilder.html

Castle Windsor IInterceptor Configuration (Nancy bug)

So I ran into this issue with the Windsor bootstrapper for Nancy. I managed to whip together a small test project where I can reproduce what is going wrong. You can find the project here.
What seems to go wrong is this: DynamicProxy only seems to catch the invocation of the void Handle(Action<string> oncomplete) method and not the string Handle(string input) method that is called on another thread. As if the Engine is no longer proxied after it had been sent to another thread. Scratch that: It's just the call to another method on the same class that is not proxied.
This means the output of the program is only
Handled Handle with return type System.Void
test
and not
Handled Handle with return type System.Void
Handled Handle with return type System.String
test
Is this the expected behaviour of Dynamic Proxy? That proxies on another thread are not longer, well, proxied? Or is there something wrong with the code?
EDIT: Just RTFM'd Dynamic Proxy, and it seems like it Works As Intended. Now how do I configure my IEngine Instance to use the correct kind of Proxy?
Try changing :
Component.For<MyEngine>().Forward<IEngine>().Interceptors<ScopeInterceptor>());
into
Component.For<MyEngine>().Forward<IEngine>().Forward<MyEngine>().Interceptors<ScopeInterceptor>());
I don't have the time to actually try it but this should force windsor into creating a class proxy, which should solve your issue
Kind regards,
Marwijn.
-- edit --
for the current link try replacing :
Component.For<IEngine>().ImplementedBy<Engine>()
with:
Component.For<IEngine, Engine>().ImplementedBy<Engine>()

How to jmock Final class

I was trying to mock final class(AnyFinalClass.java) in junit using JDave in eclipse.
public void setUp() throws Exception {
Mockery mockery = new Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
AnyFinalClass any = mockery.mock(AnyFinalClass.class);
}
I am trying to use jdave-unfinalizer-1.1.jar as javaagent but didnt had any success. I tried multiple things but getting following exception
java.lang.IllegalArgumentException: Cannot subclass final class class AnyFinalClass
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
Can someone who has already tried jdave unfinalizer give me exact step how to make it work on eclipse.
I set following in eclipse.ini file but got the problem
-Xbootclasspath/a:lC:\WS\JunitTesting\jars\asm-3.0.jar
-javaagent:C:\WS\JunitTesting\jars\jdave-unfinalizer-1.1.jar
While running executing the junit, I gave vm argument as
javaagent:C:\WS\JunitTesting\jars\jdave-unfinalizer-1.1.jar
I am not sure what will be the code. jdave is not having the code and its site is pointing to some other site which is not working. Please correct my code or provide your same working code.
Any help is highly appreciated.
from Enhancer.java line 446:
if (TypeUtils.isFinal(sc.getModifiers()))
throw new IllegalArgumentException("Cannot subclass final class " + sc);
I have not worked with JDave but with another mocking frameworks and the only one that allows to mock a final class was powermock
Look also here
In order to get unfinalizer running you have to put -javaagent:path_to_unfinalizer/jdave-unfinalizer-1.1.jar in the VM arguments of the run configuration of the test.
I also had to include several dependencies of jdave-unfinalizer in the classpath of the project from which the tests ar being launched. These are, taken from the maven definitions of jdave:
jdave-core 1.1
cglib-nodep 2.1_3
objenesis 1.0
asm 3.0
asm-commons 3.0
asm-tree 3.0

Debugging surefire/junit's taste in test cases

Puzzled: I added a new test case function to a junit test. I run the entire class from either Eclipse or from maven, and the old case (there was only one before) runs and the new one does not. It doesn't fail. A breakpoint in it is not hit. The new function has an #Test annotation, just like the old one.
Junit version is 4.5.
Is there a way to get junit to log or trace its thought process in selecting functions to run?
I guess you still ran old class file, as new Java file was not be compiled successfully.
You could modify an old test method to see if the class is really modified: to let successful method to fail.