Setting timezone for individual tests on Eclipse - java

I am writing tests for my Hibernate DAO implementations as I explained here and they run well when I execute them with Maven or on Eclipse with Run as -> Maven test. The application uses Oracle so I had to define the TimeZone, and I did it adding Duser.timezone to the pom file as is described here.
The problem is when I want to execute only one test method, because each method has its own running configuration on Eclipse, and it is disappointing to add the timezone to the VM arguments of each configuration.
I have tried adding System.setProperty("user.timezone", "UTC") and also TimeZone.setDefault(new SimpleTimeZone(...)) to the #beforeClass method but it doesn't work. It shows the error ORA-01882: timezone region not found. I think that the cause is that #ContextConfiguration connects to the database before the #beforeClass method.
I have found a workaround that is adding it as a default argument to the VM. I have done it at Windows -> Preferences -> Installed JREs.
Do you know a better way of doing it?

Well, If you are right about the #Configuration class meddling with the DB, an option would be to do the System.setProperty in a static block in the #Configuration class.

Related

cucumber command line options don't override #CucumberOptions

I have this cucumber runner class:
#RunWith(Cucumber.class)
#CucumberOptions(plugin = {"pretty", "html:target/cucumber"},
features="classpath:features",
tags= {"#concrete"})
public class RunCuke {
}
This is the code available in git repository. While developing my own features, I want to use the same class but want to pass my own tags for it to run. But I don't want to modify this class (passing my own tag, say, #outline instead of #concrete), because this is a nuisance while committing code because each time I want to commit, I have to revert this file back to what is in the code repo (ie back to #concrete). Only way I think of is to pass the command line argument to this class.
So I am using this command line argument (program argument in the eclipse IDE):
-Dcucumber.options=”–tags #outline”
Unfortunately it is not working. It is not overriding what is there in #CucumberOptions.
How do I make it work?
You need to select 'Run Configurations' and go to the specific runner config. Go to the 'Environment' tab. Add a new variable 'cucumber.options' and set it to '--tag #outline'. You might need to switch the 'Append environment to native environment' and 'Replace native environment with specified environment', though I have found the default of 'append' works perfectly. Apply and Run.

Jenkins - Selecting tests

I am currently working on a Maven Project, using JUnit for defining tests and Jenkins for CI and am looking into how I can group my tests.
Say I had a test class with 20 tests, but I don't want to run all 20 tests, I want to be able to configure which tests to run. For Example, in another standalone project using TestNG and Selenium you can create a test method with the following annotation:
#Test (groups = { "AllTest" })
public void myTestMethod()
{
.. do something
.. assert something
}
... and then I am able to call which group to run based on an XML configuration.
Is it possible to define such type of groupings using Jenkins? I have researched into this and came across the plugin "Tests Selector Plugin" however can't understand how to get started once I've installed the plugin. There is a Wiki Page for it but I can't understand what to do after installing.
I have copy pasted the example property file, and didn't really understand what I needed to manipulate in it. When building, I simply get that the property file cannot be found or Jenkins doesn't have permission; can't find a way around this either :(
It's possible via maven + maven-surefire-plugin
http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
You can run a single test, set of tests or tests by regexp.

IntelliJ #IfProfileValue default value

In most projects that use Spring extensively there are a few tests that use #IfProfileValue to mark it as integration test, performance test or similar. When you run these with maven you do something like this:
mvn install -Dperformance-test=true
for a class annotated like this:
#IfProfileValue(name = "performance-test", value = "true")
But if I run this test in IntelliJ I get:
Test '.Tests in Progress.MyTest' ignored
which I can go around in IntelliJ by commenting out the annotation, but I would prefer if the test just ran without having to remove the line, so I don't accidentally commit/push the class without the marker.
Is that possible?
Edit the run-configurations and set your parameter -Dperformance-test=true
as VM option.
See the IDEA Run Config for detailed info.

How to step-debug annotation processor during compile?

