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
|
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
本文标签:
版权声明:本文标题:java - How do I test a Temporal Activity where the activity method implementation is annotated with Spring @Transactional? - Sta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745168131a2645807.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Activity.getExecutionContext()
– Martin Frank Commented Feb 21 at 6:45