标签: testng

LazyInitializationException,同时使用TestNG对Spring中使用的Hibernate实体类进行单元测试

在我的Spring配置中,我已经要求会话在我的视图中保持打开状态:

  <bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
    <property name="sessionFactory" ref="sessionFactory"/>
    <property name="flushMode" value="0" />
  </bean> 
Run Code Online (Sandbox Code Playgroud)

但是,这个bean似乎不认为我的TestNG单元测试是一种观点.;-)那没关系,但有没有类似的bean用于单元测试,以便在单元测试时避免可怕的LazyInitializationException?到目前为止,我的一半单元测试因此而死亡.

我的单元测试通常如下所示:

@ContextConfiguration({"/applicationContext.xml", "/applicationContext-test.xml"})
public class EntityUnitTest extends AbstractTransactionalTestNGSpringContextTests {

  @BeforeClass
  protected void setUp() throws Exception {
    mockEntity = myEntityService.read(1);
  }

  /* tests */

  @Test
  public void LazyOneToManySet() {
    Set<SomeEntity> entities = mockEntity.getSomeEntitySet();
    Assert.assertTrue(entities.size() > 0); // This generates a LazyInitializationException
  }



}
Run Code Online (Sandbox Code Playgroud)

我已经尝试将setUp()更改为:

private SessionFactory sessionFactory = null;

@BeforeClass
protected void setUp() throws Exception {
  sessionFactory = (SessionFactory) this.applicationContext.getBean("sessionFactory");
  Session s = sessionFactory.openSession();
  TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(s));

  mockEntity …
Run Code Online (Sandbox Code Playgroud)

java testng spring unit-testing hibernate

1
推荐指数
1
解决办法
2234
查看次数

GWT + Java:全球,单身和头痛

所以这是我的项目:

我正在构建一个中央界面/仪表板,以显示多个产品版本的多种测试类型的测试数据.我们在我们的大型产品上使用TestNG,虽然没有编写足够的测试,但这是另一个主题的讨论.这是目录结构的样子:

文件系统/的ProductVersion + testType/uniqueDateAndBuildID/TestNG的-results.xml

results.xml文件包含带有子测试标记的标记,这些标记对应于文件系统目录,然后是包含实际测试用例结果的xml文件(传递,失败等)XML解析和文件系统遍历都很好,很好/可靠.

控制流程:客户端访问主页面 - >服务器打开属性文件 - >服务器检查Web服务器属性(Websphere或Tomcat,如果我在本地工作) - >服务器根据它设置一堆常量.常量包括:根文件系统目录,文件系统分隔符(转换),"类似类型(基本上在不同平台上的相同测试)",以及要附加到的基本URL. - >然后服务器再读取属性文件并完成所有XML处理.使用ObjectOutputStream将结果缓存在内存中以及文件系统中. - >将大量结果发送回客户端进行UI处理/显示.

这是我遇到问题的地方:我无法访问客户端上的全局变量(在Globals类中包含/设置...我知道错误: - /),即使它们位于共享文件夹中.如果你想知道为什么我不能再次加载属性,那是因为客户端是GWT-ified Javascript,它不包含File().所以我的下一个想法是,做了一点上层Java阅读可能是使用一个Globals单例对象并将其传回去.但是看起来这也是坏的,如果不是不可能的话. 这里的建议会很棒.

这一切都非常紧密,我以前的Java教育还没有真正进入过.由于这只是开发人员检查的内部门户,因此实际测试我的代码似乎没有多大意义.只要它正确显示,正确记录,并优雅地处理错误,对吗?总而言之,这是<15级,所以我猜这不是什么大不了的事.我应该重构清理它并使其成为"更好的Java",评论所有内容以清楚地描述控制流程,或者不要过于担心它因为它很小?我知道将来在设计之前要多考虑一些事情,但我真的不知道自开始以来我接触过的大量Java原则.

在做了一些思考后编辑,想出了一个可能的解决方法.那么,我传回了一些包含全局"标题"对象的其他自定义列表实现,而不是返回结果列表?我可以保留国家.

java gwt testng singleton rpc

1
推荐指数
1
解决办法
2023
查看次数

如何在@BeforeSuite中使用testNG @Parameters来读取资源文件

我使用testNGSelenium webdriver2.0.

在我的testNG.xml

