Tag: junit4

如何使用Selenium WebDriver和Java获取选定的选项

我是Selenium的新手,我被困在这里: 我想使用Selenium WebDriver 获取 下拉列表 的选定标签或值,然后在控制台上打印 。 我可以从下拉列表中选择任何值,但我无法检索所选值并将其打印出来。 Select select = new Select(driver.findElement(By.id(“MyDropDown”))).selectByVisibleText(data[11].substring(1 , data[11].length()-1)); WebElement option = select.getFirstSelectedOption(); 但我所有的努力都是徒劳的任何帮助都将受到赞赏。 提前致谢 :)

Junit是否在每次测试方法调用时重新初始化该类?

当我运行以下代码时,两个测试用例都成为现实: import static junit.framework.Assert.assertEquals; import org.junit.Test; public class MyTest{ private int count; @Before public void before(){ count=1; } @Test public void test1(){ count++; assertEquals(2, count); } @Test public void test2(){ count++; assertEquals(2, count); } } 预期的行为 test1 – 成功 test2 – 失败(正如预期的那样,计数将变为3) 实际行为 test1 – 成功 test2 – 成功 为什么junit会在每次测试方法调用时reinitializing class/variable 。 这是junit中的错误或故意提供。

Selenium可以使用JUnit截取测试失败的截图吗?

当我的测试用例失败时,特别是在我们的构建服务器上,我想拍一张屏幕的图片/截图来帮助我调试以后发生的事情。 我知道如何截取屏幕截图,但我希望在浏览器关闭之前,如果测试失败,JUnit中的方法可以调用我的takeScreenshot()方法。 不,我不想编辑我们的bazillions测试来添加try / catch。 我想也许,也许可能会被说成一个注释。 我的所有测试都有一个共同的父类,但我想不出我能做什么来解决这个问题。 想法?

在每个’@Test’之后和JUnit中的每个’@After’之前应用’@Rule’

我有一个测试套件,我在@After退出系统并在@AfterClass关闭浏览器。 我正在尝试使用@Rule为每种测试方法使用Selenium进行失败的测试截图。 我手动检查@Rule只在每个@Before之前运行,但是我想在@Test之后和@After之前设置它。 我找不到简单的解决方案。 任何帮助将不胜感激。 public class MorgatgeCalculatorTest { @Before public void before(){ System.out.println(“I am before”); } @BeforeClass public static void beforeclass(){ System.out.println(“I am beforeclass”); } @Test public void test(){ System.out.println(“I am Test”); } @Test public void test2(){ System.out.println(“I am Test2”); } @After public void after(){ System.out.println(“I am after”); } @AfterClass public static void afterclass(){ […]

如何使用Spring Autowire编写JUnit测试?

以下是我使用的文件: component.xml文件 ServiceImpl.java @org.springframework.stereotype.Service public class ServiceImpl implements MyService { @Autowired private MyDAO myDAO; public void getData() {…} } ServiceImplTest.java @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(“classpath*:conf/components.xml”) public class ServiceImplTest{ @Test public void testMyFunction() {…} } 错误: 16:22:48.753 [main] ERROR ostest.context.TestContextManager – Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2092dcdb] to prepare test instance [services.ServiceImplTest@9e1be92] org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘services.ServiceImplTest’: […]

创建名为defaultServletHandlerMapping的bean时出错

我将所有的XML Spring配置转换为Java代码配置,但是我无法运行我的所有测试(之前他们工作过),因为我有一个丑陋的例外: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘defaultServletHandlerMapping’ defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method ‘defaultServletHandlerMapping’ threw exception; nested exception is java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:602) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1113) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1008) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505) […]

在JUnit测试用例中指定执行顺序

我有一个测试用例,我添加一个实体,更新它并删除它。 因此,执行顺序在这里很重要。 我希望它是: 创建 更新 删除 奇怪的是,对于一个测试用例(15个中),JUnit按以下顺序执行: 删除 更新 创建 。 如何告诉JUnit以特定顺序执行它们? 在其他情况下,JUnit完全正常(串行执行)。 为什么JUnit在这种情况下表现得很奇怪? 以下相关代码段: private static Date date; private static int entity; static Parking p; public ParkingTests(String name) { super(name); } public void testAdd() throws Exception { //Add code here } public void testUpdate() throws Exception { //update code here } public void testDelete() […]

使用套件时,JUnit 4 @BeforeClass和@AfterClass

在下面使用此方法时,通过设置带有套件的jUnit。 在任何测试开始执行之前,每个Testclass中的所有@BeforeClass都会被执行,我们遇到了问题。 (对于每个运行@BeforeClass的n个TestClass文件,然后在执行之后,它开始执行第一个MyTest.class文件@Test) 这将导致我们分配大量资源和内存。 我的想法是它一定是错的,不应该每个@BeforeClass只在执行实际的测试类之前运行,而不是在套件启动时运行? @RunWith(Suite.class) @Suite.SuiteClasses({ MyTests.class, Mytests2.class, n1, n2, n }) public class AllTests { // empty } public class MyTests { // no extends here @BeforeClass public static void setUpOnce() throws InterruptedException { … @Test … public class MyTests2 { // no extends here @BeforeClass public static void setUpOnce() throws InterruptedException { […]

JUnit4是否开始支持测试订购? 这是有意的吗?

JUnit的新手(事实上是JUnit 4)并且遇到了执行测试的套件方式 @RunWith(Suite.class) @Suite.SuiteClasses( { CreateNewProfile.class, EditProfile.class, }) public class ProfileTestSuite { } 这是我在浏览新雇主的测试代码库时遇到的代码示例。 在执行期间,我资助 – 首先执行CreateNewProfile测试,然后执行EditProfile,这确实有意义,但之后它会在测试中引入依赖性。 我几个月来一直在遵循非依赖性测试机制(虽然我曾经使用过TestNG而不是JUnit),并期望EditProfile也可以单独执行。 那就是编辑配置文件应该负责创建配置文件然后编辑它然后断言操作。 我的问题是 – Junit 4引入了测试订购function。 是这个function还是一个复活节彩蛋,因为我一直觉得JUnit =独立测试。

如何使用Ant在类别/套件中运行所有JUnit测试?

我在类似于本答案中描述的设置中使用JUnit Categories和ClassPathSuite。 回顾一下: public interface FastTests { } @RunWith(Categories.class) @Categories.IncludeCategory(FastTests.class) @Suite.SuiteClasses(AllTests.class) public class FastTestSuite { } @RunWith(ClasspathSuite.class) public class AllTests { } … AllTests使用ClasspathSuite库。 属于FastTests类别的测试类看起来像这样: @Category(FastTests.class) public class StringUtilsTest { // … } 当我在IDE中运行“FastTestSuite”时,所有使用FastTests注释的测试都会执行,非常流畅: 现在, 我想用Ant做同样的事情 。 (令我惊讶的是,我无法在SO上轻松找到相关说明。)换句话说,我需要一个使用FastTests注释运行所有测试的Ant目标。 我使用或尝试了一些简单的方法…… ……但到目前为止没有运气。 编辑 :除了IDE,它在命令行上与JUnitCore一起正常工作: $ java -classpath “classes:WebContent/WEB-INF/lib/*” org.junit.runner.JUnitCore fi.foobar.FastTestSuite …………. Time: 0.189 OK (13 tests)