Replies: 3 comments
-
| Have you considered dependency injection through a custom  | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            -
| One option would be to provide a  import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedClass;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Named.named;
@ParameterizedClass
@MethodSource("parameters")
public class ParameterizedClassResourceTests {
    static Stream<Named<Supplier<EventStore>>> parameters() {
        return Stream.of(
                named("In-Memory", InMemoryEventStore::new),
                named("CSV File", CsvFileEventStore::new)
        );
    }
    final EventStore eventStore;
    ParameterizedClassResourceTests(Supplier<EventStore> eventStoreSupplier) {
        eventStore = eventStoreSupplier.get();
    }
    @Test
    void test1() {
        eventStore.add("foo");
        assertTrue(eventStore.contains("foo"));
    }
    @Test
    void test2() {
        assertTrue(eventStore.isEmpty());
    }
    abstract static class EventStore {
        private final Set<String> state = new HashSet<>();
        public void add(String value) {
            state.add(value);
        }
        public boolean contains(String value) {
            return state.contains(value);
        }
        public boolean isEmpty() {
            return state.isEmpty();
        }
    }
    static class InMemoryEventStore extends EventStore {
    }
    static class CsvFileEventStore extends EventStore {
    }
} | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            -
| I recommend to implement the parameter as an enum with factory methods:     enum EventStoreType { InMemory, CSV_File;
        EventStore<CustomerId, CustomerEvent, Customer> forCustomers() {
            switch (this) {
                case InMemory: return InMemoryEventStore.forCustomers();
            }
            throw new Error("Store-type not implemented: " + name());
        }
        EventStore<ConcertId, ConcertEvent, Concert> forConcerts() {
            return switch (this) {
                case InMemory -> InMemoryEventStore.forConcerts();
                case CSV_File -> CsvFileEventStore.forConcerts();
            };
        }
    }Therewith           @ParameterizedTest(name = "Using {0} Storage")
-        @MethodSource("concertEventStoreSupplier")
-        void findByIdForNonExistingConcertReturnsEmptyOptional(String storageType, EventStore<ConcertId, ConcertEvent, Concert> concertStore) {
+        @EnumSource
+        void findByIdForNonExistingConcertReturnsEmptyOptional(EventStoreType storageType) {
+            var concertStore = storageType.forConcerts();I would modify the parameterized class in this manner:      @ParameterizedClass
-    @MethodSource("customerEventStoreSupplier")
+    @EnumSource(value=EventStoreType.class, names = "InMemory")
     @Nested
     class CustomerEventStoreTest {
 
-        static Stream<EventStore<CustomerId, CustomerEvent, Customer>> customerEventStoreSupplier() {
-            return Stream.of(InMemoryEventStore.forCustomers());
+        CustomerEventStoreTest(EventStoreType storageType) {
+            customerStore = storageType.forCustomers();
         }
 
-        @Parameter
         EventStore<CustomerId, CustomerEvent, Customer> customerStore; | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Uh oh!
There was an error while loading. Please reload this page.
-
I have a bunch of parameterized tests that all take the same arguments, so wanted to use
@ParameterizedClassto reduce the amount of duplication. However, it appears that arguments provided are the same ("singleton") for all tests. I get this might be the intention, but I'm wondering if there's a way to have it call the@MethodSourceto obtain fresh arguments for each test method (to provide test isolation)?For an example, see https://github.com/jitterted/jitterticket-event-sourced/blob/f056b07970977f6caa74c3c3b93e930678417099/src/test/java/dev/ted/jitterticket/eventsourced/application/EventStoreTest.java#L40
The issue is that I need a "fresh" EventStore, so, e.g., need to call
InMemoryEventStore.forConcerts()for each test method to ensure there isn't stray data in the store.(Is there a better way to reduce the duplication other than
@ParameterizedClassif that's not the intention of that annotation?)Beta Was this translation helpful? Give feedback.
All reactions