假设我有以下界面:
public interface ISomething {
default int doStuff() {
return 2 * getValue();
}
int getValue();
}
Run Code Online (Sandbox Code Playgroud)
当我现在模仿这个界面时:
@Mock
private ISomething _something;
@Before
public void setup() {
doCallRealMethod().when(_something).doStuff();
}
Run Code Online (Sandbox Code Playgroud)
并尝试测试doStuff()方法,如下所示:
@Test
public void testDoStuff() {
when(_something.getValue()).thenReturn(42);
assertThat("doStuff() returns 84", _something.doStuff(), is(84));
}
Run Code Online (Sandbox Code Playgroud)
我希望测试成功,但我得到:
org.mockito.exceptions.base.MockitoException:
Cannot call real method on java interface. Interface does not have any implementation!
Calling real methods is only possible when mocking concrete classes.
Run Code Online (Sandbox Code Playgroud)
我尝试ISomething使用这样的抽象类进行子类化:
public abstract class Something implements ISomething {
}
Run Code Online (Sandbox Code Playgroud)
像上面那样嘲笑这个类.通过这种方法,我得到了同样的结果.
Mockito不支持调用默认实现吗?
我正在使用Spring Boot和Spring Boot JPA编写一个组件.我有这样的设置:
界面:
public interface Something {
// method definitions
}
Run Code Online (Sandbox Code Playgroud)
实施:
@Component
public class SomethingImpl implements Something {
// implementation
}
Run Code Online (Sandbox Code Playgroud)
现在,我有一个运行的JUnit测试SpringJUnit4ClassRunner,我想用它测试我的SomethingImpl.
当我做
@Autowired
private Something _something;
Run Code Online (Sandbox Code Playgroud)
它有效,但是
@Autowired
private SomethingImpl _something;
Run Code Online (Sandbox Code Playgroud)
导致测试失败抛出一条NoSuchBeanDefinitionException消息No qualifying bean of type [com.example.SomethingImpl] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
但是在测试用例中,我想明确地注入my,SomethingImpl因为它是我想要测试的类.我怎么做到这一点?
我正在尝试mvn release:prepare在Gitlab服务器上托管的多模块项目.
在pom.xml我的主POM包含:
<scm>
<connection>scm:git:http://my-git-server.example.com/git/somebody/my-project.git</connection>
<url>http://my-git-server.example.com/git/somebody/my-project</url>
</scm>
Run Code Online (Sandbox Code Playgroud)
当我这样做时mvn release:prepare -DautoVersionSubmodules=true,它编译所有内容并运行测试,但随后失败:
[INFO] Executing: /bin/sh -c cd /home/somebody/git/my-project && git tag -F /tmp/maven-scm-1594218362.commit my-project-1.0.0
[INFO] Working directory: /home/somebody/git/my-project
[INFO] Executing: /bin/sh -c cd /home/somebody/git/my-project && git push http://my-git-server.example.com/git/somebody my-project-1.0.0
[INFO] Working directory: /home/somebody/git/my-project
...
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-release-plugin:2.3.2:prepare (default-cli) on project iwes-lib-master: Unable to tag SCM
[ERROR] Provider message:
[ERROR] The git-push command failed.
[ERROR] Command output:
[ERROR] fatal: repository 'http://my-git-server.example.com/git/somebody/' not found
[ERROR] …Run Code Online (Sandbox Code Playgroud) 我有一个大型多模块SBT项目.一个模块是Play2项目.现在,默认情况下,IntelliJ IDEA排除整个target文件夹,IDEA自己的编译器不会compile从sbt 运行目标,但会尝试自己解决所有问题.这导致重要的Play2部分,如未构建的路由和反向路由器.
当我sbt compile从控制台运行时,这些组件是构建的,但由于它们target位于IDEA中默认排除的文件夹中,因此IDEA忽略了那里的所有内容.
每次切换Git分支时(Play2项目目前只在一个分支中)我必须进入项目结构并手动调整排除的文件夹.
是不是有可能让IDEA自己解决这个问题,并sbt compile在重建项目时打电话?
我有一个返回Try对象的方法:
def doSomething(p: SomeParam): Try[Something] = {
// code
}
Run Code Online (Sandbox Code Playgroud)
我现在想用ScalaTest测试它.目前我这样做:
"My try method" should "succeed" in {
val maybeRes = doSomething(SomeParam("foo"))
maybeRes.isSuccess shouldBe true
val res = maybeRes.get
res.bar shouldBe "moo"
}
Run Code Online (Sandbox Code Playgroud)
但是检查isSuccess是true因为选项和序列有类似的东西看起来有点笨拙should be(empty)和shouldNot be(empty).我找不到类似的东西should be(successful).
这是存在还是我的方法真的要走了?
我刚刚在Windows机器上安装了Kafka(来自Confluent Platform)。我启动了Zookeeper和Kafka并创建主题,并从中制作作品并从中使用作品。但是,一旦删除主题,卡夫卡就会崩溃,如下所示:
PS C:\confluent-4.1.1> .\bin\windows\kafka-topics.bat -zookeeper 127.0.0.1:2181 --topic foo --create --partitions 1 --replication-factor 1
Created topic "foo".
PS C:\confluent-4.1.1> .\bin\windows\kafka-topics.bat -zookeeper 127.0.0.1:2181 --topic foo --delete
Topic foo is marked for deletion.
Note: This will have no impact if delete.topic.enable is not set to true.
Run Code Online (Sandbox Code Playgroud)
这是崩溃的输出:
[2018-06-08 09:44:54,185] ERROR Error while renaming dir for foo-0 in log dir C:\confluent-4.1.1\data\kafka (kafka.server.LogDirFailureChannel)
java.nio.file.AccessDeniedException: C:\confluent-4.1.1\data\kafka\foo-0 -> C:\confluent-4.1.1\data\kafka\foo-0.cf697a92ed5246c0977bf9a279f15de8-delete
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:387)
at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287)
at java.nio.file.Files.move(Files.java:1395)
at org.apache.kafka.common.utils.Utils.atomicMoveWithFallback(Utils.java:697)
at kafka.log.Log$$anonfun$renameDir$1.apply$mcV$sp(Log.scala:579)
at kafka.log.Log$$anonfun$renameDir$1.apply(Log.scala:577)
at …Run Code Online (Sandbox Code Playgroud) 在scala中,我可以要求多个特征,如下所示:
def foo(bar: Foo with Bar): Unit = {
// code
}
Run Code Online (Sandbox Code Playgroud)
这在F#中也是可能的,还是我必须显式声明一个FooBar继承Foo和接口Bar?
我正在将 Scalamock 与 ScalaTest 一起使用,并尝试模拟 Java 接口。我目前有:
private val _iface = mock [MyInterface]
Run Code Online (Sandbox Code Playgroud)
现在我想做
_iface expects `someMethod returning "foo" once
Run Code Online (Sandbox Code Playgroud)
但是编译器没有找到expects.
我进口org.scalatest._和org.scalamock.scalatest._。我还缺少什么?
我有一个奇怪的问题:有时ScalaTests在IDEA中失败。我得到的是:
An exception or error caused a run to abort. This may have been caused by a problematic custom reporter.
java.lang.NoSuchMethodError: scala.runtime.ObjectRef.create(Ljava/lang/Object;)Lscala/runtime/ObjectRef;
at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:2347)
at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1044)
at org.scalatest.tools.Runner$$anonfun$runOptionallyWithPassFailReporter$2.apply(Runner.scala:1043)
at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:2722)
at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:1043)
at org.scalatest.tools.Runner$.run(Runner.scala:883)
at org.scalatest.tools.Runner.run(Runner.scala)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2(ScalaTestRunner.java:138)
at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Run Code Online (Sandbox Code Playgroud)
其他有相同问题的人应该设置一个选项“在编译器过程中运行工作表”,但是我在我的IDEA中找不到它。我没有文件->设置-> Scala应该位于的位置。
我该怎么办?
编辑:我研究了更多,真正奇怪的是:这是一个多模块SBT构建,并且在某些项目中测试有效。我创建了一个新模块,添加了一个非常简单的测试,并且该测试运行。只有一个模块中的测试失败,并显示此错误。
EDIT2:这似乎是捆绑的SBT编译器中的错误,请参阅此链接。现在,我清理了目录,删除了.ivy2文件夹,然后尝试重新导入所有内容,看看是否有任何更改。
我的里面有这样的东西build.sbt:
lazy val someDeps = Seq(
libraryDependencies += "com.example" %% "foo" % "1.3.37",
// more
)
lazy val some_library = project.in(file("libs/somelibrary")).
settings(commonSettings).
settings(
// project-specific settings
libraryDependencies ++= someDeps
)
lazy val something_with_deps_provided = project.in(file("swdp")).
settings(commonSettings).
settings(
// project-specific settings
libraryDependencies ++= someDeps.map(d => d % "provided")
).dependsOn(some_library)
Run Code Online (Sandbox Code Playgroud)
当我现在使用sbt-assembly-plugin创建 的程序集时something_with_deps_provided,它仍然将依赖项放入生成的 jar 中,忽略provided. 是否可以provided稍后设置传递依赖,如果可以,是如何完成的?
scala ×4
scalatest ×3
sbt ×2
apache-kafka ×1
confluent ×1
f# ×1
git ×1
gitlab ×1
hibernate ×1
java ×1
maven ×1
mockito ×1
sbt-assembly ×1
scalamock ×1
spring ×1
spring-boot ×1
spring-mvc ×1
windows ×1