Files
Application/app/src/test/java/com/example/boidelov3/data/ResultTest.java
T

80 lines
2.6 KiB
Java

package com.example.boidelov3.data;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Tests unitaires pour la classe Result.
*/
public class ResultTest {
@Test
public void testSuccess_createsSuccessfulResult() {
Result<String, Exception> result = Result.success("test data");
assertTrue("Result should be success", result.isSuccess());
assertFalse("Result should not be failure", result.isFailure());
assertEquals("Data should match", "test data", result.getData());
assertNull("Error should be null", result.getError());
}
@Test
public void testFailure_createsFailedResult() {
Exception error = new Exception("test error");
Result<String, Exception> result = Result.failure(error);
assertFalse("Result should not be success", result.isSuccess());
assertTrue("Result should be failure", result.isFailure());
assertNull("Data should be null", result.getData());
assertEquals("Error should match", error, result.getError());
}
@Test
public void testGetOrNull_returnsDataWhenSuccess() {
Result<String, Exception> result = Result.success("test data");
assertEquals("Should return data", "test data", result.getOrNull());
}
@Test
public void testGetOrNull_returnsNullWhenFailure() {
Result<String, Exception> result = Result.failure(new Exception("error"));
assertNull("Should return null", result.getOrNull());
}
@Test
public void testGetOrElse_returnsDataWhenSuccess() {
Result<String, Exception> result = Result.success("test data");
assertEquals("Should return data", "test data", result.getOrElse("default"));
}
@Test
public void testGetOrElse_returnsDefaultWhenFailure() {
Result<String, Exception> result = Result.failure(new Exception("error"));
assertEquals("Should return default", "default", result.getOrElse("default"));
}
@Test
public void testWithIntegerType() {
Result<Integer, Exception> result = Result.success(42);
assertTrue("Should be success", result.isSuccess());
assertEquals(Integer.valueOf(42), result.getData());
}
@Test
public void testWithCustomException() {
class CustomException extends Exception {
public CustomException(String message) { super(message); }
}
Result<String, CustomException> result = Result.failure(new CustomException("custom error"));
assertTrue("Should be failure", result.isFailure());
assertEquals("custom error", result.getError().getMessage());
}
}