Amrit
Amrit

Reputation: 2333

Where is 'main' function invoked for JUnit test case

I have a sample JUnit test case which works absolutely fine however I am not sure how did it start executing as it does not contain main or any runner class function. Can anyone share how is JUnit test case executed without main function?

The code is:

import static org.junit.Assert.assertEquals;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class testJUnit {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
    System.out.println("Before Class");
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
    System.out.println("Tear Down After Class");
}

@Before
public void setUp() throws Exception {
    System.out.println("Setup");
}

@After
public void tearDown() throws Exception {
    System.out.println("Tear Down");
}

@Test
public void test() {
    int a = 1;
    assertEquals("Testing...", 1, a);
}

@Ignore
@Test
public void test1() {
    int a = 156;
    assertEquals("Testing...", 156, a);
}

}

Upvotes: 0

Views: 1345

Answers (4)

Guillaume Poussel
Guillaume Poussel

Reputation: 9822

When you run a JUnit test, you are in fact executing org.junit.runner.JUnitCore and providing your fully qualified class name as argument.

You are probably using an IDE, like Eclipse or Netbeans, which handle all that logic for you. See @Matthew Farwell answer on this subject.

Source : jUnit FAQ (2006)

Upvotes: 2

Matthew Farwell
Matthew Farwell

Reputation: 61705

When running a JUnit test from Eclipse, Eclipse runs another JVM which has it's own main class (RemoteTestRunner if you're interested). This does effectively the same job as JUnitCore, but with changes specific for Eclipse - it needs to pass the results back to Eclipse.

For more information, see my answer to How does Eclipse actually run Junit tests?

Upvotes: 2

Raúl
Raúl

Reputation: 1552

It is handled by the framework itself. Execution occurs in order of @Before, @Test & @After ..

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

The class doesn't need a main method. jUnit contains a main method for execution and at some point jUnit creates an instance of the test class.

Upvotes: 0

Related Questions