0

I have two classes.

One with a JUnit Test as follows:

public class ValidateResponseCodeTest {

    @ClassRule
    //code here

    @BeforeClass
    //more code

    @Test
    public void sendAndReceiveCodeCheck() {
        // test code goes here
        System.out.println("only this ran!");
    }
}

Then I have another class which i want to use to invoke that test sendAndReceiveCodeCheck

Public class AllTests {

    @ClassRule
    //code here

    @BeforeClass
    //more code

    private final ValidateResponseCodeTest validateResponseCodeTest;

    public AllTests() {
        validateResponseCodeTest = new ValidateResponseCodeTest();
    }

    @Test
    public void runTest() {
        ValidateResponseCodeTest.sendAndReceiveCodeCheck();
    }
}

How can i ensure that the method runTest() ONLY runs the code within test method sendAndReceiveCodeCheck and not run all code in class: ValidateResponseCodeTest

Ideally, I don't want to remove annotations or touch ValidateResponseCodeTest class in any way.

Saffik
  • 911
  • 4
  • 19
  • 45
  • Remove the annotations in ValidateResponseCodeTest. – user1766169 Apr 24 '18 at 10:25
  • Any other way to do it without removing them? – Saffik Apr 24 '18 at 10:29
  • Don't know. Why do you have e.g. the Before-annotation if you don't want the code to execute? – user1766169 Apr 24 '18 at 10:33
  • 1
    Can you explain why you need it? Do you need to run a selection of tests from a TestClass depending on configuratoin/profile? (i.e run only 5, not 10, if profile is "default"). There might be another way around it, without having to call the method yourself – Cargeh Apr 24 '18 at 10:33
  • I have about 20 tests in 20 classes. I wanted to call all tests within one class that will reference all those 20 tests. Each test in a class does some setup stuff, which is common and I just wanted to do it once in the for e.g: AllTests. Each time I invoke a test, I don't want it to do it 20 times hence only wanting to run the test method only. – Saffik Apr 24 '18 at 10:42
  • @Saffik Remove the Test-annotation from ValidateResponseCodeTest and move the Before-annotation to AllTests. – user1766169 Apr 24 '18 at 11:45
  • @user1766169 - appreciate your response but is there another way to do it without me changing even 1 line in `ValidateResponseCodeTest` – Saffik Apr 24 '18 at 12:02
  • @Saffik why don't you do the necessary configuration using BeforeClass annotation instead Before so? (BeforeClass runs that configuration before any test, and Before runs it before every test) – Eduardo Meneses Apr 24 '18 at 17:22
  • @Saffik more details [here](https://stackoverflow.com/questions/20295578/difference-between-before-beforeclass-beforeeach-and-beforeall) – Eduardo Meneses Apr 24 '18 at 17:27
  • @EduardoMeneses - Great, I have changed it to @BeforeClass now. The reason I want to ONLY execute code from class `ValidateResponseCodeTest.sendAndReceiveCodeCheck` and nothing else is because that "setup code" will be done once in class `AllTests` - The setup is creating docker containers. I don't want to create them again in `ValidateResponseCodeTest` leading to duplicate container creation. So any idea how I can just call the TEST-CODE only from `ValidateResponseCodeTest`? – Saffik Apr 25 '18 at 13:18
  • @Saffik I didn't ever saw this using java. You can do it using [maven](http://maven.apache.org/plugins-archives/maven-surefire-plugin-2.12.4/examples/single-test.html) (mvn -Dtest=TestCircle#mytest test) or using [gradle](https://stackoverflow.com/questions/22505533/how-to-run-only-one-test-class-on-gradle?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) maybe (gradle test --tests org.gradle.SomeTest.someSpecificFeature). If you call single test you will both call BeforeClass and Before always – Eduardo Meneses Apr 25 '18 at 14:30

0 Answers0