对反应式编程感兴趣,我玩了一点构建反应式 RESTful Web 服务指南。并想向前移动并添加一些单元测试。
我试图RouterFunction用普通的 Junit/Mockito 测试来测试我的处理程序 ( )。但是,因为它是响应式的,处理程序返回一个Mono<ServerResponse>. 所以我不得不用block()它来测试ServerResponse状态,但无法提取他的身体来测试它。在网上搜索解决方案时,似乎所有示例都使用WebTestClient.
我的问题是:
鉴于所有样本都WebTestClient用于测试反应式 REST 服务,并且(单元)测试ServerResponse. 对 a 进行单元测试是一种很好的做法,RouterFunction还是始终使用 进行更广泛的测试更好WebTestClient?
非常感谢。
java unit-testing reactive-programming spring-boot spring-webflux
Liskov 替换原则指出
如果
S是 的子类型T,则类型的对象T可以替换为类型的对象,S而不会改变该程序的任何所需属性。
但是,在 Scala 中,PartialFunction并非在所有情况下都适用/定义。
trait Function1 {
def apply(x: A): R
}
trait PartialFunction extends Function1 {
def apply(x: A): R
def isDefinedAt(x: A): Boolean
}
Run Code Online (Sandbox Code Playgroud)
如果将 aPartialFunction应用于未定义的值,您将收到异常。
PartialFunction在 Scala 中创建一个的方便方法是使用模式匹配。这样做,您会收到一个MatchError未定义的值。
val fn:Function1[String, Int] = s => s.length
val pf:PartialFunction[String, Int] = {
case "one" => 3
}
def program(f:Function1[String, Int], s:String):(Boolean, Int) = (
f.isInstanceOf[Function1[String, Int]], f(s)
)
program(fn, "one") == …Run Code Online (Sandbox Code Playgroud) 使用 spring webflux 和@RestController模型,我有@RestControllerAdvice一些@ExceptionHandler方法。
我想获取原始请求作为参数,因为我想记录它并在我的响应中使用它。
但是,我已经尝试了经典 MVC 模型中处理程序方法的所有可能类型,但没有一个被接受(HttpServletRequest,WebRequest和ServerRequest)。
我可以使用什么类型来访问 webflux 注释处理程序方法中的原始请求?
我有一个 Android 应用程序,我想对其进行单元测试。通过 MVP 模式,我能够在“android 世界”之外提取许多类,以便在单独的模块中将它们作为普通单元测试(使用 Junit)进行测试[1]。但是,我想记录这些课程的一些消息。所以我尝试slf4j-api与android绑定一起使用。旨在为我的测试提供简单的绑定。但是“ test ”模块首先抱怨类路径中有两个 slf4j 绑定,并且他正在使用 android 绑定。
所以我的问题是,如何slf4j-android从“测试”模块中排除依赖项?这是我的“测试”模块的build.gradle
evaluationDependsOn(":app")
apply plugin: 'java'
dependencies {
def app = project(':app')
testCompile project(path: ':app', configuration: 'debugCompile')
def debugVariant = app.android.applicationVariants.find({it.name == 'debug'})
testCompile debugVariant.javaCompile.classpath
testCompile debugVariant.javaCompile.outputs.files
testCompile files(app.plugins.findPlugin("com.android.application").getBootClasspath())
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock-api-mockito:1.6.1'
testCompile 'org.assertj:assertj-core:1.7.1'
}
Run Code Online (Sandbox Code Playgroud)
[1] http://blog.blundell-apps.com/how-to-run-robolectric-junit-tests-in-android-studio/
android unit-testing gradle build.gradle android-gradle-plugin
如何测试接收和返回的处理程序方法?ServerRequestMono<ServerResponse>
我可以创建一个ServerRequestvia org.springframework.mock.web.reactive.function.server.MockServerRequest.builder()。并断言ServerResponse.statusCode()。但是,我想测试它的主体,ServerResponse但是没有办法提取它。
ServerResponse response = target.search(MockServerRequest.builder()
.method(HttpMethod.GET)
.build()
).block();
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK);
//assertThat(response.body()).isNotNull();
Run Code Online (Sandbox Code Playgroud)
我不想使用进行更广泛的测试WebTestClient,我想使用单元测试来测试所有可能的响应情况。
谢谢
使用vuex-module-decorator,我有一个authenticate应该改变状态的动作。
@Action
public authenticate(email: string, password: string): Promise<Principal> {
this.principal = null;
return authenticator
.authenticate(email, password)
.then(auth => {
const principal = new Principal(auth.username);
this.context.commit('setPrincipal', principal);
return principal;
})
.catch(error => {
this.context.commit('setError', error);
return error;
});
}
// mutations for error and principal
Run Code Online (Sandbox Code Playgroud)
但这失败,并显示以下消息:
未处理的承诺拒绝错误:“ ERR_ACTION_ACCESS_UNDEFINED:您是否要尝试在@Action中访问this.someMutation()或this.someGetter?仅在动态模块中有效。如果不是动态的,请使用this.context.commit(” mutationName“,有效负载)和this.context.getters [“ getterName”]
我不明白的是,它与@MutationActionand 一起使用效果很好async。但是我想念返回类型Promise<Principal>。
@MutationAction
public async authenticate(email: string, password: string) {
this.principal = null;
try {
const auth = await authenticator.authenticate(email, …Run Code Online (Sandbox Code Playgroud) 对存储库和聚合的思考。文献称每个聚合都有一个存储库。
但是,如果我的聚合都是基类的子类(是一种关系,则继承不用于重用)。我是否必须为所有子类创建存储库,或者我可以为所有子类使用相同的存储库。
PaperBag paperBag = paperBagsRepository.get(paperBagId);
PlasticBag plasticBag = plasticBagsRepository.get(plasticBagId);
Run Code Online (Sandbox Code Playgroud)
或者
PaperBag paperBag = bagsRepository.get(paperBagId);
PlasticBag plasticBag = bagsRepository.get(plasticBagId);
Run Code Online (Sandbox Code Playgroud) abstract-class domain-driven-design ddd-repositories aggregateroot
我有一个form-el谁只是一个容器,必须div用特定的类包装所有的孩子.div我希望允许from-el包装它们,而不是在每个表单元素中重复这个.我可以遍历所有元素并将其包装在其他html标记内吗?
// Markup
<form-el>
<input-el label="name" type="text" />
<span>This must also be wrapped</span>
</form-el>
// Would produce
<form>
<div class="form-field">
<label>name</label>
<input type="text" name="name" />
</div>
<div class="form-field">
<span>This must also be wrapped</span>
</div>
</form>
// Where '<div class="form-field">' is produced by 'from-el'
Run Code Online (Sandbox Code Playgroud) 我开始使用领域驱动设计,并对ValueObject有疑问:
它们可以包含不变量或其他规范吗?
考虑一个不可变的ValueObject:
ValueObject (
prop integer: Int
prop string: String
// Value and copy constructor
// Observers for integer and string
// Equality methods on integer and string value
)
Run Code Online (Sandbox Code Playgroud)
我可以添加一些不变量,例如integer > 0 & < 42。或者它们必须是没有任何逻辑的简单传输?
我希望他们可以,但需要确认。
我想使用该存储库中的 cats-saga:https://github.com/VladKopanev/cats-saga
然而我被困在这段代码上OrderSagaCoordinator.scala L160:
def apply[F[_]: Sync: Concurrent: Timer: Sleep: Parallel](
paymentServiceClient: PaymentServiceClient[F],
loyaltyPointsServiceClient: LoyaltyPointsServiceClient[F],
orderServiceClient: OrderServiceClient[F],
sagaLogDao: SagaLogDao[F],
maxRequestTimeout: Int
): F[OrderSagaCoordinatorImpl[F]] =
Run Code Online (Sandbox Code Playgroud)
它是什么F,它从哪里来,有人可以解释一下那段代码吗?
谢谢
编辑:我知道什么是通用类型。然而,在这种情况下apply,调用该方法时不会指定具体类型,而且我看不到它来自的任何地方。
(for {
paymentService <- PaymentServiceClientStub(randomUtil, clientMaxReqTimeout, flakyClient)
loyaltyPoints <- LoyaltyPointsServiceClientStub(randomUtil, clientMaxReqTimeout, flakyClient)
orderService <- OrderServiceClientStub(randomUtil, clientMaxReqTimeout, flakyClient)
xa = Transactor.fromDriverManager[IO]("org.postgresql.Driver", "jdbc:postgresql:Saga", "postgres", "root")
logDao = new SagaLogDaoImpl(xa)
orderSEC <- OrderSagaCoordinatorImpl(paymentService, loyaltyPoints, orderService, logDao, sagaMaxReqTimeout)
// ...
Run Code Online (Sandbox Code Playgroud) 这可能是一个愚蠢的问题,但从 ZIO 开始,我无法设法将 a 转换Seq[ZIO]为ZIO[Seq]:
def translate(keys: Seq[String], locales: Seq[Locale]):RIO[Translator, Seq[Translation]] = {
for {
service <- ZIO.environment[Translator]
} yield {
// service.translate produce a zio.Task[Translation]
keys.map(k => service.translate(k, locales)
}
}
Run Code Online (Sandbox Code Playgroud)
必需:RIO[翻译器,Seq[翻译]]
找到:ZIO[翻译器,无,Seq[zio.Task[翻译]]
我尝试过,flatMap但是flatten我无法与任何人取得预期的结果。collectAllmerge
如何将 a 转换Seq[ZIO[_, _, B]]为 a ZIO[_, _, Seq[B]]?
谢谢
编辑:这似乎ZIO.foreach是最好的选择,但是由于 for 理解,我仍然将它包裹在另一个 ZIO 中。
我将通过https://www.scala-exercises.org/获取Cats。我想我明白是什么意思了Apply.ap。但我看不到它有任何用途。
有什么区别:
Apply[Option].map(Some(1))(intToString)
Run Code Online (Sandbox Code Playgroud)
和
Apply[Option].ap(Some(intToString))(Some(1))
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下或指出更多解释吗?
我们有一个显示一种地图的应用程序.这个Swing应用程序主要绘制一组java.awt.Shapes,java.awt.Graphics2D#draw(Shape)一切都很好.
现在,我必须扩展这个应用程序,以允许我们的用户编辑(移动形状)地图.但是没有translate或move方法java.awt.Shape.所以我无法改变java.awt.Point形状的位置().
我试图覆盖java.awt.Shape#getPathIterator以应用与形状位置匹配的翻译.但这很棘手,因为这个方法已经接受了我必须合并的转换,并且因为要正确PathIterator应该从那开始,(0, 0)因为它是相对于形状位置.
无论如何,这是行不通的,因为它似乎Graphics2D并不总是使用这种方法来绘制java.awt.Shape.
所以现在,我感觉有点迷茫.另一个解决方案可能是让自己绘制形状,但后来我必须重写应用程序的一部分.这不是问题,但我必须知道哪个可能是最好的解决方案:
找到一招移到一个java.awt.Shape
似乎是最好的解决方案,但我想不出如何做到这一点.
将应用程序更改为具有自绘图形状
可能很好但是我必须计算myslef contains和其他更复杂的方法.
scala ×4
unit-testing ×3
java ×2
scala-cats ×2
spring ×2
spring-boot ×2
android ×1
applicative ×1
build.gradle ×1
drawing ×1
functor ×1
generics ×1
gradle ×1
invariants ×1
java-2d ×1
liskov-substitution-principle ×1
polymer ×1
swing ×1
testing ×1
typeclass ×1
vue.js ×1
vuex ×1
vuex-modules ×1
zio ×1