小编dty*_*dty的帖子

在测试类之前和之后使ApplicationContext变脏

MyTest在我的Spring集成测试中有一个特定的类(比方说),它@PrepareForTest在Spring组件上使用PowerMock 注释:@PrepareForTest(MyComponent.class).这意味着PowerMock将通过一些修改加载此类.问题是,我@ContextConfiguration是在扩展的超类上定义的MyTest,并且ApplicationContext是在不同的测试类之间缓存的.现在,如果MyTest先运行,它将具有正确的PowerMock版本MyComponent,但如果没有 - 测试将失败,因为上下文将被加载以进行另一次测试(没有@PrepareForTest).

所以我想要做的是重新加载我的上下文MyTest.我可以通过这样做

@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
Run Code Online (Sandbox Code Playgroud)

但是,如果我还想在完成此测试后重新加载上下文怎么办?所以我会在MyComponent没有PowerMock修改的情况下再次清理.有没有办法做到既BEFORE_CLASSAFTER_CLASS

现在我用以下黑客做到了:

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
Run Code Online (Sandbox Code Playgroud)

在MyTest然后

/**
 * Stub test to reload ApplicationContext before execution of real test methods of this class.
 */
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
@Test
public void aa() {
}

/**
 * Stub test to reload ApplicationContext after execution of real test methods of this class.
 */
@DirtiesContext(methodMode …
Run Code Online (Sandbox Code Playgroud)

java spring spring-test

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

使用JaCoCo创建代码覆盖率报告时出错

我正在尝试使用JaCoCo为代码覆盖添加报告生成.该项目正在使用Maven,所以我有jacoco maven插件配置如下:

         <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.maven.plugin.version}</version>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
                    </configuration>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
                        <outputDirectory>${basedir}/target/jacoco</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

还有像这样的surefire插件:

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.plugin.version}</version>
            <configuration>
                <argLine>${argLine} -XX:-UseSplitVerifier</argLine>
                <printSummary>true</printSummary>
                <excludes>
                    <exclude>**/selenium/**/*.java</exclude>
                <excludes>
            </configuration>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

但是在跑步结束时

mvn清洁包

我收到这样的错误:

[错误]无法执行目标org.jacoco:jacoco-maven-plugin:0.7.5.201505241946:项目LMS上的报告(报告):JaCoCo测试报告生成中发生错误.创建报告时出错:无效的文字/长度代码 - > [帮助1] [错误] [错误]要查看错误的完整堆栈跟踪,请使用-e开关重新运行Maven.[ERROR]使用-X开关重新运行Maven以启用完整的调试日志记录.[错误] [错误]有关错误和可能的解决方案的更多信息,请阅读以下文章:[错误] [帮助1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

问题是,在另一个项目中,同样的事情就像一个魅力.我在网上找不到解决方案.有人有什么想法吗?

使用-e开关我收到以下错误:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.jacoco:jacoco-maven-plugin:0.7.5.201505241946:report (report) on project LMS: An error has occurred in JaCoCo Test report generation.
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
        at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) …
Run Code Online (Sandbox Code Playgroud)

java surefire maven jacoco

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

JDK 1.8.0_92 Nashorn JS引擎indexOf行为

我在Java8中使用“ nashorn” JavaScript引擎在运行时评估某些表达式。我有一个util类与此方法:

    public static String evaluateJavaScriptExpression(String expression) throws ScriptException {
    if (expression == null) {
        return null;
    }
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine javaScriptEngine = scriptEngineManager.getEngineByName(JAVASCRIPT_ENGINE);
    return String.valueOf(javaScriptEngine.eval(expression));
}
Run Code Online (Sandbox Code Playgroud)

为此,我创建了一些单元测试。其中之一是这样的:

    String expression = "var arr = [1, 3, 2, 5, 4]; arr.indexOf(0);";
    assertEquals("-1", ExpressionEvaluatorUtil.evaluateJavaScriptExpression(expression));
Run Code Online (Sandbox Code Playgroud)

当我使用Java版本“ 1.8.0_91”时,它对我来说工作正常。但是使用Java版本“ 1.8.0_92”的人报告测试失败。我将我的版本切换到build 92,但对我来说也失败了。实际结果是“ -1.0”。另外,我在Chrome控制台中尝试了相同的js代码,并且返回的值与内部版本91一样为“ -1”。

有谁知道为什么两个JDK版本之间的结果会有这种差异?这是一个错误还是故意更改了?

javascript arrays java-8 nashorn

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

使用@Async方法的JUnit回滚事务

我正在使用编写集成测试SpringJUnit4ClassRunner。我有一个基类:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({ /*my XML files here*/}) 
@Ignore
public class BaseIntegrationWebappTestRunner {

@Autowired
protected WebApplicationContext wac; 

@Autowired
protected MockServletContext servletContext; 

@Autowired
protected MockHttpSession session;

@Autowired
protected MockHttpServletRequest request;

@Autowired
protected MockHttpServletResponse response;

@Autowired
protected ServletWebRequest webRequest;

@Autowired
private ResponseTypeFilter responseTypeFilter;

protected MockMvc mockMvc;

@BeforeClass
public static void setUpBeforeClass() {

}

@AfterClass
public static void tearDownAfterClass() {

}

@Before
public void setUp() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).addFilter(responseTypeFilter).build();
}

@After
public void tearDown() {
    this.mockMvc = null;
}
}
Run Code Online (Sandbox Code Playgroud)

然后,我将其扩展并使用mockMvc创建一个测试: …

java junit spring-test spring-transactions spring-async

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