I created two classes under the same package one is called preparation the other is X when I use dependsOnMethods to point to the test case in Preparation I get an exception.
class X.
#Test(enabled = true, dependsOnMethods = {"com.selenium.scripts.passkey.regression.delegateprofile.Preparations.TC_01"})
public void TC_01() {
something ...
}
class preparation :
#Test(enabled = true, description = "Preparation: create a new hotel.")
public void TC_01() {........}
Here is the error:
com.selenium.scripts.passkey.regression.delegateprofile.DProfile.TC_01()
is depending on method public void
com.selenium.scripts.passkey.regression.delegateprofile.Preparations.TC_02(),
which is not annotated with #Test or not included.
The method should be included in the .xml file
The method on which your test methods depends on should also be in the same class than different classes. This will not make the code ambiguous.
As I know, dependsOnMethods only accept method name and not a class + name.
What you can try to do is using groups and dependsOnGroups attribute.
Related
I'm playing with javassist (to use it later on a project) but I don't manage to make a simple update to a class.
I try to insert code before a method but its not being executed.
I've a gradle project and I'm using javassist version: '3.27.0-GA'.
Given the following class:
public class Dummy{
public int dummy(){
return 5;
}
}
The following test fails, so the class is not being modified:
#Test
public void modifyReturnValueTest() throws NotFoundException, CannotCompileException, IOException {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get("Dummy");
CtMethod m = cc.getDeclaredMethod("dummy");
m.insertBefore("{ if(true) return 3; }");
cc.writeFile();
assertEquals(3, new Dummy().dummy());
}
I'm missing something?
My guess is that when you call new Dummy().dummy() in
assertEquals(3, new Dummy().dummy());
the class loader loads the original version of Dummy class.
Since you want to load/use the version that you modified represented by the CtClass instance cc, you can insert the following snippet before assertEquals to make the class loader load the modified version of the Dummy class instead:
cc.toClass();
then the assert should be successful.
Note that in order to use toClass() above we rely on the fact that the Dummy class is never loaded before the toClass() invocation. (It will throw exception otherwise, the class loader cannot load two different versions of the same class at the same time)
You can check the javassist documentation for more details. This section might be especially useful http://www.javassist.org/tutorial/tutorial.html#load.
I am wanting to use Concordion to write tests for a simple bankAccount pgm that is offered as part of a cucumber tutorial. I am having trouble working out how to initialize objects from the concordion test spec.
I created a method in the fixture called createAccount that took an initialBalance and account number as parameters.
I call it from the test spec with ...
<b concordion:execute="createAccount(#accountNum,#initBalance)">
but when I run the test I get an error ....
java.lang.Exception: Method createAccount should have no parameters.
I know that I can just assign values to these variables from within the fixture class but think it would be a lot more useful if I could do this from the test specification.
Below is part of my test fixture with the call to createAccount
#RunWith(ConcordionRunner.class)
public class CashMachineTest {
private Account newAccount;
int initBalance = 123;
int accountNum = 1;
#Before
public void createAccount(int accountNum, int initBalance) throws Throwable{
newAccount = new Account(accountNum, initBalance);
}
public int getBalance(){
return newAccount.getBalance();
}
public void deposit(int amount){
newAccount.deposit(amount);
}
....
Any help would be much appreciated.
When you want to call the initialization method from your Concordion specification, you don't need the #Before annotation. This is used by JUnit to call methods before the execution of the actual test method. But Concordion does not run a single test method, but calls the methods in your fixture class based on your instrumentation in your specification document.
Pleas, try to remove the #Before annotation and try again.
What I have to do:
I have to test my spring mvc with JMockit. I need to do two things:
Redefine MyService.doService method
Check how many times redefined MyService.doService method is called
What the problem:
To cope with the first item, I should use MockUp; to cope with the second item I should use #Mocked MyService. As I understand this two approaches are overriding each other.
My questions:
How to override MyService.doService method and simultaneously check how many times it was invoked?
Is it possible to avoid mixing a behaviour & state based testing approaches in my case?
My code:
#WebAppConfiguration
#ContextConfiguration(locations = "classpath:ctx/persistenceContextTest.xml")
#RunWith(SpringJUnit4ClassRunner.class)
public class MyControllerTest extends AbstractContextControllerTests {
private MockMvc mockMvc;
#Autowired
protected WebApplicationContext wac;
#Mocked()
private MyServiceImpl myServiceMock;
#BeforeClass
public static void beforeClass() {
new MockUp<MyServiceImpl>() {
#SuppressWarnings("unused")
#Mock
public List<Object> doService() {
return null;
}
};
}
#Before
public void setUp() throws Exception {
this.mockMvc = webAppContextSetup(this.wac).build();
}
#Test
public void sendRedirect() throws Exception {
mockMvc.perform(get("/doService.html"))
.andExpect(model().attribute("positions", null));
new Verifications() {
{
myServiceMock.doService();
times = 1;
}
};
}
}
I don't know what gave you the impression that you "should use" MockUp for something, while using #Mocked for something else in the same test.
In fact, you can use either one of these two APIs, since they are both very capable. Normally, though, only one or the other is used in a given test (or test class), not both.
To verify how many invocations occurred to a given mocked method, you can use the "invocations/minInvocations/maxInvocations" attributes of the #Mock annotation when using a MockUp; or the "times/minTimes/maxTimes" fields when using #Mocked. Choose whichever one best satisfies your needs and testing style. For example tests, check out the JMockit documentation.
I am trying to integrate TestLink with TestNG
Approach is below
1>Write ITestListner with onTestFailure and onTestSuccess
2> get Annotation of the method(like testName which will be equivalent to test name in testlink) which is being failed/success in a variable
3>Make connection with TestLink using API available and update the test case.
However I am struggling to find method Annotation value in ITestListner and requirement is to get annotation values in ITestListner only so that correct test cases can be updated in Test_link
Can someone please help me how to get Test Method annotation value in ITestListner or any other approach in which i can integrate testlink update with TestNG
Hi Thanks niharika for help
,First of all you are correct in explaining use of TestNG but we are using TestNG for Selenium and already there are around 1000 test cases writen in test Methods and we have to live with that
Some how i have figured the solution ,we can still get the testName of the test method using two listners
This is just work around I am not sure if this is the best approach but as of now solving my purpose
package com.automation.testng.listner;
import org.testng.*;
public class MyIInvokeMethodListner_TestName_TestLink implements IInvokedMethodListener {
public static String testName;
public void afterInvocation(IInvokedMethod arg0, ITestResult arg1) {
// TODO Auto-generated method stub
}
public void beforeInvocation(IInvokedMethod m, ITestResult tr) {
// TODO Auto-generated method stub
//This give the Annotation Test object
org.testng.annotations.Test t=m.getTestMethod().getMethod().getAnnotation(org.testng.annotations.Test.class);
MyIInvokeMethodListner_TestName_TestLink.testName = t.testName().toString();
}
}
MyITestListner goes like below
package com.automation.testng.listner;
import org.testng.*;
public class MyITestListner_TestLink extends TestListenerAdapter {
/*IAnnotationTransformer at;
public Listner_1()
{
this.at = new Annotation_listner();
}*/
#Override
public void onTestFailure(ITestResult tr)
{
System.out.println("Hurray !I am being inboked from Test listner");
MyIInvokeMethodListner_TestName_TestLink a = new MyIInvokeMethodListner_TestName_TestLink();
System.out.println(MyIInvokeMethodListner_TestName_TestLink.testName);
}
public void onTestSuccess(ITestResult tr)
{
MyIInvokeMethodListner_TestName_TestLink a = new MyIInvokeMethodListner_TestName_TestLink();
System.out.println(MyIInvokeMethodListner_TestName_TestLink.testName);
}
}
Basically we are getting the method and then using Test Annotation class setting the static variable which can be used in MyITestListner
The ITestListener is the one which is used after <test> tag. For getting the method name and annotation specifics, you need to implement IInvokedMethodListener and in the after/before methods of this interface, and use something like method.getTestMethod().getMethodName() to get the executing method name.
If you are adding testName at the method level, I think you are doing it wrong since the help of testng mentions this "The name of the test this test class should be placed in. This attribute is ignore if #Test is not at the class level."
If you are indeed specifying the #Test at your class level then you can get it as below :
method.getTestMethod().getTestClass().getTestName()
A bit ugly and you probably want to wrap those parts in null checks in your code but this is how you get the testName specified in the annotation from the ITestResult:
iTestResult.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class).testName()
I have a code like this:
public class MyTest extends TestCase {
private MyObject mObject1;
private MyObject mObject2;
...
#Override
public void setUp() throws Exception {
super.setUp();
}
public void testSomething() {
mObject1 = new MyObject();
mObject2 = new MyObject();
}
public void testSomething2() {
// Here I can't access the previously created objects mObject1 and
// mObject2, because they are again null.
// Why is that, if *my* setUp() method doesn't touch them?
}
My guess is that JUnit instantiates the class again every time. Can someone please explain me the workflow?
Thanks.
JUnit will instantiate the class (MyTest) once per test and then execute the methods
setUp()
testXXX()
tearDown()
until it runs all the methods that start with test and don't receive any parameters. So in your example, Junit will instantiate MyTest twice. You can read more about this in the JUnit documentation.
Bear in mind that this is the old way of writing tests. From Junit 4 (I think) the preferred way is to use annotations. You can check the annotations documentation here.
As a side note, NUnit, reuses the instance of the test, so in the same scenario, it would only instantiate MyTest once.
JUnit will instantiate this class once per test method, so only once in the code above, but try it again with two test methods and you will see it instantiated twice. If you want to save some state in fields without having to use statics, take a look at TestNG, which reuses the same instance for all test methods.