<suite data-provider-thread-count="2" name="selenium FrontEnd Test" parallel="false" skipfailedinvocationCounts="false" thread-count="2">
  <parameter name="config_file" value="src/test/resources/config.properties/"/>
  <test annotations="JDK" junit="false" name="CarInsurance Sanity Test" skipfailedinvocationCounts="false" verbose="2">
    <parameter name="config-file" value="src/test/resources/config.properties/"/>
    <groups>
      <run>
        <include name="abstract"/>
        <include name="Sanity"/>
      </run>
    </groups>
    <classes>
    </classes>
  </test> 
</suite>
Run Code Online (Sandbox Code Playgroud)

在java文件中

@BeforeSuite(groups = { "abstract" } )
@Parameters(value = { "config-file" })
public void initFramework(String configfile) throws Exception 
{
    Reporter.log("Invoked init Method \n",true);

    Properties p = new Properties();
    FileInputStream  conf = new FileInputStream(configfile);
    p.load(conf);

    siteurl = p.getProperty("BASEURL"); …
Run Code Online (Sandbox Code Playgroud)

java testng

1
推荐指数
1
解决办法
3万
查看次数

断言错误(TestNG)使用java捕获问题

请找到代码

Test(dataProvider = "User_login")
public void StatusForm_Verification(String uname, String pwd)
    throws InterruptedException {
    try {
NavigateToLogin();
Dashboard RD = LoginAs_user(uname, pwd);
Thread.sleep(2000);

    if (Integer.parseInt(ReviewedStatuscount) >= 1) {

        Assert.assertEquals("true",
                revui.Btn_SaveReview.getAttribute("disabled"));

        Assert.assertEquals("true",
                revui.Btn_submitReview.getAttribute("disabled"));

        Assert.assertEquals("true",
                revui.Btn_Needmoreinfo.getAttribute("disabled"));

        status = TestLinkAPIResults.PASSED;     

    } else {
        throw new SkipException(
                "Test Skipping - Reviewed count is Zero");
    }
    }catch((AssertionError ex) {
        testlink_result = TestLinkAPIResults.TEST_FAILED;
        msg = ex.getMessage();
}
}
Run Code Online (Sandbox Code Playgroud)

执行上面的方法,如果在TestNG报告中抛出任何断言错误,它应该是一个失败状态 - 这是正确的

但是如果我在上面的方法中使用Try catch块(捕获AssertionError),那么TestNG报告总是处于PASS状态,为什么呢?怎么样?

java testng selenium-webdriver testlink

1
推荐指数
1
解决办法
8310
查看次数

在测试时,Spring注入在自定义hibernate验证器中不起作用

测试配置类

@Configuration
@Import( value = GlobalCoreContext.class)
public class TestConfiguration {

    @Bean
    public Validator createValidatorFactory() {
        return Validation.buildDefaultValidatorFactory().getValidator();
    }

}
Run Code Online (Sandbox Code Playgroud)

ValidatorClass

public final class LoginAttemptsExceededValidatorLoginId implements ConstraintValidator<LoginAttemptsExceeded, String> {

    @Resource
    private LoginDao loginDao;

    private LoginAttemptsExceeded loginAttemptsExceeded;

    @Override
    public void initialize(final LoginAttemptsExceeded loginAttemptsExceeded) {
        this.loginAttemptsExceeded = loginAttemptsExceeded;
    }

    /**
     * Validation fails if the login attempts have exceeded the preset number.
     * @param loginId
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(final String loginId, final ConstraintValidatorContext constraintValidatorContext) {
        if(StringUtility.IsEmpty(loginId)) return …
Run Code Online (Sandbox Code Playgroud)

testng spring spring-mvc hibernate-validator

1
推荐指数
1
解决办法
2801
查看次数

Selenium Automation使用TESTNG重试失败的测试

有人可以建议我如何使用testng为批量测试执行准备测试脚本...我有大约1000个测试用例,具有不同的测试数据迭代...

此外,我想在完成测试套件的执行后执行失败的测试用例.假设有1000个测试用例,200个失败,那么我想再次运行200个

testng selenium selenium-webdriver

1
推荐指数
1
解决办法
1493
查看次数

如何使用List <WebElement> webdriver

我正在为一些网站创建一个自动测试,我正在使用WebDriver,TestNG和用Java编写的代码.在页面上显示类别的注册,在括号中是拍卖的数量,我需要将此数字作为变量.

我用这个代码

By bycss =By.cssSelector(".list.list-categories>li:first-child");
WebElement number1_1_vse = driver.findElement(bycss );
String text_vse1_1 = number1_1_vse.getText();
Run Code Online (Sandbox Code Playgroud)

但我得到的只是第一个数字,我需要得到所有.有人告诉我,我应该使用List.但是当我使用它时,我只能得到它

[[[[[[[FirefoxDriver: firefox on WINDOWS (7e6e0d0f-5cbb-4e48-992f-26d743a321a5)] -> css selector: .list.list-categories>li:first-child]] -> xpath: ..]] -> xpath: .//*], [[[[[[FirefoxDriver: firefox on WINDOWS (7e6e0d0f-5cbb-4e48-992f-.....
Run Code Online (Sandbox Code Playgroud)

码:

By bycss2 =By.cssSelector(".list.list-categories>li:first-child");
WebElement number1_1_vse2 = driver.findElement(bycss2 );
WebElement parent1 = number1_1_vse2.findElement(By.xpath(".."));
List<WebElement> childs1 = parent1.findElements(By.xpath(".//*"));
System.out.println(childs1);
Run Code Online (Sandbox Code Playgroud)

链接到该网站

截图 - >带有数字的图片

有人可以告诉我吗?

java testng parents webdriver list

1
推荐指数
1
解决办法
8万
查看次数

Testng:如果存在异常,则跳过/失败其余测试

我在Java上使用Selenium和TestNG编写测试.如果发现异常,如何跳过/失败测试的其余部分?

例如,如果我在测试中有3个步骤

@Test(priority = 1)
    public void login() {

    }

    @Test(priority = 2)
    public void checkLoginResult() {

    }

    @Test(priority = 3)
    public void submitImage() {

    }
Run Code Online (Sandbox Code Playgroud)

如果在第一次测试'登录'上捕获了Selenium异常(TimeOutException ie),我怎么能跳过剩下的测试?

是否有一些注释或任何可用于此目的的方法?

谢谢

java testng selenium

1
推荐指数
1
解决办法
2914
查看次数

Maven无法进行特定测试

我正在通过maven运行测试用例.使用以下命令:

获得成功:mvn test

失败:mvn -Dtest=LoginTest test

类名是LoginTest

堆栈跟踪 :

[错误]无法在项目Web上执行目标org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test(default-test):没有执行任何测试!(设置-DfailIfNoTests = false以忽略此错误.) - > [帮助1] org.apache.maven.lifecycle.LifecycleExecutionException:无法执行目标org.apache.maven.plugins:maven-surefire-plugin:2.12.4:项目Web上的测试(默认测试):没有执行任何测试!(设置-DfailIfNoTests = false以忽略此错误.)org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:212)org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor. java:153)org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)atg.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)at org.apache .maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)位于org.apache.maven.lifecycle的org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51) .internal.LifecycleStarter.execute(LifecycleStarter.java:128)org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)atg.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)at org.位于org.apache.mav的org.apache.maven.cli.MavenCli.execute(MavenCli.java:862)的apache.maven.DefaultMaven.execute(DefaultMaven.java:106)位于org.apache.maven.cli.MavenCli.main(MavenCli.java:197)的en.cli.MavenCli.doMain(MavenCli.java:286)位于sun.reflect的sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method). NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)位于org.codehaus.plexus.classworlds的java.lang.reflect.Method.invoke(Method.java:497)的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) .launcher.Launcher.launchEnhanced(Launcher.java:289)org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher) .java:415)at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)引起:org.apache.maven.plugin.MojoFailureException:没有执行任何测试!(设置-DfailIfNoTests = false忽略此错误.)org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:579)org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java: 134)在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)... 20更多

java testng maven selenium-webdriver

1
推荐指数
1
解决办法
1862
查看次数

TestNG.xml-错误“元素类型“ classes”的内容必须与“(class *,parameter *)”匹配。”

下面的xml文件是我的TestNG.xml。当我添加方法标签时,这会显示错误:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">

    <listeners>
        <listener class-name="Util.Listnr" />
    </listeners>
    <test thread-count="5" name="Test">
        <classes>
            <class name="AWD_Tests.NewTest" />
            <methods>
                <include name="AWDRetrieval" />
                <include name="function" />

                <exclude name="AWDCreation" />
            </methods>

        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->
Run Code Online (Sandbox Code Playgroud)

xml testng selenium

1
推荐指数
1
解决办法
1736
查看次数