小编Jav*_*igs的帖子

在servlet中自动装配

我想在servlet中使用spring autowiring,所以这是我的代码:

@Configurable
public class ImageServlet extends HttpServlet {

   @Autowired
   private SystemPropertyDao systemPropertyDao;

   @Override
   public void init() throws ServletException {


   String imagePath = systemPropertyDao.findByID(StaticParam.CONTENT_FOLDER);

}
Run Code Online (Sandbox Code Playgroud)

SystemPropertyDao注释时@Repository

和我的applicationContext.xml:

<context:component-scan base-package="com.basepackage" />
<mvc:annotation-driven />
<context:annotation-config />
<context:spring-configured/>
Run Code Online (Sandbox Code Playgroud)

web.xml:

  <servlet>
    <servlet-name>imageServlet</servlet-name>
    <servlet-class>com.xeno.basepackage.ImageServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>imageServlet</servlet-name>
    <url-pattern>/myimages/*</url-pattern>
  </servlet-mapping>
Run Code Online (Sandbox Code Playgroud)

有时自动装配工作,有时它不工作(对spring bean systemPropertyDao的引用为null),任何人都可以告诉我,如果我错过了什么?

spring servlets dependency-injection autowired java-ee

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

以编程方式替换PowerMock的@PrepareForTest?

我正在使用PowerMock在junit测试中模拟静态方法,通常如下所示:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Foo.class,Bar.class})
public class SomeUnitTest {

  @Before
  public void setUpTest() {
    setUpFoo();
    setUpBar();
  }

  private void setUpFoo() {
    mockStatic(Foo.class);
    when(Foo.someStaticMethod()).thenReturn(1);
  }

  private void setUpBar() {
    mockStatic(Bar.class);
    when(Bar.someStaticMethod()).thenReturn(2);
  }

  @Test
  public void someTestCase() {
    ...
  }

}
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我发现指定@PrepareForTest注释阻止我使我的测试API灵活.

我想做的是如下:

public class MockLibraryOne {

  public static void setUpLibraryOne() {
    setUpFoo();
    setUpBar();
  }

  private static void setUpFoo() {
    mockStatic(Foo.class);
    when(Foo.someStaticMethod()).thenReturn(1);
  }

  private static void setUpBar() {
    mockStatic(Bar.class);
    when(Bar.someStaticMethod()).thenReturn(2);
  }

}

@RunWith(PowerMockRunner.class)
public class SomeUnitTest {

  @Before
  public void setUpTest() { …
Run Code Online (Sandbox Code Playgroud)

junit unit-testing powermock

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

在球衣项目中包含jersey-bom导入范围依赖的目的是什么?

使用工件生成基于的项目时the jersey-quickstart-grizzly2

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
-DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example \
-DarchetypeVersion=2.7
Run Code Online (Sandbox Code Playgroud)

pom生成了一个可以删除jersey-bom依赖项:

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>${jersey.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
Run Code Online (Sandbox Code Playgroud)

这种依赖:

<dependency>
      <groupId>org.glassfish.jersey.containers</groupId>
      <artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这是maven依赖图的样子:

在此输入图像描述

jersey-bom在项目中包含依赖项的目的是什么?

java jersey java-ee maven jersey-2.0

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

maven提供了范围

<dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我在项目的pom.xml中使用了依赖项导入.我的问题是我宣布2.5为版本.但是,编写较低版本是否重要?例如,我的意思是,如果我的项目使用3.0版本,我写的将提供2.5?(我的意思是让我们接受2.5很好,我的项目运作良好,如果我不改变其他任何东西,只需将2.5更改为2.0会导致错误吗?)

dependencies maven

15
推荐指数
2
解决办法
2万
查看次数

如何通过log4j控制每日滚动和最大文件大小的日志文件?

我想创建可以在第二天开始时滚动的日志文件,或者如果达到指定的文件大小,日志文件必须包含在日期文件夹中.文件夹的格式是YYYYMMDD(/20111103/mylogfile.log)

是否可以通过Log4j执行此操作而不实现自定义类?

现在我正在使用log4j和log4j-extra,我设置log4j API中定义的FileNamePattern属性来每天滚动我的文件并将最大文件大小设置为50 MB.

log4j.xml是:

<appender name="MYAPPENDER" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="encoding" value="UTF-8" />
    <param name="append" value="true" />
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern" value="${catalina.home}/logs/MY-APP/%d{yyyyMMdd}/MY-APP_%d{yyyyMMddHHmmss}.log" />
    </rollingPolicy>
    <triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
        <param name="maxFileSize" value="50000000" />
    </triggeringPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="[%d{dd/MM/yyyy HH\:mm\:ss}] %-5p [%c.%M(),%4L] - %m%n" />
    </layout>
</appender>
Run Code Online (Sandbox Code Playgroud)

上述设置的结果是日志文件未在下一天开始滚动,但如果文件大小达到50 MB,则将滚动日志文件.

请帮忙告诉我.m(_ _)m

log4j rollingfileappender

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

Gradle | 不会排除Spring启动依赖项

我正在尝试让log4j在我正在进行的项目中工作.我在build.gradle中添加了相关的log4j依赖项,并排除了Spring启动启动日志记录,以便它可以工作.

当我使用Maven作为构建工具时,这工作正常,但是一旦我切换到Gradle,它根本不工作(除了从Spring启动器启动记录).以下是build.gradle中的依赖项

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web'){
        exclude module: 'org.springframework.boot:spring-boot-starter-logging'
    }
    compile('org.springframework.boot:spring-boot-starter-log4j')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.postgresql:postgresql:9.3-1101-jdbc41')
    compile('org.scala-lang:scala-library:2.10.4')
    testCompile('org.springframework.boot:spring-boot-starter-test') {
        exclude module: 'commons-logging'
    }
    providedCompile('org.springframework.boot:spring-boot-starter-tomcat')
}
Run Code Online (Sandbox Code Playgroud)

但是你可以在依赖树中清楚地看到spring-boot-starter-logging它仍然存在.我猜这就是为什么日志记录不起作用的问题.

这是依赖树:

+--- org.springframework.boot:spring-boot-starter-data-jpa: -> 1.2.1.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:1.2.1.RELEASE
|    |    +--- org.springframework.boot:spring-boot:1.2.1.RELEASE
|    |    |    +--- org.springframework:spring-core:4.1.4.RELEASE
|    |    |    \--- org.springframework:spring-context:4.1.4.RELEASE
|    |    |         +--- org.springframework:spring-aop:4.1.4.RELEASE
|    |    |         |    +--- aopalliance:aopalliance:1.0
|    |    |         |    +--- org.springframework:spring-beans:4.1.4.RELEASE
|    |    |         |    |    \--- org.springframework:spring-core:4.1.4.RELEASE
|    |    |         |    \--- org.springframework:spring-core:4.1.4.RELEASE
|    | …
Run Code Online (Sandbox Code Playgroud)

spring log4j gradle build.gradle spring-boot

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

在Webdriver中使用个人SSL证书(Selenium 2.0)

我正在测试一个需要个人SSL证书的网站才能做某些事情,比如登录.

我有一个使用代理设置的Webdriver(Selenium 2.0)测试:

    Proxy localhostProxy = new Proxy();
    localhostProxy.setProxyType(Proxy.ProxyType.MANUAL);
    localhostProxy.setHttpProxy("www-proxyname:port");

    FirefoxProfile profile = new FirefoxProfile();
    profile.setProxyPreferences(localhostProxy);
    driver = new FirefoxDriver(profile);
Run Code Online (Sandbox Code Playgroud)

这将很好地访问主页.然后,测试点击登录按钮,输入正确的凭据并单击提交.此时浏览器进入加载状态,我假设是因为我的身边缺少SSL证书,因此无法连接到登录服务.

我搜索了不同的代理解决方案,发现了这个:

    profile.setAcceptUntrustedCertificates(true);
    profile.setAssumeUntrustedCertificateIssuer(true);
Run Code Online (Sandbox Code Playgroud)

所以我把它添加到我的代码中,但它似乎没有做我想要的.我想我正在寻找一种方法告诉WebDriver我的ssl证书在x目录中,请在访问此站点时使用它.有谁知道如何做到这一点?

我的测试代码是:

@Test
public void userSignsInAndVerifiesDrawerViews(){
            driver.get("www.url.com");
            waitFor(5000);
    driver.findElement(By.xpath("//a[contains(text(), 'Sign in')]")).click();
    waitFor(3000);
    String username = "seleniumtest";
    String password = "seleniumtest1";
    driver.findElement(By.id("username")).sendKeys(username);
    driver.findElement(By.id("password")).sendKeys(password);
    driver.findElement(By.xpath("//signin")).click();
    waitFor(30000);
    String signInLinkText = driver.findElement(By.xpath("//xpath")).getText();
    assertEquals(signInLinkText, username);
}
Run Code Online (Sandbox Code Playgroud)

谢谢,贝西

java selenium webdriver

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

在gradle中配置cpp源

我在Qt之上设置了一个项目(因此源代码是用C++编写的),我想尝试使用Gradle进行自动构建.我花了一些时间进入配置多项目构建的细节(有一个可执行文件以及两个库),现在我想告诉cpp-execpp-lib插件我的源代码树是如何构成的.

我已经设置了一个应该打印所有源集的任务(至少应该有默认值?),它看起来像这样:

task projectinfo {
description = "Informations about the current project"
group = INFORMATIONS_GROUP

doFirst {
    task -> print ("""${task.project.sourceSets.all}""")
}
Run Code Online (Sandbox Code Playgroud)

如果我运行此任务,Gradle会告诉我该项目没有属性"sourceSets".该插件的文档告诉我可以自定义源位置,但不能如何自定义源位置.

所以我的问题是:如何告诉Gradle cpp插件使用哪些源文件.如果有关于该cpp插件的任何文档,除了它的API文档和Gradle用户指南之外也会有所帮助.

c++ build-automation gradle

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

Webdriver findElements由xpath提供

1)我正在做一个教程来展示findElements by xpath是如何工作的.我想知道为什么它返回<div>带有属性的元素之后的所有文本id=container.

xpath的代码: By.xpath("//div[@id='container']

2)我应该如何修改代码,以便它只返回跟随父笔记的第一个或前几个节点,例如第一个节点,如"Home",首先是几个节点,如Home,Manual Testing和Automation Testing.

感谢您的建议和帮助!

以下是本教程的代码片段:

import java.util.List;

import org.junit.Test;
import org.junit.Before;
import org.junit.After;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WD_findElements 
{
    @Test
    public void test_byxpath(){

        WebDriver driver = new FirefoxDriver();

        try{
            driver.get("http://www.hexbytes.com");

        List<WebElement> elements = driver.findElements(By.xpath("//div[@id='container']"));    
            System.out.println("Test7 number of elements: " + elements.size());

            for(WebElement ele : elements){
                //ele.sendKeys("hexbyes");
                System.out.println(ele.getText());
                //System.out.println(ele.getAttribute("id"));
                //System.out.println(ele.getTagName());
            } 
        }
        finally {
            driver.close();
        }

    }//end of test_byxpath

 public void xpathDemo2() {
           WebDriver driver = …
Run Code Online (Sandbox Code Playgroud)

java selenium xpath webdriver selenium-webdriver

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

从 Weblogic 迁移到 Apache Tomcat

我正在将基于 Weblogic 10c 构建的项目(使用 servlet / jsp / jdbc / jndi)迁移到 Apache Tomcat 7.0.22。我已经成功配置了 ldap 身份验证服务器,并替换了 weblogic 使用的 xxx-jdbc.xml。现在我的问题是我正在尝试迁移 web Content/WEB-INF 目录中找到的 weblogic.xml 文件。xml文件的内容如下:

<?xml version = '1.0' encoding = 'UTF-8'?>
<weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd"
              xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
  <security-role-assignment>
    <role-name>REGISTERED_USER</role-name>
    <principal-name>GROUP_NAME_FROM_LDAP</principal-name>
  </security-role-assignment>
  <session-descriptor>
    <debug-enabled>false</debug-enabled>
    <tracking-enabled>true</tracking-enabled>
    <cookie-name>nameOfCookie</cookie-name>
    <cookie-max-age-secs>-1</cookie-max-age-secs>
    <url-rewriting-enabled>false</url-rewriting-enabled>
    <encode-session-id-in-query-params>false</encode-session-id-in-query-params>
    <sharing-enabled>false</sharing-enabled>
  </session-descriptor>
  <context-root>my_app_context_root</context-root>
  <servlet-descriptor>
    <servlet-name>FileDownload</servlet-name>
  </servlet-descriptor>
</weblogic-web-app>
Run Code Online (Sandbox Code Playgroud)

从上到下,我有安全角色分配,它将 ldap 组中的用户映射为拥有 REGISTERED_USER。我认为标签会话描述符是自我解释的。然后是我的应用程序上下文根上下文根。然后是一些 servlet 定义,用于将 servlet 注册到 Weblogic(这也在 web.xml 中定义,我认为这不需要更多处理)。

那么在我的应用程序中迁移此 weblogic.xml 文件的最佳方法是什么?

java migration tomcat servlets weblogic

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

Geb &amp; Spock - If/Then/Else 逻辑 - 如何检查记录并在存在时执行一件事,但如果不存在则继续执行

我正在使用 Geb/Spock 测试在我的网站上创建和删除记录。但是,如果记录已经存在,我将无法创建该记录,因此我检查该记录是否存在,如果在测试开始时存在,则将其删除。当记录不存在时就会出现问题,导致测试失败。有没有办法合并一些 if/then/else 逻辑,以便如果在开始时没有找到记录,测试将继续,如果找到,则将其删除?

编辑示例代码:

/**
 * Integration test for Create Record
**/
class CreateAndRemoveRecordSpec extends GebSpec {

def 'check to make sure record 999 does not exist'() {

    given: 'user is at Account Page'
    to MyAccountPage

    when: 'the user clicks the sign in link'
    waitFor { header.signInLink.click() }

    and: 'user logs on with credentials'
    at LoginPage
    loginWith(TEST_USER)

    then: 'user is at landing page.'
    at MyAccountPage

    and: 'list of saved records is displayed'
    myList.displayed

    /* I would like some …
Run Code Online (Sandbox Code Playgroud)

testing logic if-statement spock geb

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