Tag: unit testing

指定定制应用上下文

我们正在使用泽西弹簧3将泽西1.x的一些数据服务从泽西1.x迁移到泽西2.x. 我们有几个inheritance自JerseyTest的测试类。 其中一些类使用未在web.xml文件中指定的自定义applicationContext.xml文件。 在Jersey 1.x中,扩展JerseyTest的测试类可以使用WebappDescriptor.Builder调用超级构造函数,可以传递上下文参数来设置或覆盖应用程序上下文路径。 例如 public MyTestClassThatExtendsJerseyTest() { super(new WebAppDescriptor.Builder(“com.helloworld”) .contextParam( “contextConfigLocation”, “classpath:helloContext.xml”) .servletClass(SpringServlet.class) .contextListenerClass(ContextLoaderListener.class) .requestListenerClass(RequestContextListener.class).build()); } 如何用Jersey 2.x实现同样的目标? 我已经梳理了API文档 , 用户指南和一些来源,但无法找到答案。 谢谢。

Maven没有运行Spring Boot测试

我有一个我要测试的restSpring Boot REST API。 我可以在Eclipse中手动运行测试(没有maven并通过运行应用程序作为JUnit测试)并且它运行正常并显示结果,但mvn test不起作用,因为您将在下面找到。 这是我的POM文件: http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0 org.demo rest 0.0.1-SNAPSHOT war UserRegistrationServices RESTful API org.springframework.boot spring-boot-starter-parent 1.2.5.RELEASE UTF-8 1.8 junit junit test org.springframework.boot spring-boot-starter-data-mongodb org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-security org.springframework.boot spring-boot-starter-tomcat provided org.springframework.boot spring-boot-starter-test test com.jayway.restassured rest-assured 2.4.1 test com.jayway.restassured json-schema-validator 2.4.1 test com.fasterxml.jackson.core jackson-core 2.5.4 com.fasterxml jackson-xml-databind 0.6.2 commons-codec commons-codec 1.10 spring-snapshots http://repo.spring.io/snapshot […]

用Java测试加密和解密的单元

免责声明,我是iOS开发人员,一直在使用Android上的加密技术。 目前我已经设法在Android中实现加密,但我问自己一个单元如何测试加密和解密数据? 现在想到的第一个想法是: String encryptedInputData = encryptedInputData(“Hello”); String decryptedData = decryptData(encryptedInputData); Assert.assertEquals(decryptedData,”Hello”); 然而,这个测试提出了一个缺陷……如果在encryptedInputData和decryptData方法中确实发生了某些变化,那么这个测试就不会告诉它改变了什么以及为什么它现在正在破坏。 所以我想编写更细粒度的测试。 所以例如给出这个代码: Cipher cipher = Cipher.getInstance(“RSA/ECB/NoPadding”); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] data = cipher.doFinal(message); 我想确保cipher变量在ECB模式下使用RSA算法而没有填充。 我想测试.doFinal(message)中的.doFinal(message)遵循特定格式等。 现在我想象我能够模拟Cipher类,这里的问题是编写的加密和解密,仅作为Util类,并且能够对此进行unit testing,我将不得不通过模拟Cipher进入代码,假设这是一个Util类似乎会变得混乱,即我必须创建一个init方法只是为了unit testing目的或创建setter方法只是为了unit testing这个。 这将允许我对代码进行unit testing,但然后Util类使用我实际上不需要用于生产目的的代码变得笨拙。 是否有任何优雅的方式可以像这样unit testing场景? 即encryptedInputData和decryptData是公共方法,但这些方法使用各种私有方法,坦率地需要进行unit testing,问题是如何?

Java中的代码覆盖与EclEmma不扫描期望exception方法

