Tag: 测试

junit assertEquals忽略大小写

我只是从c# – > java移动。 我需要使用junit编写一些测试。 在我的测试中,我需要比较两个字符串,看看它们是否匹配。 所以我们也有Assert.assertEquals,但这是区分大小写的。 我怎样才能使它不区分大小写? 我需要的是: “blabla”.equals(“BlabLA”) 返回真实。 所以在C#中,我们曾经有: public static void AreEqual ( string expected, string actual, bool ignoreCase, string message ) 我很快就通过Junit文档,但我似乎无法找到这样的东西。

如何测试该方法是否添加了redirectAttributes?(由MockMvc提供)

我写过这个方法: @RequestMapping(value=”/someURL”, method=GET) public String myMethod(RedirectAttributes redirectAttributes) { redirectAttributes.addAttribute(“rd”, “rdValue”); redirectAttributes.addFlashAttribute(“fa”, faValue); return “redirect:/someOtherURL”; } 测试代码: @Test public void myMethod() throws Exception{ MockHttpServletRequestBuilder request = MockMvcRequestBuilders .get(“/someURL”); ResultActions result = mockMvc.perform(request); } 如果我调用测试执行,我看到: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String com.epam.hhsystem.web.controllers.VacancyMenuController.myMethod(org.springframework.web.servlet.mvc.support.RedirectAttributes)]; nested exception is java.lang.IllegalStateException: Argument [RedirectAttributes] […]

如何通过除“class”和“name”之外的属性直接查找WebElements(例如“title”)

我是Java和Selenium的新手,所以如果我的问题听起来有些重要,我会提前道歉。 我用: driverChrome.findElements(By.className(“blabla”)); 查找具有“blabla”作为其className的元素,例如: … 现在,如果我想通过其他属性查找所有元素,该怎么办? 就像是: driverChrome.findElements(By.titleValue(“the title”)); 这是我目前用于执行此任务的代码: List spans = driverChrome.findElements(By.tagName(“span”)); for (WebElement we : spans) { if (we.getAttribute(“title”) != null) { if (we.getAttribute(“title”).equals(“the title”)) { … break; } } } 但它并不快速且易于使用。

无法使用@ContextConfiguration加载ApplicationContext(classes = {…})

我正在尝试使用Java和注释配置运行一些弹簧测试。 我在测试包中有两个配置类: @Configuration @ComponentScan(basePackages = “com.mypackages”) public class TestConfig { @Bean public MyService getMyService() { return new MyServiceImpl(); } } @Configuration @EnableTransactionManagement public class JpaTestConfig { @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){ LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean(); lcemfb.setDataSource(this.dataSource()); lcemfb.setPackagesToScan(new String[] {“com.mypackages”}); lcemfb.setPersistenceUnitName(“MyTestPU”); HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter(); lcemfb.setJpaVendorAdapter(va); Properties ps = new Properties(); ps.put(“hibernate.dialect”, “org.hibernate.dialect.HSQLDialect”); ps.put(“hibernate.hbm2ddl.auto”, “create”); […]

logback.xml的perf4j设置

大家好,我想知道在使用perf4j时logback.xml的确切配置设置。 我在logback.xml中创建此配置 firstBlock,secondBlock <!– The NotificationThresholds param configures the sending of JMX notifications when statistic values exceed specified thresholds. This config states that the firstBlock max value should be between 0 and 800ms, and the secondBlock max value should be less than 1500 ms. You can also set thresholds on the Min, Mean, StdDev, Count […]

Spring-Test-MVC / MockServletContext – 测试中的内容为空,但是在Tomcat上工作

我们正在尝试为Spring-MVC Web应用程序设置Spring-Test-MVC。 我们开始使用freemarker,一切都很好。 我们决定反对它,现在正试图用JSP进行设置。 当测试应用程序部署在Tomcat上时,它正在运行。 当我们运行简单测试时: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(loader = WebContextLoader.class, locations = { “file:src/main/webapp/WEB-INF/servlet-context.xml” }) public class SkelletonTest { @Inject private MockMvc mockMvc; @Test public void homeTest() throws Exception { mockMvc.perform(get(“/”)).andExpect(status().isOk()) .andExpect(content().type(“text/html;charset=ISO-8859-1”)) .andExpect(content().string(containsString(“Hello World!”))); } 它说: content type not set或如果删除,内容将为空。 然而,控制器将被调用,因此映射必须起作用。 所以这强烈暗示我们的测试没有呈现视图,但我不知道我可能缺少什么设置。 这是我们的servlet-context.xml: WebContextLoader: public class WebContextLoader extends GenericWebContextLoader { public WebContextLoader() { super(“src/main/webapp”, false); […]

关闭TestNG中的测试输出

我目前正在使用此XML文件启动我的测试: 我正在寻找一种方法来切断TestNG默认情况下生成的测试输出文件夹。 来自http://reportng.uncommons.org/我可以看到 您可能还希望通过将useDefaultListeners属性设置为“false”来禁用默认的TestNG报告者。 这似乎符合我的需要,不是因为他们的XML结构看起来与我的不同。 有人知道如何使用TestNG关闭测试输出文件吗? 谢谢

使用Thread.sleep进行测试

使用Thread.sleep()加速测试的推荐方法是什么? 当连接断开或发生超时错误时,我正在测试具有重试function的网络库。但是,库在重试之间使用Thread.sleep() (因此在服务器重启时它不会连接数千次) 。 该调用显着减慢了unit testing,我想知道选项是什么来覆盖它。 注意,我愿意实际更改代码,或者使用模拟框架来模拟Thread.sleep(),但是我想先听听您的意见/建议。

在maven中运行单个测试 – >没有执行任何测试!

当我使用此命令在Maven中运行单个测试时: mvn test -Dtest=InitiateTest 我得到以下结果: No tests were executed! 它在几分钟前工作,但现在由于某种原因停止工作。 我在运行测试之前尝试运行mvn clean几次,但没有用。 测试看起来像这样: import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import org.junit.After; import org.junit.Before; import org.junit.Test; public class InitiateTest { public static FirefoxDriver driver; @Before public void setUp() throws Exception { driver = new FirefoxDriver(); } @Test public void initiateTest() throws Exception { driver.get(“http://localhost:8080/login.jsp”); … } […]

关于with(任何(Class.class)))与JMock

使用JMock: context.checking(new Expectations() {{ // Other oneOf() will() statements … oneOf(shopAccount).enter(100, with(any(String.class))); will(returnValue(true)); // Other oneOf() will() statements … }}); 执行期间将引发以下exception: java.lang.IllegalArgumentException:并非所有参数都被赋予显式匹配器:要么所有参数都必须由匹配器指定,要么所有参数都必须由值指定,您不能混合使用匹配器和值。 为什么我不能这样做? 使用with(any(Klass.class)) ?