admin管理员组

文章数量:1414928

I have an activity + implementation

@ActivityInterface
interface Foo {
  @ActivityMethod
  long bar();
}

@Component
@RequiredArgsConstructor
public class FooImpl implements Foo {
  private final SomeJpaRepository repo;
  @Transactional
  public long bar() {

    System.out.println("Here");
    final var processingDate =
      Instant.ofEpochMilli(
        Activity.getExecutionContext()
                .getInfo()
                .getStartedTimestamp())
             .atZone(ZoneId.systemDefault())
             .toLocalDate();
    System.out.println(processingDate);
    var ret = repo.someJpaOperation();
    System.out.println("There");
    return ret;
  }
}

I tried to test it like this

@DataJpaTest
class MyTest {
  @Autowired
  private FooImpl fooImpl;
  private TestActivityEnvironment testActivityEnvironment;
  @BeforeEach
  void setUpTestActivity() {
    testActivityEnvironment = TestActivityEnvironment.newInstance();
    testActivityEnvironment.registerActivitiesImplementations(fooImpl);
  }
  @Test
  void testFoo() {
    val act = testActivityEnvironment.newActivityStub(Foo.class);
    assertThat(act.bar()).isPositive();
  }
}

But it will only show the Here and the processing date, but not the There

I want to avoid creating a whole workflow, I just wanted to test the activity method.

Right now my workaround is to simply have a test path.


    long epochMilli;
    try {
      epochMilli = Activity.getExecutionContext().getInfo().getStartedTimestamp();
    } catch (IllegalStateException e) {
      epochMilli = Instant.now().toEpochMilli();
    }
    final var processingDate = Instant.ofEpochMilli(epochMilli).atZone(ZoneId.systemDefault()).toLocalDate();

I have an activity + implementation

@ActivityInterface
interface Foo {
  @ActivityMethod
  long bar();
}

@Component
@RequiredArgsConstructor
public class FooImpl implements Foo {
  private final SomeJpaRepository repo;
  @Transactional
  public long bar() {

    System.out.println("Here");
    final var processingDate =
      Instant.ofEpochMilli(
        Activity.getExecutionContext()
                .getInfo()
                .getStartedTimestamp())
             .atZone(ZoneId.systemDefault())
             .toLocalDate();
    System.out.println(processingDate);
    var ret = repo.someJpaOperation();
    System.out.println("There");
    return ret;
  }
}

I tried to test it like this

@DataJpaTest
class MyTest {
  @Autowired
  private FooImpl fooImpl;
  private TestActivityEnvironment testActivityEnvironment;
  @BeforeEach
  void setUpTestActivity() {
    testActivityEnvironment = TestActivityEnvironment.newInstance();
    testActivityEnvironment.registerActivitiesImplementations(fooImpl);
  }
  @Test
  void testFoo() {
    val act = testActivityEnvironment.newActivityStub(Foo.class);
    assertThat(act.bar()).isPositive();
  }
}

But it will only show the Here and the processing date, but not the There

I want to avoid creating a whole workflow, I just wanted to test the activity method.

Right now my workaround is to simply have a test path.


    long epochMilli;
    try {
      epochMilli = Activity.getExecutionContext().getInfo().getStartedTimestamp();
    } catch (IllegalStateException e) {
      epochMilli = Instant.now().toEpochMilli();
    }
    final var processingDate = Instant.ofEpochMilli(epochMilli).atZone(ZoneId.systemDefault()).toLocalDate();
Share Improve this question edited Feb 24 at 8:53 cyberbrain 5,1551 gold badge16 silver badges29 bronze badges asked Feb 21 at 3:34 Archimedes TrajanoArchimedes Trajano 42.1k29 gold badges216 silver badges359 bronze badges 2
  • what happens, when you debug the test? i strongly expect that you run into an error, when you try to access one of these Activity.getExecutionContext() – Martin Frank Commented Feb 21 at 6:45
  • 1 without the workaround there is an error because it is expecting to run in an Activity environment. What I am trying to find out is how to get it tested in a TestActivityEnvironment and still work with Spring Data JPA Transactions – Archimedes Trajano Commented Feb 21 at 15:45
Add a comment  | 

1 Answer 1

Reset to default 1

you have to mock the repository

when you start your test, the variable private final SomeJpaRepository repo; is still null, so you have to mock/inject it.

@DataJpaTest
class MyTest {
  @Autowired 
  private SomeJpaRepository repo; //adding this will prevent the NPE
  @Autowired 
  private FooImpl fooImpl;
  private TestActivityEnvironment testActivityEnvironment;
  
  @BeforeEach
  void setUpTestActivity() { ... }
  @Test
  void testFoo() { ... }
}

if you want to mock the repo use the @InjectMocks annotation instead of @Autowired

source: https://www.baeldung/junit-datajpatest-repository

本文标签: