📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
1. Create Simple Maven Project
mvn archetype:generate
-DgroupId=org.yourcompany.project
-DartifactId=application
2. Project Packaging Structure
- src/main/java – This folder contains Java source code packages and classes
- src/main/resources – This folder contains non-java resources, such as property files and Spring configuration
- src/test/java – This folder contains the test source code packages and classes
- src/test/resources – This folder contains non-Java resources, such as property files and Spring configuration
── pom.xml
└── src
├── main
│ ├── java
│ │ └── com
│ │ └── javadevelopersguide
│ │ └── junit
│ │ └── Calculator.java
│ ├── resources
└── test
├── java
│ └── com
│ └── javadevelopersguide
│ └── junit
│ └── CalculatorTest.java
└── resources
3. Update JUnit Dependencies in pom.xml File
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
4. Create Calculator.java class
public class Calculator { public int evaluate(String expression) { int sum = 0; for (String summand : expression.split("\\+")) sum += Integer.valueOf(summand); return sum; } }
Create a JUnit Test for Calculator Clas
- Create a method and annotate a method with @org.junit.Test
- When you want to check equality, import org.junit.Assert.* statically, call assertEquals(), and pass expected and actual values.
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculatorTest {
@Test
public void evaluatesExpression() {
Calculator calculator = new Calculator();
int sum = calculator.evaluate("1+2+3");
assertEquals(6, sum);
}
}
5. Run the test
mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building junit-getting-started 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ junit-getting-started ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ junit-getting-started ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Git_Work\junit-developers-guide\junit-getting-started\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ junit-getting-started ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ junit-getting-started ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to E:\Git_Work\junit-developers-guide\junit-getting-started\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ junit-getting-started ---
[INFO] Surefire report directory: E:\Git_Work\junit-developers-guide\junit-getting-started\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.developersguide.junit.CalculatorTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.069 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.022 s
[INFO] Finished at: 2018-06-27T21:49:29+05:30
[INFO] Final Memory: 11M/28M
[INFO] ------------------------------------------------------------------------
Conclusion
GitHub Repository: JUnit Developers Guide
Comments
Post a Comment
Leave Comment