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
|
1 Answer
Reset to default 1you 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
Activity.getExecutionContext()
– Martin Frank Commented Feb 21 at 6:45