我正在尝试使用Eclipse和EclEmma在java中获取代码覆盖率。 我的测试是使用JUnit 4,我有一些看起来像这样的测试: @Test(expected = IllegalArgumentException.class) public void createTime_withInvalidMinuteUnder0_throws(){ //Arrange … //Act Something triggering IllegalArgumentException Here } EclEmma说测试失败了,因为抛出了IllegalArgumentException。 因此它会丢弃我的代码覆盖率指标,即使它应该抛出一些东西。 有没有选项让它看到JUnit预期的exception标记? 编辑:我发现如果你把测试添加到测试声明中,它就可以了!

使用junit @Rule,expectCause()和hamcrest匹配器

我有一个测试: @Rule public ExpectedException thrown = ExpectedException.none(); … @Test public void testMethod() { final String error = “error message”; Throwable expectedCause = new IllegalStateException(error); thrown.expectCause(org.hamcrest.Matchers.equalTo(expectedCause)); someServiceThatTrowsException.foo(); } 当通过mvn运行测试方法时,我收到错误: java.lang.NoSuchMethodError:org.junit.rules.ExpectedException.expectCause(Lorg / hamcrest / Matcher;)V 测试编译好。 请帮帮我,不明白如何测试exception的原因?

java.lang.IllegalStateException:前面的方法调用getMessage(“title”)缺少行为定义

我正在使用EasyMock(版本2.4)和TestNG来编写UnitTest。 我有一个以下场景,我不能改变定义类层次结构的方式。 我正在测试扩展ClassA的ClassB。 ClassB看起来像这样 public class ClassB extends ClassA { public ClassB() { super(“title”); } @Override public String getDisplayName() { return ClientMessages.getMessages(“ClassB.title”); } } ClassA代码 public abstract class ClassA { private String title; public ClassA(String title) { this.title = ClientMessages.getMessages(title); } public String getDisplayName() { return this.title; } } ClientMessages类代码 public class ClientMessages { private […]

Java中独立于文件系统的文件和路径?

我正在使用Java 8编写与文件系统交互的应用程序。 为了使测试更容易,我将一个FileSystem注入到我的方法中,可以将其换出来进行测试。 我目前在我的unit testing中使用jimfs ,在生产中使用DefaultFileSystem.getFileSystem() 。 我希望我的代码能够独立于平台并在unit testing和生产之间移植,但是,我认为标准库中的类可能会阻止这种情况。 例如, java.io.File包含: private static final FileSystem fs = DefaultFileSystem.getFileSystem(); 似乎File总是引用真实文件,这对我的unit testing来说是一个问题。 另一方面, Path只是一个接口,它应该让事情变得简单,除了它有一个创建File的方法! File toFile(); 哪些Java类与真实文件系统绑定? 我有什么选择编写Filesystem可移植代码?

如何在Play框架unit testing中加载不同的插件?

我有不同的插件实现Plugin接口。 现在我把它们硬编码在play.plugins中,如下所示: 100:test.A 200:test.B 但是在我的unit testing中,我不希望一次加载它们。 换句话说,在测试AI中只想加载插件A而在测试B中只加载B. 而不是手动更改配置文件,有没有办法以编程方式更改它? 我fakeApplication()调用fakeApplication()时,默认情况下会加载所有插件。

unit testingjava项目中所有类的可序列化

我的java项目中有数千个类。 其中一些实现了可序列化的接口。 现在这是一个问题。 有人可以进入一个类,添加既不是瞬态也不可序列化的新变量。 代码编译正常但是进程会在运行时爆炸。 为了说明这一点 class Foo implements Serializable { …. // all good } class Foo implements Serializable { // OOps, executorService is not serializable. It’s not declared as transient either private ExecutorService executorService = .. } 我正在考虑编写一个unit testing,通过所有课程并确保“真正的可串行化”。 我已经阅读了一些关于连续特定对象的讨论。 我理解这个过程,但它需要 1)创建一个对象。 2)序列化然后 3)反序列化。 是否有更有效和实用的方法。 也许用reflection。 通过所有类,如果类具有可序列化,则所有属性必须是可序列化的或具有临时关键字。 想法?

如何在Spring Boot中测试组件/ bean

要在Spring Boot应用程序中测试组件/ bean, Spring Boot文档的测试部分提供了大量信息和多种方式: @SpringBootTest @WebMvcTest , @DataJpaTest @SpringBootTest , @WebMvcTest , @DataJpaTest以及许多其他方法。 为什么提供这么多方法? 如何决定赞成的方式? 我应该考虑作为集成测试我的测试类使用Spring Boot测试注释注释,例如@SpringBootTest , @WebMvcTest , @DataJpaTest ? PS:我创建了这个问题,因为我注意到许多开发人员(甚至经验丰富的人)都没有得到使用注释而不是另一个注释的后果。