mirror of
https://github.com/google/nomulus
synced 2026-09-20 23:14:44 +00:00
Add TmOverrideExtension for more safe TM overrides in tests (#1382)
* Add TmOverrideExtension for more safe TM overrides in tests This is safer to use than calling setTmForTest() directly because this extension also handles the corresponding call to removeTmOverrideForTest() automatically, the forgetting of which has been a source of test flakiness/instability in the past. There are now broadly two ways to get tests to run in JPA: either use DualDatabaseTest, an AppEngineExtension, and the corresponding JPA-specific @Test annotations, OR use this override alongside a JpaTransactionManagerExtension.
This commit is contained in:
+5
-2
@@ -132,13 +132,16 @@ public class TransactionManagerFactory {
|
||||
/**
|
||||
* Sets the return of {@link #tm()} to the given instance of {@link TransactionManager}.
|
||||
*
|
||||
* <p>DO NOT CALL THIS DIRECTLY IF POSSIBLE. Strongly prefer the use of <code>TmOverrideExtension
|
||||
* </code> in test code instead.
|
||||
*
|
||||
* <p>Used when overriding the per-test transaction manager for dual-database tests. Should be
|
||||
* matched with a corresponding invocation of {@link #removeTmOverrideForTest()} either at the end
|
||||
* of the test or in an <code>@AfterEach</code> handler.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public static void setTmForTest(TransactionManager newTm) {
|
||||
tmForTest = Optional.of(newTm);
|
||||
public static void setTmOverrideForTest(TransactionManager newTmOverride) {
|
||||
tmForTest = Optional.of(newTmOverride);
|
||||
}
|
||||
|
||||
/** Resets the overridden transaction manager post-test. */
|
||||
|
||||
Reference in New Issue
Block a user