I have the following scenario:
I perform several tests (#Test) and tests in Cucumber, in Selenium Webdriver, Java.
The tests are going well. However, I want to leave a string stored in one #Test (public void) in another #Test (public void). I cannot.
Could anyone help?
First test:
#Test
public void testDocuments() {
OneClass oneClass = new OneClass();
oneClass.one();
oneClass.two();
}
Second test:
#Test
public void testDocuments() {
OneClass oneClass = new OneClass();
oneClass.one();
oneClass.two();
}
Method one
public String one() {
if (this.cnpj == null) {
this.cnpj = add.cnpj(false);
} else {
}
return this.cnpj;
}
Both tests I want you to use the same generated string !!!!
I look forward and thanks in advance!
I'm not sure what your method one() does, but assuming you want to use the same value for two different tests, why not just do this:
OneClass oneClass = new OneClass();
String yourGeneratedString = oneClass.one();
// First test
#Test
public void testDocuments() {
yourFunction(yourGeneratedString);
}
// Second test
#Test
public void testDocuments2() {
yourOtherFunction(yourGeneratedString);
}
If I understand correctly, you need this.cnpj value to be available within the second test?
Each time you do new OneClass() , it creates a new instance of it.
So you can do one of the following:
Use singleton instance of OneClass
Make cnpj a static field within OneClass
If I understand it right, you want to share data from one test to second one. If you user testNG then you can do it this way.
import org.testng.ITestContext;
import org.testng.annotations.Test;
public class MyTest {
#Test
public void testOne(ITestContext context){
context.setAttribute("myKey", "myValue");
}
#Test
public void testTwo(ITestContext context){
String valueFromTestOne = (String) context.getAttribute("myKey");
System.out.println("My key = " + valueFromTestOne);
}
}
Related
I wrote a simple class, in which first function's output is second function's input and second function's output is third function's input.
After that I am printing these outputs in another class which implements JUnit #Test cases.
But for call to second function's test, I'm getting 'res' value as null(which should be output of first function and passing it as input to second function). Same is the case with third function.
I'm declaring 'res' as global variable. Then why is its value changing to null instead of holding the result of first function and then of second function( for call to third function) ?
Here is the class which contains 3 functions:
package con.nc.junitexmples.Junit4Examples;
public class StringExample {
public static String firstFunction() {
String msg1 = "msg1";
return msg1;
}
public static String secondFunction(String msg1) {
String msg2 = msg1+"msg2";
return msg2;
}
public static String thirdFunction(String msg2) {
String msg3= msg2+"msg3";
return msg3;
}
}
Here is class which implements JUnit #Test cases and printing output:
package con.nc.junitexmples.Junit4Examples;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestStringExample {
String res;
#BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("\n\t\t-----JUnit cascading input Example------\n\n");
System.out.println("before class---->only once");
}
#Before
public void setUp() throws Exception {
System.out.println("\nbefore--->before each test case");
}
#Test
public void testFirstFunction(){
System.out.println("\ttest case: FIRST Function");
res = StringExample.firstFunction();
System.out.println("\t"+res);
}
#Test
public void testSecondFunction(){
System.out.println("\ttest case: SECOND Function");
res = StringExample.secondFunction(res);
System.out.println("\t"+res);
}
#Test
public void testThirdFunction(){
System.out.println("\ttest case: THIRD Function");
res = StringExample.thirdFunction(res);
System.out.println("\t"+res);
}
#After
public void tearDown() throws Exception {
System.out.println("after ---> after each test case\n\n");
}
#AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("after class--->only once");
}
}
And here is the output I'm getting:
-----JUnit cascading input Example------
before class---->only once
before--->before each test case
test case: FIRST Function
msg1
after ---> after each test case
before--->before each test case
test case: SECOND Function
**nullmsg2**
after ---> after each test case
before--->before each test case
test case: THIRD Function
**nullmsg3**
after ---> after each test case
after class--->only once
How do I pass result of first function to second as input and so on?
Each of these tests is self contained, independent and the value of res at the start of these tests methods is null.
So, that explains why you are seeing:
nullmsg2
nullmsg3
Given this question from the comments above:
how do I pass output of first function as input to second function for second test?
You could test all three calls in one test method, like so:
#Test
public void testAll() {
String res = StringExample.firstFunction();
Assert.assertEquals("msg1", res);
res = StringExample.secondFunction(res);
Assert.assertEquals("msg1msg2", res);
res = StringExample.thirdFunction(res);
Assert.assertEquals("msg1msg2msg3", res);
}
For pass the result of first function to second function:
#Test
public void testSecondFunction(){
System.out.println("\ttest case: SECOND Function");
String resFromFirstFunc = StringExample.firstFunction();
res = StringExample.secondFunction(resFromFirstFunc);
System.out.println("\t"+res);
}
But you shouldn't write the test like above example, it always passed.
You should use:
Assert.assertEquals(input,output)
This question already has answers here:
Is it possible to use different #Before #After for each test case in JUnit?
(2 answers)
Closed 8 years ago.
Is there any way to avoid calling populateRandomData() method at the begining of each test without having a fixed parameter 100. I need to call the same method to setup data before execution of each test but I need to change the number of test data entries e.g. 100 in each case .
public class Tester
{
#Before
public void setUp() {
populateRandomData(100)
}
#Test
public void testMethod() {
}
private void populateRandomData(n){
//n times insert random data in table.
}
}
You can create Parameterized JUnit Test which allows you to add number of parameters you want to pass in unit test case. Have a look at example tutorial Create Parameterized Test Case.
OR
#Rule, using this annotations on your test methods to parameterize the execution of your rules makes it even more useful. Taken from JUnit 4.7 #Rules
EDIT :
Example of Using #Rule :
Below is the class which allows you to initialize different value of num variable which will be used in test method :
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
public class Test1 implements TestRule {
private final int num;
public Test1(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public class Test1Statement extends Statement {
private final Statement statement;
public Test1Statement(Statement statement, int num) {
this.statement = statement;
}
#Override
public void evaluate() throws Throwable {
statement.evaluate();
}
}
#Override
public Statement apply(Statement statement, Description description) {
return new Test1Statement(statement, num);
}
}
The class below is the actual test case class. It contains JUnit test cases & set value of num variable in test method.
import org.junit.Rule;
import org.junit.Test;
public class RuleNumberTester {
#Rule
public Test1 test = null;
#Rule
public Test1 test1 = null;
#Test
public void num1Test() {
test = new Test1(111);
System.out.println("Num 1 : " + test.getNum());
}
#Test
public void num2Test() {
test1 = new Test1(222);
System.out.println("Num 2 : " + test1.getNum());
}
}
Output :
Test cases are executed successfully & shows the values of num variable which was initialized in test methods on console.
Num 1 : 111
Num 2 : 222
I suppose you could use a #Rule to ensure populateRandomData() is called each time with the correct parameters.
However, this gets ugly quickly since you then need to maintain a list of test method names.
private static final Map<String, Integer> dataCounts = new HashMap<>();
static {
// list (or otherwise obtain) counts here
dataCounts.put("testMethod", 100);
}
#Rule
public TestWatcher watcher = new TestWatcher() {
#Override
protected void starting(Description description) {
Integer count = dataCounts.get(description.getMethodName());
assertNotNull(count);
populateRandomData(count.intValue());
};
};
Hello I have a simple testNG project which has a SampleTest.java class file which has 2 test cases and I have added a listener called MyListener to it . For this I have a MyListener.java class file which extends the TestListener of TestNG where in I'm printing pass or fail or skipped depending upon the test case execution. So every time i run SampleTest I can see Pass/fail in the console.. But I want it with the classname
My problem statement is, How can i get the Test Case file name (i.e. Sampletest here) in the MyListener class??? I tried with stacktrace but no help.. As I guess its not being called but its just acting upon/listening to the testcases in the file.. Please let me know how can I get the name of that class in listener????
SampleTest.java:
package com.abc;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
#Listeners({ com.mindtree.MyListener.class})
public class SampleTest {
#Test
public void SampleTest() throws Exception{
System.out.println("Hello world");
}
#Test
public void SampleTest1() throws Exception{
System.out.println("Hello Swarna");
}
}
MyListener.java:
package com.abc;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
public class MyListener extends TestListenerAdapter {
private int m_count = 0;
#Override
public void onTestFailure(ITestResult tr) {
log("Fail");
}
#Override
public void onTestSkipped(ITestResult tr) {
log("Skipped");
}
#Override
public void onTestSuccess(ITestResult tr) {
log("\nSuccess\n");
## WANT TO PRINT HERE THE TESTCASE CLASS NAME
}
private void log(String string) {
System.out.print(string);
if (++m_count % 40 == 0) {
System.out.println("");
}
}
}
but wont work for multiple testcase files.. Just create an object of SampleTest in MyListener access the classname through getters and setters
From your description I understand you want to get the name of the class where your #Test annotated test-methods reside, and only the class name without the package name
In your case that would be the output: "SampleTest".
You can achieve this the following way:
public void onTestSuccess(ITestResult tr) {
log("\nSuccess\n");
String className = tr.getTestClass().getRealClass().getSimpleName()
log(className);
}
The solution of #JacekM also works fine but in your case would return "com.abc.SampleTest"
None of the solution posted worked for me, however I found that it is possible to get the required information by accessing the "ITestResult" object in the following manner:
public void onTestSuccess(ITestResult result) {
log("\nSuccess\n");
String className = result.getInstanceName();
log(className);
}
NOTE: The above code was tested on my environment successfully
You can find out by examining the stacktrace like:
StackTraceElement trace[] = Thread.currentThread().getStackTrace();
you can get the the calling class by trace[i].getClassName().
Note this is pretty expensive, so don't use in loggers etc.
Use a beforeMethod method in which you can get the name of the method
#BeforeMethod
public void beforeMethod(final Method method) throws Exception {
String testName = method.getName();
}
You have the access to ITestResult object, so you should be able to do:
public void onTestSuccess(ITestResult tr) {
String className = tr.getMethod().getTestClass().getName();
System.out.println(className);
}
I think I might have found a bug in JMockit, but I would like some to confirm whether it's a bug or there's something I'm missing.
I have the following (very simple) class:
public class Dummy {
public void foo() {System.out.println("O");}
}
Now I have the following tests, where in each of them I try to mock the method 'foo' more than once (each test does it a little differently):
Test #1
#Test
public void test1() {
new MockUp<Dummy>() {
#Mock
public void foo(Invocation inv) {
System.out.println("A");
inv.proceed();
}
}
new MockUp<Dummy>() {
#Mock
public void foo(Invocation inv) {
System.out.println("B");
inv.proceed();
}
}
new Dummy().foo();
}
Test #2
#Test
public void test2() {
mock("A");
mock("B");
new Dummy().foo();
}
private void mock(final String s) {
new MockUp<Dummy>() {
#Mock
public void foo(Invocation inv) {
System.out.println(s);
inv.proceed();
}
}
}
The only difference between the tests is the extraction of the mock code to a different method. But the results are not the same...
Test #1 output:
B
A
B
O
This is odd, because I wouldn't expect A to appear at all. But anyway, here's test #2 output:
B
A
A
A
...ad infinitum
Test #2 will fail with a StackOverflowError.
Is this a bug or am I missing something?
Update (with the solution)
As #Rogério mentioned, this behavior is not acceptable.
Then how can the mock be overridden? like this:
private MockUp<Dummy> mock;
#Test
public void test3() {
mockCorrectly("A");
mockCorrectly("B");
new Dummy().foo();
}
private void mockCorrectly(final String s) {
if (mock != null) {
mock.tearDown();
}
mock = new MockUp<Dummy> {
#Mock
public void foo(Invocation inv) {
System.out.println(s);
inv.proceed();
}
}
}
And for the output:
B
O
Great :)
It's not clear what exactly happens here; apparently, at runtime some "chained mocking" is occurring.
The real problem is that both tests are doing something invalid with the MockUp API: they are mocking the same method in the same class twice in the same test. It is ok to have two different mock-ups for the same class in the same test, as long as they mock different methods/constructors.
The resulting behavior is undefined, as JMockit does not support multiple simultaneous mockings of the same method.
I have been trying to run the following test using mockito and junit and I keep on getting "java.lang.NullPointerException: name must not be null"
Can anyone tell me why this is happening?
On debugging, I found out that this exception is thrown when the test executes the following statement in isStopValid(String) method:
FacilityValidationUtil facUtil = new FacilityValidationUtil();
#RunWith(MockitoJUnitRunner.class)
public class MyFormTest{
#InjectMocks MyForm form = new MyForm();
#Mock FacilityValidationUtil facUtil;
#Test
public void testIsStopValid() throws FinderException{
when(facUtil.isFacilityValid("")).thenReturn(false);
form.setOrigin("");
assertEquals(false, form.isStopValid(form.getOrigin()));
}
}
Class with method to be tested:
public class MyForm{
FacilityValidationUtil facUtil = new FacilityValidationUtil();
public boolean isStopValid(String stop){
try {
return facUtil.isFacilityValid(stop);
} catch (FinderException e) {
log.error("Error finding the stop. "+e.getCause());
return false;
}
}
}
public class FacilityValidationUtil{
private FacilityDAO facilityDao = new HibernateFacilityDAO();
public boolean isFacilityValid(String facility) throws FinderException{
boolean test;
FacilityImpl facilityImpl = facilityDao.findFacilityByNassCode(facility);
test = (facilityImpl==null)?false : true;
return test;
}
}
public class HibernateFacilityDAO extends HibernateAbstractDeltaDAO implements FacilityDAO {
public HibernateFacilityDAO() {
super(false);
}
}
Short Answer: You are trying to mock a variable (facUtil) that is local to your isStopValid method, so the mock version of this object in your test is never going to be called because you are 'newing it up" each time.
Long Answer: It looks like you are trying to mock the call to your FacilityValidationUtil class, and if this is the case, then you need to either make the class a field so that Mockito can inject the object by reflection (if this object is thread safe, which it looks like it is) or explore a mocking framework like PowerMockito that will allow you to mock a constructor (google for PowerMockito when new).
PowerMockito.whenNew(FacilityValidationUtil.class).withNoArguments().thenReturn(facUtil);
Mockito doesn't support any mocking of constructor args by default.
EDIT
If you are still having trouble, then I would suggest starting with a smaller example. I've put together one for you that works and uses the code you are trying to test (It's using inner classes though, which Mockito has some quirky rules about, but I'm just doing it to compress the example).
#RunWith(MockitoJUnitRunner.class)
public class MyFormTest {
#InjectMocks
private MyForm form = new MyForm();
#Mock
private FacilityValidationUtil facUtil;
#Test
public void testIsStopValid_false() {
when(facUtil.isFacilityValid("")).thenReturn(false);
assertEquals(false, form.isStopValid(""));
}
#Test
public void testIsStopValid_true() {
when(facUtil.isFacilityValid("")).thenReturn(true);
assertEquals(true, form.isStopValid(""));
}
public class MyForm {
private FacilityValidationUtil facUtil = new FacilityValidationUtil();
public boolean isStopValid(String stop) {
try {
return facUtil.isFacilityValid(stop);
} catch (FinderException e) {
return false;
}
}
}
public class FacilityValidationUtil {
public boolean isFacilityValid(String facility) throws FinderException {
throw new RuntimeException(facility);
}
}
public class FinderException extends RuntimeException {
public FinderException(String message) {
super(message);
}
}
}
What's really important is that your mock is not getting injected correctly. Until you get that resolved, you are going to keep getting the same error. Set a break point in your MyForm at the point you call facUtil.isFaciltyValid and look at the object. It should be a mockito object, not your class.
Good luck.