Skip to content

Instantly share code, notes, and snippets.

testUserActive()
Starting process: IsUserActive
Found user: User(id='6dbcb954-ac34-4945-abdc-ff239c662069', banned=false)
Check user: User(id='6dbcb954-ac34-4945-abdc-ff239c662069', banned=false) is active and returning: true
Unregistering mocks
testUserInactive()
Starting process: IsUserActive
Found user: User(id='dcf81663-abfb-48c3-a181-d19b81c1d323', banned=false)
Check user: User(id='dcf81663-abfb-48c3-a181-d19b81c1d323', banned=false) is active and returning: false
class IsUserActiveProcessTest: BaseProcessTest() {
protected fun prepareEnvAndStartProcess(userExists: Boolean = true, userIsActive: Boolean = true): ProcessAssertions {
val mockUserService = mock(UserService::class.java)
val mockUser = User()
if (userExists) {
`when`(mockUserService.findUser(mockUser.id)).thenReturn(mockUser)
`when`(mockUserService.isBanned(mockUser)).thenReturn(!userIsActive)
@RunWith(SpringRunner::class)
@SpringBootTest
abstract class BaseProcessTest {
@Autowired protected lateinit var runtimeService: RuntimeService
abstract fun processName(): String
fun startProcess(variables: Map<String, Any> = mapOf()) {
logger.debug("Starting process: ${processName()}")
@Configuration
class TestConfiguration {
@Bean
fun flowableTestSpringProcessEngineConfig() = EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {
it.expressionManager = MockExpressionManager()
}
}
val userService = mock(UserService::class.java)
val serviceTask = BanUserServiceTask(userService)
@Component
class BanUserServiceTask @Autowired constructor(
val userService: UserService
)
@Component
class IsUserActiveServiceTask @Autowired constructor(
val userService: UserService
) {
fun checkUserExists(userId: String) {
userService.findUser(userId).let {
if (it == null) {
logger.error("User with id: $userId is not found")
throw BpmnError(ERR_USER_NOT_EXIST)
@Component
class BanUserServiceTask @Autowired constructor(
val userService: UserService
) {
fun ban(userId: String) {
userService.findUser(userId)?.let {
logger.debug("Banning user: $it")
userService.ban(it)
return
}
class User(val id: String): Serializable {
var banned = false
}
@Service
class UserService {
fun findUser(id: String): User? {
throw NotImplementedError()
}
fun isBanned(user: User): Boolean {
throw NotImplementedError()
}