Skip to content

Commit

Permalink
Perlito5 - misc/Java-Asm-Interpreter/MethodExecutorAsm docs
Browse files Browse the repository at this point in the history
  • Loading branch information
fglock committed Aug 2, 2024
1 parent d0da852 commit 0707fda
Showing 1 changed file with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
Organizing a test suite for a Java project involves setting up a structured environment where you can write, manage, and execute tests efficiently. Here’s a step-by-step guide to help you organize your test suite:
1. Choose a Testing Framework

The most commonly used testing frameworks for Java are:

JUnit: Widely used for unit testing.
TestNG: Similar to JUnit but with more advanced features.

For this guide, we'll use JUnit 5.
2. Project Structure

Organize your project directory to separate source code and test code. A common structure is:

my-java-project/
├── src/
│ ├── main/
│ │ └── java/
│ │ └── com/
│ │ └── example/
│ │ └── MyClass.java
│ └── test/
│ └── java/
│ └── com/
│ └── example/
│ └── MyClassTest.java
├── build.gradle (or pom.xml for Maven)
└── ...

3. Set Up Your Build Tool

If you are using Gradle, add the following to your build.gradle:

plugins {
id 'java'
}

repositories {
mavenCentral()
}

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
}

test {
useJUnitPlatform()
}

If you are using Maven, add the following to your pom.xml:

<project>
...
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
</project>

4. Writing Tests

Create test classes in the src/test/java directory. Each test class should correspond to a class in the src/main/java directory.

Example MyClass.java:

package com.example;

public class MyClass {
public int add(int a, int b) {
return a + b;
}
}

Example MyClassTest.java:

package com.example;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;

public class MyClassTest {

@Test
public void testAdd() {
MyClass myClass = new MyClass();
assertEquals(5, myClass.add(2, 3), "2 + 3 should equal 5");
}
}

5. Running Tests

Gradle: Run ./gradlew test to execute the tests.
Maven: Run mvn test to execute the tests.

6. Organizing Tests

Unit Tests: Test individual methods and classes. Place these in the src/test/java directory.
Integration Tests: Test the interaction between multiple components. You can create a separate directory like src/integrationTest/java if needed.
Test Suites: Group related tests together. JUnit 5 provides the @Suite annotation for this purpose.

Example of a test suite:

package com.example;

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({ MyClassTest.class, AnotherClassTest.class })
public class AllTestsSuite {
}

7. Continuous Integration

Integrate your test suite with a CI/CD pipeline (e.g., GitHub Actions, Jenkins, Travis CI) to automatically run tests on every commit or pull request.

Example GitHub Actions workflow (.github/workflows/ci.yml):

name: Java CI

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
- name: Build with Gradle
run: ./gradlew build

0 comments on commit 0707fda

Please sign in to comment.