At times you may be testing your code to get results back within a certain amount of time, if the result is not returned during x seconds or minutes, your test case should be considered to fail. In such cases make use of the org.junit.rules.Timeout class from JUnit unit test library.
Calculation.javapackage org.code2care;
public class Calculation {
public int someComplexAlog(int no) throws InterruptedException {
//....
Thread.sleep(5000);
return no;
//...
}
public int someSimpleAlog(int no) throws InterruptedException {
//....
Thread.sleep(1000);
return no;
//...
}
}
CalculationTest.java
package org.code2care.test;
import static org.junit.Assert.assertEquals;
import org.code2care.Calculation;
import org.junit.Test;
public class CalculationTest {
@Test(timeout = 4500)
public void complexAlogTimeout4500() throws InterruptedException {
Calculation calculation = new Calculation();
int no = 20;
assertEquals(20, calculation.someComplexAlog(no));
}
@Test(timeout = 1500)
public void complexAlogTimeout1500() throws InterruptedException {
Calculation calculation = new Calculation();
int no = 10;
assertEquals(10, calculation.someSimpleAlog(no));
}
}
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- How to see HTTP Request Response Headers in Google Chrome Browser - Chrome
- How to save IntelliJ IDE Console logs to external log file - Android-Studio
- [git] fatal: your current branch 'main' does not have any commits yet - Git
- How to know docker Engine details - Docker
- 🎃 Trending, Popular Halloween hashtags for year 2020 🎃 [Facebook, Twitter, Instagram, Snapchat] - Hashtags
- Fix error The tool was unable to install the Web Server (IIS) Role during SharePoint 2019 Setup - SharePoint
- Clear browsing history in Safari on Mac Ventura 13 - MacOS
- How to docker remove a container when it exits - Docker