Brandon Jones
Brandon Jones

Reputation: 31

initializationError when running Junit test in static

I have been on a search recently for an answer to my question and I cannot seem to find it. I am trying to execute a simple test to run a JUnit test in static. When I try to execute the test I receive the following Failure.

java.lang.Exception: Method SimpleINt()should not be static.

I have JUnit 4 and hamcrest installed and pathsbuilt. (I am still new to Selenium/Java so I am sure there is an easy explanation for this.)

package iMAP;

import org.junit.Test;


public class Test1 {



@Test
public static  void SimpleINt ()
{
    int i = 34;
    System.out.println(i);
}
}

Upvotes: 1

Views: 886

Answers (2)

GhostCat
GhostCat

Reputation: 140457

The JUnit documentation for @Test states:

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure.

So, what is implicitly said here: the expectation is that @Test is only used for non static methods.

Beyond that: don't use keywords because you can. Learn what they mean. And static is something that you rather avoid (instead of using it all over the place).

Upvotes: 1

iamsankalp89
iamsankalp89

Reputation: 4739

Junit Methods should not be static as much I read and study. Just delete Static and try this:

package iMAP;

import org.junit.Test;

public class Test1 {

@Test
public void SimpleINt ()
{
    int i = 34;
    System.out.println(i);
}
}

Upvotes: 0

Related Questions