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!
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- How to declare Arrays in Java with Examples - Java
- Detect if Cookies are enabled using JavaScript - JavaScript
- How to hard reset Mac Terminal Window - MacOS
- Bootstrap Nav Menu Dropdown on hover - Bootstrap
- Converting Array into ArrayList - Java - Java
- Excel Fix: SECURITY RISK Microsoft has blocked macros from running because the source of this file is untrusted. - JavaScript
- Can Microsoft SharePoint Lists be synced and accessed offline without internet? - SharePoint
- Check version of pip package installer for Python - Python