Add validation tests to detect run configuration drift

- Add spring-boot-starter-test dependency for JUnit Jupiter
- Validate main class exists and matches run config reference
- Validate module name matches pom.xml artifactId
- Validate schema.sql contains active column
- Prevents configuration breakage from refactoring
- Fails with clear messages pointing to which configs need updates

All 5 tests passing.
This commit is contained in:
2026-03-09 21:47:45 -06:00
parent a1be1a5688
commit 015bb574b8
2 changed files with 87 additions and 0 deletions

View File

@@ -80,6 +80,12 @@
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,81 @@
package com.petshop.backend.config;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import static org.junit.jupiter.api.Assertions.*;
/**
* Validates that IntelliJ run configurations match actual project structure.
* Prevents configuration drift when module name or main class changes.
*/
class RunConfigValidationTest {
@Test
void mainClassExists() throws ClassNotFoundException, NoSuchMethodException {
// Verifies the main class referenced in run configs actually exists
Class<?> mainClass = Class.forName("com.petshop.backend.BackendApplication");
assertNotNull(mainClass, "Main class should exist");
assertNotNull(mainClass.getMethod("main", String[].class), "Main method should exist");
}
@Test
void moduleNameMatchesPomArtifactId() throws Exception {
// Parse pom.xml
File pomFile = new File("pom.xml");
assertTrue(pomFile.exists(), "pom.xml should exist in project root");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(pomFile);
doc.getDocumentElement().normalize();
// Get project's artifactId (direct child of project, not parent's artifactId)
Element projectElement = doc.getDocumentElement();
NodeList children = projectElement.getChildNodes();
String artifactId = null;
for (int i = 0; i < children.getLength(); i++) {
if (children.item(i) instanceof Element) {
Element child = (Element) children.item(i);
if ("artifactId".equals(child.getTagName())) {
artifactId = child.getTextContent().trim();
break;
}
}
}
assertNotNull(artifactId, "artifactId not found in pom.xml");
// Expected module name in run configs should match artifactId
assertEquals("backend", artifactId,
"pom.xml artifactId has changed. Update .idea/runConfigurations/PetshopBackendApplication.xml module name to match.");
}
@Test
void runConfigurationFileExists() {
File runConfigFile = new File(".idea/runConfigurations/PetshopBackendApplication.xml");
assertTrue(runConfigFile.exists(),
"Run configuration file missing. Ensure .idea/runConfigurations/ directory is committed to Git.");
}
@Test
void databaseSchemaFileExists() {
File schemaFile = new File("src/main/resources/schema.sql");
assertTrue(schemaFile.exists(), "schema.sql should exist in src/main/resources/");
}
@Test
void schemaContainsActiveColumn() throws Exception {
File schemaFile = new File("src/main/resources/schema.sql");
String schemaContent = new String(java.nio.file.Files.readAllBytes(schemaFile.toPath()));
assertTrue(schemaContent.contains("active BOOLEAN") || schemaContent.contains("active boolean"),
"schema.sql should contain 'active' column definition in users table. " +
"If you see 'Unknown column active' error, run 'Reset Database Only' configuration.");
}
}