'@After ,@before not working in testcase
I have started testing and now i want to use @After, @Before and @Test but my application only runs the @Before method and gives output on console
before
However, if I remove @After and @Before it runs the @Test. My code is here:
public class TestPractise extends AbstractTransactionalDataSourceSpringContextTests{
@Before
public void runBare(){
System.out.println("before");
}
@Test
public void testingMethod(){
System.out.println("testing");
}
@After
public void setDirty(){
System.out.println("after");
}
}
Why aren't @After, @Test and @before working simultaneously?
Solution 1:[1]
Use @BeforeEach instead of @Before and @AfterEach instead of @After.
Solution 2:[2]
Check that you are using Junit4 because from Junit5 onwards @Before/@After is now @BeforeEach/@AfterEach and similalry @BeforeClass/@AfterClass is @AfterAll/@BeforeAll.
Solution 3:[3]
It should work... But since you are working with spring framework and JUnit 4 was introduced years ago I's suggest you to use annotations instead of inheritance.
So, annotate you class with @RunWith(SpringJUnit4ClassRunner.class). Remove extends AbstractTransactionalDataSourceSpringContextTests.
Don't forget to make the @Before and @After methods static
Now it should work.
Even if you want to extend Spring abstract test classes at least pay attention that some of them are deprecated. For example class AbstractTransactionalDataSourceSpringContextTests is deprecated.
Solution 4:[4]
If you use auto import in an IDE, make sure the @Test and @Before are imported from the org.junit package.
Solution 5:[5]
in my case, I had that problem the solution was to change the java access modifier, It was way private.
before (not working) @Test void validate() throws Exception {}
after (working) @Test public void validate() throws Exception {}
Solution 6:[6]
JUnit Jupiter, aka "JUnit 5": use @BeforeAll
If you use the newer JUnit Jupiter (Java 8 onward), you'll want to replace @Before with @BeforeAll.
Furthermore, you'll need to either annotate your test class with @TestInstance(Lifecycle.PER_CLASS) or make the @BeforeAll method static. Here's an example:
@TestInstance(Lifecycle.PER_CLASS)
class MyTestClass {
MyHeavyResource sharedResource;
@BeforeAll
void init() {
System.out.println("init");
sharedResource = new MyHeavyResource(1234);
}
@Test
void myTest() {
System.out.println("myTest");
sharedResource.methodUnderTest();
}
}
Understanding Lifecycle.PER_CLASS
The likely reason JUnit 5 is more stringent with this -- demanding either static or Lifecycle.PER_CLASS -- is that it wants the test author to acknowledge that any resource instance initialized in a @BeforeAll method will genuinely be shared across each individual unit test method within the class. This could compromise their isolation, for example if the sharedResource in the above example isn't stateless/idempotent.
If sharedResource cannot be safely shared (or if it's reasonably leightweight), the init method should be annotated with @BeforeEach instead, which would create a new instance before executing each individual test within the class.
The Javadoc for TestInstance explain how using Lifecycle.PER_CLASS actually enforces a single instance of the test class; whereas the behaviour of JUnit 4 and earlier was equivalent to Lifecycle.PER_METHOD, which created a new instance of the test class for each @Test method contained therein. This would somewhat mislead the author to suppose that @Before was only executed once for each of those tests.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | anothernode |
| Solution 2 | Anurag Bharti |
| Solution 3 | Franz Ebner |
| Solution 4 | user514949 |
| Solution 5 | Mauricio Carrero |
| Solution 6 | sxc731 |
