Junit 3: How to run only one testcase from a test class - junit

I am using JUnit 3 and I have a test class which had 4 test methods.
CLass A:
test1()
test2()
test3()
test4()
Now I have written another test class B, which has the following methods:
test10()
test11()
test12()
Now, test10() requires test1() (of class A) as a pre requisite.
SO I would like to run only test1() from Class A, when inside test10() of class B.
Could someone please hep me with this?
Thanks and regards,
Sunny

This sounds like code that should be extracted to a helper class. But if you must do this, it is just regular Java code.
In test B:
public void test100() {
ClassA helper = new ClassA("");
helper.setUp();
helper.test1();
// your actual logic for test100
}
Also, consider better names if your tests are really named test100(), test101(), etc.

Related

Junit how to get invoking classname in parent #BeforeClass method?

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.

Mocking New Object Call In Superclass

I am having a hard time getting this one piece of mocking figured out for my unit tests. The classes in question are all part of legacy code that I don't really have the option of changing right now (I am hoping to be able to do some refactoring in the future, but need tests now).
Here are the two classes that I am dealing with, and the specific part I am having trouble with. Class A declares an object using new and then class B uses the object. I am trying to mock the object but I keep getting the real version of it instead of the mocked version.
public class B extends A(){
...
int x = problemObj.doMethod();
}
public class A(){
...
ProblemObj problemObj = new ProblemObj();
}
Here is my test class.
#RunWith(PowerMockRunner.class)
#PrepareForTest({A.class, B.class})
public class ATest(){
private ProblemObj problemObjMock;
#Before
public void setUp(){
problemObj = PowerMockito.Mock(ProblemObj.class);
}
#Test
public void test(){
PowerMockito.whenNew(ProblemObj.class).withNoArguments().thenReturn(problemObj);
...//rest of test below here
}
}
I have done other whenNew mocking in tests and set it up the same way as this. But for some reason this object being in the superclass is really throwing me off.
Versions used are Junit:4.11, Mockito:1.9.5, Powermock: 1.6.6

Difference between test suite, test case and test category

What is the difference between test suite, test case and test category.
I found a partial answer here
But what about categories?
Test case is a set of test inputs, execution conditions, and expected results developed to test a particular execution path. Usually, the case is a single method.
Test suite is a list of related test cases. Suite may contain common initialization and cleanup routines specific to the cases included.
Test category/group is a way to tag individual test cases and assign them to categories. With categories, you don't need to maintain a list of test cases.
Testing framework usually provides a way to specify which categories to include or exclude from a given test run. This allows you to mark related test cases across different test suites. This is useful when you need to disable/enable cases that have common dependencies (API, library, system, etc.) or attributes (slow, fast cases).
As far as I can understand, test group and test category are different names for the same concept used in different frameworks:
#Category in JUnit
#group annotation in PHPUnit
TestCategory in MSTest
Test Categories is like a sub Test Suite. Take this documentation by example. One a class file, you will have multiple test cases. A Test Suite is a grouping of Test classes that you want to run. A Test Category is a sub grouping of Test Cases. You could put a annotation at some test cases in your class file, and create to test suite pointing to the same test class, but filtering one of the suites to test only some categories. Example from documentation:
public interface FastTests { /* category marker */ }
public interface SlowTests { /* category marker */ }
public class A {
#Test
public void a() {
fail();
}
#Category(SlowTests.class)
#Test
public void b() {
}
}
#Category({SlowTests.class, FastTests.class})
public class B {
#Test
public void c() {
}
}
#RunWith(Categories.class)
#IncludeCategory(SlowTests.class)
#SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
// Will run A.b and B.c, but not A.a
}
#RunWith(Categories.class)
#IncludeCategory(SlowTests.class)
#ExcludeCategory(FastTests.class)
#SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
// Will run A.b, but not A.a or B.c
}
Notice that both Test Suite points to the same test classes, but they will run different test cases.

Junit: How to stubs the following java class in junit

Hi I have a class which the invoke the run() method of a thread from the constructor of the class by calling the start() method , So please help me to Stubs the so to write the junit test cases . The class is as follows
public class MyClass extends Thread {
Student st=null;
University uni= new University();
public MyClass(Student st) {
this.st=st;
start();
}
public void run() {
uni.calculate(st);
}
}
Thanks
Take a look at the discussion here:
Testing Constructor With Powermock
It discusses sub-classing and overriding.
In general it should be considered bad practice to have to mock the class under test in order to test it. It is also hard to do since most mocking frameworks will not allow mocking a single method once in the class under test since they create wrapping proxies.

How to add test suite dynamically in junit 4.10

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;
}
}