Report Test Case Names for #TestFactory - maven-surefire-plugin

I have a Junit #TestFactory method, and I would like to create a report of all the test case names. This is easily viewed in Eclipse, but I would also like to do some text processing on the names at the command line. Is there any way that I can get this reported by Surefire at the command line?

You could implement and register a custom TestExecutionListener.
For example, you could implement something similar to the following.
package my.example.package;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import org.junit.platform.engine.TestDescriptor.Type;
import org.junit.platform.engine.TestExecutionResult;
public class DynamicTestListener implements TestExecutionListener {
private final Set<TestIdentifier> dynamicTests = Collections.synchronizedSet(new LinkedHashSet<>());
#Override
public void dynamicTestRegistered(TestIdentifier testIdentifier) {
if (testIdentifier.getType() == Type.TEST) {
this.dynamicTests.add(testIdentifier);
}
}
#Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
if (this.dynamicTests.contains(testIdentifier)) {
System.out.println(testIdentifier.getDisplayName());
}
}
}
The above example can be automatically registered via Java's ServiceLoader mechanism by putting the following in src/test/resources/META-INF/services/org.junit.platform.launcher.TestExecutionListener (or under src/main/resources if you are publishing a library):
my.example.package.DynamicTestListener

Related

Codename one separate class along with statemachine class

I am working a project in gui builder..As my project is growing bigger and bigger, i find it hard to search a particular forms and methods all in a statemachine class. so i wanted to create a separate class for each form. but since the gui builder create the methods automatically in statemachine which extends statemachineBase class. how can i use separate class for separate gui forms so that they automatically create methods in the designated class. for instance when i click before show event of form named "NextPage", the gui builder automatically create beforeNextPage method in NextPage class instead of statemachine. I did the followings but lost in the process..
NextPage.class
public class NextPage extends StateMachine {
private ArrayList<Map<String, Object>> mData;
private ArrayList<Map<String, Object>> moreData;
public NextPage(String resFile) {
super(resFile);
}
#Override
public void beforeNextPage(Form f) {
//.........
}
}
Forms generated by GUI Builder cannot be separated from StateMachineBase into different Classes.
What I do personally is create a form in GUI to get the right Look and Feel and then create a replica of that form in code, then delete the one on GUI Builder once I'm satisfied with the code version. It makes my projects well organized and easy to debug.
BeforeShow() would be handle while the form class is loading and to do anything in PostShow(), just do this:
this.addShowListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
removeShowListener(this);
//Your postShow() codes here.
revalidate();
}
});
Forms created in code are light-weight and more customizable than GUI forms.

Ignore method during Proguard Obfuscation

I have been trying desperately to ignore the following method during Proguard obfuscation:
package com.foo.me;
public class MyClass extends Activity implements Callback {
...
public void setButtonColor(String color, Button button){
...
}
...
}
I feel like I've been through all the answers here with no luck. Can any share the correct statement to skip over this method with Proguard?
Thanks,
Josh
You can keep ProGuard from removing or renaming the class and the method with this configuration:
-keep class com.foo.me.MyClass {
public void setButtonColor(java.lang.String, android.widget.Button);
}
See the ProGuard manual > Examples > Processing callback methods

MEF Recomposition error

I get the exception:
1) More than one export was found that matches the constraint:
ContractName CompositionTest.C
RequiredTypeIdentity CompositionTest.C
When running the program
namespace CompositionTest
{
// [Export] // Also doesn't work
[Export(typeof(C))]
public class C
{
//[ImportAttribute(AllowRecomposition = true)] // also doesn't work
[Import(AllowRecomposition = true)]
public C PropertyC { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Declare a composition container.
CompositionContainer compositionContainer = new CompositionContainer();
compositionContainer.ComposeParts( new C() );
compositionContainer.ComposeParts( new C() ); // exception here!
}
}
}
What am I doing wrong?
The first time you call ComposeParts, a new C object is added as an export to the container. Then second time you call ComposeParts, another C object is added as an export. This creates a problem with the import because there are two possible parts for import and MEF cannot make a decision. Hence the cardinality exception.
One solution would be to change the import to:
[ImportMany(AllowRecomposition = true)]
public IEnumerable<C> PropertyC { get; set; }
Another solution is to actually use a catalog when creating the container. This is the common way to use MEF. Pretty much all the examples you can find follow this approach:
//Create a catalog. In this case, a catalog based on an already loaded assembly.
var catalog = new AssemblyCatalog(typeof(C).Assembly);
//Create a container using the catalog. Only the parts from that catalog will be used.
var compositionContainer = new CompositionContainer(catalog);
For more on catalogs you should read this article.
By the way I have never seen such an example of MEF usage before. My answer is mainly based on observations I made while debugging it.

How to avoid that proguard obfuscates the classes annotated with #OnStart

In applications based on NetBeans Platform 7.2, it is possible to
replace the ModuleInstall classes with this code:
import org.openide.modules.OnStart;
import org.openide.modules.OnStop;
#OnStart
public final class Installer implements Runnable {
#Override
public void run() {
System.out.println("enable something...");
}
#OnStop
public static final class Down implements Runnable {
#Override
public void run() {
System.out.println("disable something...");
}
}
}
My problem is that, after obfuscation, the class loader does not find
the annotated classes.
In the Proguard configuration I added (as suggested here)
-keep #org.openide.modules.OnStart class *
But apparently this is not enough or it does not work.
Does anybody have a suggestion?
From I could figure out, you need to explicitly keep the annotations that you use to keep whatever specifications. So, in your case, adding
-keep enum org.openide.modules.OnStart
would allow this annotation to be used as a selector.
Proguard really ought to include a warning message if annotation selectors are not actually matching. It also doesn't really make sense to keep the annotation, especially if it's not of runtime retention.
Have you tried -keepattributes *Annotation*? It might do the trick, based on proguard usage.

Can mockito or easymock replace rmock

I'm sitting with a legacy project, and we're starting to replace some old legacycode. As Rmock don't have support for junit4, we would like to replace it. One thing i was wondering is - how could i replace the dynamictestsuite feature of rmock. This is a good feature where you create a dynamic testsuite for each run, and can do stuff like.
#Override
protected void setupSuite() {
forEach(is.clazz.assignableTo(TestCase.class).and(is.not(is.clazz.name(is.endingWith("oldTest")))).perform(addAllToSuite);
}
that would get all testclasses not ending with oldTest and create a dynamictestsuite. And so on, you get the point.
ClasspathSuite can define a suite by searching for JUnit test classes in the class path, with a filter on the test class names to include or exclude.
import org.junit.extensions.cpsuite.ClasspathSuite.*;
import org.junit.runner.RunWith;
#RunWith(ClasspathSuite.class)
#ClassnameFilters({"!.*oldTest"})
public class MySuite {}

Resources