Files
ProjectEulerJava/src/test/java/com/mattrixwv/project_euler/problems/BaseTest.java
2022-12-06 23:02:54 -05:00

76 lines
2.2 KiB
Java

package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.atMostOnce;
import static org.mockito.Mockito.verify;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import com.mattrixwv.Stopwatch;
import com.mattrixwv.project_euler.exceptions.Unsolved;
@ExtendWith(MockitoExtension.class)
@TestInstance(Lifecycle.PER_CLASS)
@TestMethodOrder(OrderAnnotation.class)
public abstract class BaseTest{
@Spy
protected Stopwatch timer;
protected static String description;
protected static String result;
public abstract void testDescription();
public void testDescription(Problem problem){
assertEquals(description, problem.getDescription());
assertFalse(problem.getSolved());
}
public abstract void testSolve();
public void testSolve(Problem problem){
//Verify the results cannot be seen until the problem has been run
assertThrows(Unsolved.class, () -> { problem.getResult(); });
problem.solve();
//Verify solved was set
assertTrue(problem.getSolved());
assertEquals(result, problem.getResult());
//Verify timer function were called
verify(timer, atLeastOnce()).start();
verify(timer, atLeastOnce()).stop();
//Verify the problem won't be run again
problem.solve();
//Verify solved is still set
assertTrue(problem.getSolved());
//Verify timer functions were not called again
verify(timer, atMostOnce()).start();
verify(timer, atMostOnce()).stop();
}
public abstract void testReset();
public void testReset(Problem problem){
assertTrue(problem.solved);
problem.reset();
//Verify solved was reset
assertFalse(problem.getSolved());
//Verify the problem can be solved again
testSolve();
}
}