BeforeAll and AfterAll methods aren't being called #5544
-
|
I have the following test class: package daotest;
import java.sql.SQLException;
import org.junit.jupiter.api.Test;
import com.clone.hago_clone.models.EmployeeBean;
import com.clone.hago_clone.db.BaseDAO;
import com.clone.hago_clone.db.EmployeeDAO;
import org.junit.jupiter.api.AfterAll;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.BeforeAll;
/**
*
* @author Enrico Tuvera Jr
*/
public class TestEmployeeDao {
static BaseDAO createBase() {
System.out.println("CREATING BASEDAO INSTANCE!");
try {
BaseDAO bd = new BaseDAO(
"jdbc:mysql://localhost:3306/javaclass_test",
"root",
""
);
return bd;
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("IF YOU SEE THIS SOMETHING'S GONE WRONG!");
return null;
}
@BeforeAll
public static void createDatabaseTable() {
System.out.println("CREATING DATABASE TABLE!");
try {
BaseDAO bd = createBase();
EmployeeDAO ed = new EmployeeDAO(bd);
ed.createEmployeeTable();
} catch (SQLException e) {
System.out.println("COULDN'T CREATE EMPLOYEE TABLE!");
e.printStackTrace();
System.exit(1);
}
}
@AfterAll
public static void destroyDatabaseTable() {
System.out.println("DROPPING DATABASE TABLE!");
try {
BaseDAO bd = createBase();
EmployeeDAO ed = new EmployeeDAO(bd);
ed.dropEmployeeTable();
} catch (SQLException e) {
System.out.println("COULDN'T DROP EMPLOYEE TABLE!");
e.printStackTrace();
System.exit(1);
}
}
@Test
public void testCreateEmployee() {
BaseDAO bd = createBase();
EmployeeDAO ed = new EmployeeDAO(bd);
System.out.println("DOES THIS SHOW UP IN THE CONSOLE?!");
try {
EmployeeBean r = ed.addEmployee(
"admin",
"enrico",
"admin@admin.com",
"hunter2"
);
assertEquals("enrico", r.getName());
assertNotNull(r.getId());
} catch (SQLException e) {
fail(e.toString());
}
}
}I am expecting the @BeforeAll and @afterall methods to create / drop a database table before running the tests, but it does not seem to be working. The sole @test method is failing with a missing table exception, and I can't really figure out why. I have inserted System.out.println() calls in various places, and can see that the @BeforeAll and @afterall methods aren't even being called. createBase() and the @test method are being called. I have tried moving the createBase(), @BeforeAll and @afterall into a base class and inheriting, but I get the same error. I have tried decorating the class with @testinstance(TestInstance.LifeCycle.PER_CLASS) but I get the same error as well. I have tried messing with the visibilities, i.e. changing them from explicitly public to package-private, but I get the same error. Interestingly, changing the @test method to package-private causes it to not be run at all. I am positive that JUnit 5 has been set up correctly, my pom.xml's dependencies are as follows (and imports are at the top of the snippet above): <dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>${jakartaee}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.51.3.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.14.3</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc5</artifactId>
<version>11.2.0.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.49</version>
</dependency>
</dependencies>Screenshot of the test runner output: I really don't know what's going on here, could someone lend a hand? I realize that actually writing to a database might not be sound testing strategy but I'm not allowed to use any other frameworks / ORMs and the EmployeeDAO is emitting raw SQL under the hood so I feel there's some value in doing things this way. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
You have two problems. You can't solve both at the same time by "trying" things. If you solve one by accident, you won't notice because the other problem still blocks you. Problem 1: You don't know if your code works. So it is important to simplify your problems to the point the other problem isn't involved. To reduce the complexity of problem 1, you can exclude JUnit all together by creating a main method that calls To reduce the complexity of problem 2, you can clone https://github.com/junit-team/junit-examples and add the before all and after all hooks with a print statement to the If the One obvious problem I do see is that you're using the I also see that you're not using |
Beta Was this translation helpful? Give feedback.
-
|
As a side note, to me this sounds a lot like issues that many people have run into over the years where they were accidentally making use of Maven Surefire's POJO Test support. The signature of your test method matches that: So, it could be that you have not properly configured Maven Surefire to use the JUnit Platform and the JUnit Jupiter test engine. |
Beta Was this translation helpful? Give feedback.

You have two problems. You can't solve both at the same time by "trying" things. If you solve one by accident, you won't notice because the other problem still blocks you.
Problem 1: You don't know if your code works.
Problem 2: You don't know if JUnit's before and after all hooks work.
So it is important to simplify your problems to the point the other problem isn't involved.
To reduce the complexity of problem 1, you can exclude JUnit all together by creating a main method that calls
createDatabaseTable,testCreateEmployeeanddestroyDatabaseTablein order. Then run that main method. If that doesn't work you'll have to figure out why. We can't help you with that - JUnit is not involved.T…