TestNG java.net.SocketException - exception

I'm attempting to run TestNG tests but I keep getting a java.net.SocketException. Other forums state that this is a known issue for users running TestNG 6.4 and greater on IDEA but I am using Eclipse. I tried reinstalling TestNG to eclipse but that did not help. Furthermore TestNG is failing to report which tests have passed and which have failed. Below is the code that is causing this Exception and notice that the test method is actually empty.
public class AdminUITest extends AccountingTS{
#BeforeClass
public void oneTimeSetUp() {
}
#AfterClass
public void oneTimeTearDown(){
driver.quit();
// closeJSchSessions();
}
#BeforeMethod
public void setUp(Method method){
resetStep();
printStep("TEST CASE >>> "+method.getName());
}
#Test
public void test()
{
}
}

I resolved this in Eclipse by clicking on Windows>Preferences>Java>Installed JREs>Edit and changing the JRE Home to C:\Program Files\Java\jdk1.7.0_10.

If you installed TestNG from eclipse then go to your project properties, click TestNG and uncheck Use project TestNG jar.
Please don't include TestNG jar explicitly in your project's build path, doing so lead to confusion and exception.

Related

Difference in running Cucumber-JVM vs Cucumber runner(Junit)

I am fairly new to Cucumber. I was experimenting with it by just creating few test features when I noticed the difference when running a single feature vs running the whole suite (from the IntelliJ).
I noticed that when I run single feature it runs using the cucumber-jvm option and in this case, the CucumberConfig(the blank class to define the runner and cucumber options) and the Runner is not utilized. However, when I run the whole suite it runs as a JUnit test and obviously, in this case, the Config class and the runner comes into the picture.
I confirmed this with the following sample code:
#RunWith(CustomRunner.class)
#CucumberOptions()
public class CucumberConfig {
#BeforeClass
public static void beforeClass()
{
System.out.println("This is run before Once: ");
}
#AfterClass
public static void afterClass()
{
System.out.println("This is run after Once: ");
}
}
CustomRunner
public class CustomRunner extends Cucumber {
public CustomRunner(Class clazz) throws InitializationError, IOException {
super(clazz);
System.out.println("I am in the custom runner.");
}
}
Also, I understand that while running as cucumber-junit we can't pass specific feature to run as in cucumber-jvm. Correct me if I am wrong.
My doubt is, is this the default behavior or am I doing something wrong. And, if this is default how can I make cucumber to always use the Config file.
I'll appreciate if someone can provide some insight on this.
When you're using IntelliJ IDEA to run the tests, IDEA will use cucumber.api.Main to run the tests. As such it will ignore CucumberConfig neither will it run #BeforeClass nor #AfterClass, these are only used by the JUnit runner.

Execute code before #Before method for JUnit test

I need to execute some code before the #Before method of each unit test is executed. The problem is that I also need to know which test (the name is sufficient) will be executed afterwards.
I can either use AspectJ or Java Agents with bytecode manipulation to achieve this. Also the solution should work for tests where there is no #Before annotation present.
Any ideas?
EDIT: I can't modify the unit tests themselves, as I'm developing a framework for executing tests of other projects
You might want to look into the TestName rule in JUnit:
http://junit.org/junit4/javadoc/4.12/org/junit/rules/TestName.html
About the ordering, a solution could be to define a super class for your tests and put a #Before in there, as #Before methods in super classes are run before those in sub classes.
If you want to write a Java agent and you are not bound to Javassist or AspectJ, have a look at Byte Buddy for doing so. You can add the code in the MyAdvice class to any method annotated with #Test given that the type name ends with Test (as an example) by:
public class MyAgent {
public static void premain(String arg, Instrumentation inst) {
new AgentBuilder.Default()
.type(nameEndsWith("Test"))
.transform((type, cl, builder) -> builder.visit(Advice
.to(MyAdvice.class)
.on(isAnnotatedWith(Test.class)))
.installOn(instrumentation);
}
}
class MyAdvice {
#Advice.OnMethodEnter
static void enter() {
System.out.println("foo");
}
}
Just bundle the above code to a Javaagent with the proper manifest code and register it before running. If you are running on a JDK, you can also attach the agent programmatically using the byte-buddy-agent project.

Problems Testing Sling Model with Custom Injectors

I've been doing some work with sling models for a project and in the process created a couple of custom injectors. Everything seems to work great when implemented (used in AEM). However, when I'm testing the custom injectors are not getting run.
Here's an example of what I currently have set up
In MyModel
#Inheritable
#CustomAnnotation("foo")
private String _foo
In test (tests using wcm.io mocking Libraries)
#Rule
AemContext context = new AemContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
//required by the injector
#Mock
InheritanceService _inheritanceService;
#Mock
InheritableInjector _inheritanceInjector;
#Before
public void setup() {
context.registerService(InheritanceService.class, _inheritanceService);
context.registerService(InheritableInjector.class, _inheritanceInjector);
context.addModelsForPackage("com.package.example.models");
//use this resource in tests to adaptTo(MyModel.class)
_resource = context.load().json("myJson.json", "/myPath");
}
... tests
The tests compile and run, but the Injector isn't being executed. I know it's registered because when I don't have the Injector's dependent services registered in the context I get an error. When I debug through it, none of the breakpoints are hit. I'm wondering if I need to also register the "Inheritable" annotation somewhere or if anyone just has any general information on how I can get the custom injector to execute.
thank you
I was able to figure out my error. So the important thing to remember about Sling Model Injectors is that they are just OSGI services (something I completely let myself get away from).
So just treating them like normal services and then remembering to annotate the Injector with #InjectMocks was what I needed to do in order to fix the error.
The following now works great.
#Mock
InheritanceService _inheritanceService; //injector dependency
#InjectMocks
InheritanceInjector _inheritanceInjector;
#Before
public void setup() {
context.registerService(InheritanceService.class, _inheritanceService);
context.registerService(InheritableInjector.class, _inheritanceInjector);
}
hopefully that helps anyone that might run into the issue. If anyone can make this answer better please feel free to reply/edit.

How to test individual testng methods when it depends on other methods

Consider the following example code
public class PropertyServiceTest extends AbstractTestNGSpringContextTests {
#Test(groups={"property_service"})
public void testCreateProperty() { ... }
#Test(groups={"property_service"}, dependsOnMethods={"testCreateProperty"})
public void testCreateProperty1() {...P
}
When I execute individual test 'testCreateProperty1' using maven,
mvn -Dtest=PropertyServiceTest#testCreateProperty1
am getting the following exception.
Caused by: org.testng.TestNGException:
com.service.PropertyServiceTest.testCreateProperty1() is depending on method public void com.service.PropertyServiceTest.testCreateProperty(), which is not annotated with #Test
at org.testng.internal.MethodHelper.findDependedUponMethods(MethodHelper.java:95)
at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:245)
at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:316)
at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:51)
Kindly help me to resolve this issue.
This does not appear to be related to the Spring TestContext Framework.
Rather, this appears to be a configuration issue with TestNG.
You therefore might find the "How to solve test method dependencies?" discussion on Google Groups helpful.
Let us know if that helps you solve your issue.

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