I have implemented a generic TestBase.java and many test case class extends this base.
#BeforeClass method is to prepare some specific data for corresponding test case class.
My question is, is it possible to get the invoking test case classname in TestBase #BeforeClass method so that I can use the classname to get correct data prepared?
I don't want to implement the #BeforeClass in separate test case class, as the steps are totally the same, the only difference is the data name which can be generated by test case classname.
For example:
My project is to test query function of our product.
Test case classes like GenericQueryTest.java, BooleanQueryTest.java etc.
Each test case class need to index prepared data before run test and cleanup the environment after all tests in the testcase class finish.
I implemented a TestBase.java like following.
public class TestBase {
#BeforeClass
public static void setUpBeforeClass() throws Exception {
I want to get invoked test case class name here. if I get the name(eg. classname= "GenericQueryTest ")
File testDataFile = new File("C:/users/" + classname +".csv";
then read the "C:/users/GenericQueryTest.csv "to prepare data in env
}
public class GenericQueryTest extends TestBase{
I donot need to implement #BeforeClass
#Test
.....
#Test
...
}
I figured out how to resolve the problem. Just implement customized runner. And you can get Class from the runner.
Related
I want to test SomeClass methods.
For that, I need SomeClass instance in every test so I'm using #Before annotation and initiate an instance of SomeClass named SC.
The problem is:- How can I test the constructor function after I already use it? It doesn't make sense.
Additional question:- The constructor can get number of arguments and they can influnce the methods outputs, should I mock this class instead of creating an instance of it?
public class SomeClassTest {
SomeClass SC;
#Before
public void initlize() throws IOException{
SC= new SomeClass (argument1,argument2,..);
}
#Test
public void ConstructorTest() {
}
Just don't use the object SC in your ConstructorTest. If you wan't to test a certain outcome from the construction of a SomeClass object with certain parameters then just construct it as such within your ConstructorTest and then assert the relevant outcomes you expect on the newly constructed object.
And no you shouldn't be mocking this class. The test is for testing this class so if you mock it's behaviour then you aren't really testing anything.
I am writing a TestNG testcase which extends atgdustcase. I need to mock a method in class B. This class B is injected into class A using properties file in ATG. Class C is a test class for testing functionality of class A. D is a test class which extends AtgDustCase.
#RunWith(PowerMockRunner.class)
#PrepareForTest({A.class, B.class})
Public class c extends D{
#InjectGlobalComponent(Path for component A)
#InjectMocks
private A aClass;
public void testMethod(){
String string = "abc";
Map map = new HashMap();
map.put("a", "c");
aClass = getRequest.resolveName(Component A path);
B b = PowerMockito.mock(B.class);
A a = PowerMockito.spy(new A());
PowerMockito.whenNew(A.class).withNoArguments().thenReturn(a);
a.setB(B);
PowerMockito.when(a.getB()).thenReturn(b);
Mockito.stub(b.getMethodToMock(string)).toReturn(map);
Mockito.verify(b).getMethodToMock(string);
aClass.invokeTestMethod();// This method calls the b.getMethodToMock()
}
}
I need to mock getMethodToMock(). When I execute invokeTestMethod(), this calls getMethodToMock(). At that time, map should be returned. Instead it is executing getMethodToMock() and it is throwing an error(The problem with execution is I need to fetch some records from DB. I need to mock this method and return a map which contains information retrieved from DB). I am not sure whether mocking is happening properly or not, as in debug mode I am able to see that the getMethodToMock() is getting invoked. Please help me how can I mock this method and skip the execution of this method.
Note: I tried using Powermockito, but my testcase is TestNGsuite. I need to run TestNGSuite instead of running it as Junit.
#RunWith(DataProviderRunner.class)
#RunWith(SpringJUnit4ClassRunner.class)
public class DatabaseModelTest {
// some tests
}
or
#RunWith(Parameterized.class)
#RunWith(SpringJUnit4ClassRunner.class)
public class DatabaseModelTest {
// some tests
}
We can not use two runner property in one test case class...!! so that
I want to run test case with Multiple data how i pass multiple parameter in Rest web service to execute test case ??
Any solution for extend class for DataProviderRunner or parameterized ??
Thanks
(stayconnected52)
You could use Spring's JUnit rules instead of the SpringJUnit4ClassRunner. This works at least with the Parameterized runner. I don't know whether it works with the DataProviderRunner, too.
You need at least version 4.2.0 of the Spring framework and spring-test.
#RunWith(Parameterized.class)
public class DatabaseModelTest {
#ClassRule
public static final SpringClassRule SCR = new SpringClassRule();
#Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
...
}
I tested the solution of #Stefan and works also well for #RunWith(DataProviderRunner.class)
I found a second solution in DataProvider for Spring Integration Testing, they wrote a class DataProviderRunnerWithSpring and set the test class like:
#RunWith(DataProviderRunnerWithSpring.class)
public class TestClass{
...
}
So I'm trying to run tests that will evaluate certain properties of different websites. The actual evaluation is being handled by a pay-per-call resource, so I want to minimize the number of times I generate the resource. Also, I need this to run in JUnit to fit into a larger automated test suite.
I've been doing this with parameterized tests so far, but I just learned that they instantiate a new instance for each test method.
Now I'm trying to figure out a way to have the resource created just once for each parameter that is being fed into the constructor of my testing class. #BeforeClass does it just once, and #Before does it once before each test.
All the help topics I've been able to find have dealt with creating expensive resources once for all tests, but in this case I need the resource to be recreated for each new set of parameters.
I've written some example code / output below to better show what I'm looking for:
#RunWith(Parameterized.class)
public class MyTestClass {
private static Resource expensiveToCreateResource;
public MyTestClass(String url) {
System.out.println("Constructing resource for " + url);
expensiveToCreateResource = new Resource(url); //This is getting created 4x, which is wrong
}
#Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {{"url1"},{"url2"}});
}
#Test
public test1() {
expensiveToCreateResource.method1();
System.out.println("test1");
}
#Test
public test2() {
expensiveToCreateResource.method2();
System.out.println("test2");
}
}
would produce output:
Constructing resource for url1
test1
test2
Constructing resource for url2
test1
test2
Any ideas / solutions? Thanks.
If you want to have the class instantiated once per parameter, you'll have to write your own JUnit test runner. Instead I'd try to cache the information as needed, e.g. in a static map that maps URLs to resources.
I wanted to know if there's any way to add test suites dynamically in junit 4.
For example I have a TestClassA as mentioned below having test case "test1"
class TestClassA
{
#Test
public void test1()
{
createTestClassDynamically(); // this creates a test class having
// setUp(), tearDown() methods and one test case .
}
}
Test case test1 has a method createTestClassDynamically() that dynamically creates a new test class (lets say TestClassB) having setUp(), tearDown() methods and one test case (lets say test2()).
I want to run the test1 and then when TestClassB is dynamically generated I want test case "test2" also to be executed.
I know this is quite complicated and not the best thing to do but in my framework I need to do it to generate large number of test classes dynamically rather than having them physically in the package.
Can anyone please provide any help/suggestions?
I have solved this is my framework using the Parameterized feature of Junit 4 which helps to execute same test case with different parameters.
Below mentioned is the sample code on how I acheived it, thought to post it if it helps anyone.
Also, if someone has a better solution, feel free to post it.
class TestClassA
{
private TestClassB classBObj;
public TestClassA(TestClassB obj) {
classBObj= obj;
}
#Test
public void test1()
{
// createTestClassDynamically(); // remove this method as Parameterized
// feature will take care of dynamic test execution.
}
#Test
public void test2()
{
// Test case from Test class B using TestClassB object (classBObj)
}
public static Collection<Object[]> getParameters() {
Collection<Object[]> parameteres = new ArrayList<Object[]>();
Object[] obj1 = new Object[]{new TestClassB()};
Object[] obj2 = new Object[]{new TestClassB()};
parameteres.add(obj1);
parameteres.add(obj2);
// ....... add more test data this way or create a loop
return parameteres;
}
}