I have an annotation processor for an annotation of retention policy=SOURCE.
I have not idea how to step-debug it.
I have issued print statements, logger info when I run mvn install, compile or package or ant javac, and I see their sysouts in the compile log.
However, I have no idea how to step-debug the processor in Eclipse. I mean, how do you step-debug compile-time?
An option in recent times is to use something like http://github.com/google/compile-testing which lets you invoke the compilation job against arbitrary annotation processors, which you can set break points, step through, etc.
#Test public void testStuff() {
// Create a source file to process, or load one from disk.
JavaFileObject file = JavaFileObjects.fromSourceLines("test.Foo",
"package test;",
"",
"import bar.*;",
"",
"#MyAnnotation(blah=false)",
"interface TestInterface {",
" Bar someBar();",
"}",
// assert conditions following a compilation in the context of MyProcessor.
assert_().about(javaSource()).that(file)
.processedWith(new MyProcessor())
.failsToCompile()
.withErrorContaining("some error message").in(file).onLine(5);
}
This test expects you will get some error message because #MyAnnotation is incorrectly declared in the test data source. If this assertion fails, you can run it in debug mode in your IDE, set breakpoints in MyProcessor, and step through with a full compiler environment active during debugging.
For unit testing specific methods within your processor, you can also use the #Rule called CompilationRule from which you can obtain Elements and Types utility classes in order to test specific logic in your compiler in a more isolated way.
You have to invoke the Java compiler from Eclipse, using a debug configuration (you'll need to create the configuration manually, from the "Debug Configurations..." menu choice.
The "correct" way to invoke the Java compiler under JDK 1.6 or above is to use the JavaCompiler interface in javax.tools, which you get from the ToolProvider (I include all the links because there's a decent amount of class/package documentation that you should read).
The "quick-and-dirty" way (that should work, but I make no guarantees) is to invoke com.sun.tools.javac.Main.main(), passing it your normal command-line arguments. To do this, you'll need tools.jar on your classpath (it's found in $JAVA_HOME/lib).
Annotation processing occurs during compilation, so normal debugging won't work. If you want to debug it in the context of you project, you can use Eclipse remote debugging, while having Gradle or Maven in debug mode. Then you can put breakpoints in the Annotation Processor's files.
See Debugging an Annotation Processor in any project.
Disclaimer: I wrote the post.

How to run Tests when developing javaagents?

I'm trying to fiddle with Foursquare's HeapAudit, and am attempting to set it up using IntelliJ IDEA. I have managed to get it to build just fine, using the dependencies from the pom.xml.
However, when I actually try to run the JUnit tests, basically all of them fail. I'm guessing this is because using HeapAudit requires the JVM to be started with it as a -javaagent, according to the github:
$ java -javaagent:heapaudit.jar MyTest
Presumably the tests would pass if I put this line in, and referenced the heapaudit.jar i downloaded/built earlier. However, it seems to me that if I make changes the the source, I'm gonna need to re-package this silly .jar file in order to see if it works.
Is there any way of running the tests with a -javaagent without going through the whole rigmarole of compile -> package-into-jar every testing cycle? Perhaps getting IntelliJ to attached the newly-compiled .class files as a -javaagent before running the tests?
1) Have a jar just with a META-INF/MANIFEST.MF
The manifest must be properly configured with Premain-Class and other attributes. The jar doesn't need any other files. Use this jar with the -javaagent. Provided that the agent classes are in the classpath, the agent will start normally.
This might fail when using maven-surefire-plugin with forkMode=never because by default the application classes are loaded in a child ClassLoader.
Works fine with Eclipse and Intellij.
If doing this, double check the manifest syntax (once I spent a long time to figure out that a package name was wrong).
2) Use ea-agent-loader
It will allow you to load the agent (any agent) in runtime (it uses VM.attach()). However the VM.attach() sometimes disrupts debugging and breakpoints might fail to trigger.
It will have the same issues with the surefire in forkMode=never
3) Load the agent in runtime.
Write your on code to load the agent in runtime. And call it from your #BeforeClass You will still need a jar (which you can generate in runtime if you want).
Just you need to call this (only once):
AgentLoader.loadAgentClass(YourAgentClass.class.getName());

Categories