At times when writing unit test cases for your Java project, you may want to fail a test based on the time a particular code takes to execute. For such cases, you can make use of the Junit assertTimeout static method from org.junit.jupiter.api.Assertions.
Below is a simple example, where we have a BusinessLogic class with a method complexCalculation that should complete within 10 seconds or else the unit test should fail.
Example:
package org.example;
public class BusinessLogic {
public void complexCalculation() throws InterruptedException {
//Some code that should complete
// in 10 seconds
Thread.sleep(12000);
}
}
import org.example.BusinessLogic;
import org.junit.jupiter.api.Test;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertTimeout;
public class BusinessLogicTest {
@Test
public void testComplexCalculationExecutionTime() {
BusinessLogic businessLogic = new BusinessLogic();
assertTimeout(Duration.ofSeconds(10), () -> {
businessLogic.complexCalculation();
});
}
}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- [Program] How to read three different values using Scanner in Java - Java
- Xcode 13 - unknown error compiling for iOS 15.0 but module has a minimum deployment target of iOS 15.2 - iOS
- Fix: Mac Screenshot opens in Mail Application - MacOS
- [fix] zsh: command not found: telnet on Mac - MacOS
- pip get list of all outdated Python packages - Python
- 5 Reasons Why Jupyter Notebook is Not Opening and Solutions - Python
- How to wrap column text in SharePoint Online Modern List Grid View using JSON formatting - SharePoint
- Java: Print Stack Trace as String Without Exception - Java