I have TestNG tests in some classes, which extends the same main class.
I need the same last test for class1 and class2, but if I add #Test(priority = 666) to main class it start after all classes.
How I should annotate #Test in main class what would it starts after all tests of each class?
Big thanks. Also sorry for bad english.
main class
public class MainTest {
#BeforeClass()
public void setup() {
//something
}
#AfterClass()
public void tearDown() {
//something
}
#AfterMethod()
public void log_writer(Method method) {
//something
}
#Test(priority = 666) {}
}
class1
public class Class1 extends MainTest {
#Test
public void test1(){}
#Test
public void test2(){}
}
and class2
public class Class2 extends MainTest {
#Test
public void test1(){}
#Test
public void test2(){}
}
what you are looking for is the #AfterClass annotation. handle the part where you want to check logs in the AfterClass annotated method
It's not possible. Basically each #Test runs only once. If you need to do something after each test class you have to use #AfterClass annotation on method in your MainTest. You can do some hacks with method order etc. in MethodInterceptor (http://testng.org/doc/documentation-main.html#methodinterceptors) but it's not good idea for this case.
Related
I have grouped my test methods in 2 Test Groups like so -
#TestGroup("integration")
public abstract static class AbstractIntegrationTest {
#ClassRule
public static TestGroupRule rule = new TestGroupRule();
}
#TestGroup("unit")
public abstract static class AbstractUnitTest {
#ClassRule
public static TestGroupRule rule = new TestGroupRule();
}
And have my test methods defined this way -
public static class UnitTest extends AbstractUnitTest {
#Test
public void Test1() {...}
#Test
public void Test2() {...}
}
public static class IntegrationTest extends AbstractIntegrationTest {
#Test
public void Test3() {...}
#Test
public void Test4() {...}
}
When I try to run one of my test methods (say Test1) from the Intellij IDE, I get an error saying -
org.junit.AssumptionViolatedException: None of the test groups [unit] are enabled. Enabled test groups: []
How can I enable my test group and if I am running a specific test, do I need to still enable anything?Have I set this up completely wrong?
Thanks.
A class implementing interface ensures that all the methods in the interface are defined in the class, likewise, is there any way to tell the JVM that some specific annotations should be used in implementing class. Consider the following interface
public interface TestClass{
#BeforeClass
public void setup();
#AfterClass
public void tearDown();
}
and following is the implementing class
public class TestScripts implements TestClass{
#BeforeClass
public void setup(){
/*method body*/
}
#AfterClass
public void tearDown(){
/*method body*/
}
}
All I want is that the compiler should show error if #BeforeClass annotation is not used with the method public void setup() and #AfterClass annotation is not used with the method public void tearDown(). Any way to achieve this?
You can't enforce this but what you can enforce is that a sub-class implements an abstract method.
public abstract class TestClass {
#BeforeClass
public void beforeClass(){
setup();
}
protected void abstract setup();
#AfterClass
public void afterClass(){
tearDown();
}
protected void abstract tearDown();
}
and then any sub-class has to implement these methods and they will be called based on the annotation.
Is it possible to place the setup/teardown methods using JUnit framework in a single class (which would be my baseclass) so on test runs they methods are always called first/last? it would be in a similar way to which nunit tests can be structured
currently the only way I can get my tests to kick off is if I have the setup/teardown methods within the same class as my tests are (which is something I wan't to avoid, to keep my test classes tidy)
example I would hope to set up;
public class baseclass
{
#Before
public void setUp
{}
#After
public void tearDown
{}
}
public class tests
{
#Test
public void test1
{
// test content here
}
}
Run this test and see the sequence of events
class Test1 {
#Before
public void setUp1() {
System.out.println("setUp1");
}
}
public class Test2 extends Test1 {
#Before
public void setUp2() {
System.out.println("setUp2");
}
#Test
public void test() {
System.out.println("test");
}
}
Yes, as long as your test class extend your baseclass.
For instance:
Suite
#RunWith(Suite.class)
#SuiteClasses(Tests.class)
public class AllTests {
}
BaseClass
public class BaseClass {
#BeforeClass
public static void beforeAll() {
}
#Before
public void setUp() {
}
#After
public void tearDown {
}
#AfterClass
public static void afterAll() {
}
}
Tests
public class Test extends BaseClass {
#Test
public void test1() {
}
#Test
public void test2() {
}
}
I'm experimenting with the JUnit Enclosed runner in order to try and improve the organisation of some of my tests. At the moment I'm trying to work out how to share some setup between the inner classes.
Attempt the first:
#RunWith(Enclosed.class)
public class EnclosedTest {
#Before
public void printSomething() {
System.out.println("Helllooo Meggan");
}
public static class FirstTest {
#Test
public void assertThatSomethingIsTrue() {
assertThat(true, is(true));
}
}
public static class SecondTest {
#Test
public void assertThatSomethingIsFalse() {
assertThat(false, is(false));
}
}
}
Unfortunately, no-one says hello to Meggan. If I update an inner class to extend the outer one, then I get the following:
java.lang.Exception: class 'org.scratch.EnclosedTest$FirstTest' (possibly indirectly) contains itself as a SuiteClass
at org.junit.runners.model.InitializationError.<init>(InitializationError.java:32)
Is there a particular Enclosed idiom to use when trying to share setup between inner test classes? I was hoping it would be as simple as the C# example I found.
Enclosed runner internally works as a Suite, that is, it runs the classes as Test cases. And since Junit 4.12 abstract inner classes are ignored by Enclosed runner.
That said the way to share set up is to create an abstract class containing it (#Before, #After):
#RunWith(Enclosed.class)
public class EnclosedTest {
abstract public static class SharedSetUp {
#Before
public void printSomething() {
System.out.println("Helllooo Meggan");
}
}
public static class FirstTest extends SharedSetUp {
#Test
public void assertThatSomethingIsTrue() {
assertThat(true, is(true));
}
}
public static class SecondTest extends SharedSetUp {
#Test
public void assertThatSomethingIsFalse() {
assertThat(false, is(false));
}
}
}
Notice that you can even declare custom runners for each subclass.
i'm getting a nullpointerexception when i run the junit test in eclipse. what am i missing here?
MainTest
public class MainTest {
private Main main;
#Test
public void testMain() {
final Main main = new Main();
main.setStudent("James");
}
#Test
public void testGetStudent() {
assertEquals("Test getStudent ", "student", main.getStudent());
}
#Test
public void testSetStudent() {
main.setStudent("newStudent");
assertEquals("Test setStudent", "newStudent", main.getStudent());
}
}
setters and getters are in the Main class
Main
public String getStudent() {
return student;
}
public void setStudent(final String studentIn) {
this.student = studentIn;
}
thanks.
You need to initialize your main object before using it
You can do it either on an #Before method or inside the test itself.
OPTION 1
Change
#Test
public void testSetStudent() {
main.setStudent("newStudent");
assertEquals("Test setStudent", "newStudent", main.getStudent());
}
to
#Test
public void testSetStudent() {
main = new Main();
main.setStudent("newStudent");
assertEquals("Test setStudent", "newStudent", main.getStudent());
}
OPTION 2
Create a #Before method, when using #Before the main field will be created before any #Test is executed, there is another option, option 3, to use #BeforeClass
#Before
public void before(){
main = new Main();
}
OPTION 3
#BeforeClass
public static void beforeClass(){
//Here is not useful to create the main field, here is the moment to initialize
//another kind of resources.
}
Every test method gets a new instance of MainTest. This means that the changes you make in your first method won't show up in your second method, and so on. There is no sequential relationship between one test method and another.
You need to make each method a self-contained test that tests one aspect of your class's behaviour.