小编Mic*_*jan的帖子

如果使用@PrepareForTest,Jacoco会错过所有覆盖范围

我遇到了一个有趣的问题,如果使用的话,Jacoco会报告单位测试下的0%覆盖率@PrepareForTest.

长话短说,我的单元测试是使用@RunWith(PowerMockRunner.class)@PrepareForTest(SomeStaticMethodClass.class)执行单元测试的组合,需要模拟静态方法.你可以阅读这两篇文章,我就嘲笑静态方法(发表文章1,2).模拟静态方法效果很好.不好的副作用是,当Jacoco使用@PrepareForTest注释运行单元测试时,任何测试的类单元都会被忽略,并且该类的Jacoco报告显示0%的覆盖率.我已经将问题专门针对@PrepareForTest注释.我这样做是通过创建一个简单的POJO单元测试,在该POJO上获得100%的覆盖率,然后在@PrepareForTest单元测试的注释中添加.在添加该注释后,Jacoco报告显示POJO覆盖率为0%.

所以任何人对此都有任何想法?

以下是一些技术信息:

java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) Client VM (build 25.45-b02, mixed mode)

Apache Maven 3.0.4 (r1232337; 2012-01-17 02:44:56-0600)

<plugin>
  <groupId>org.jacoco</groupId> 
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.7.4.201502262128</version>
. . .
</plugin>


<plugin>
  <groupId>org.apache.maven.plugins</groupId> 
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.18.1</version>
  . . .
</plugin>


    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.5.6</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-easymock</artifactId>
        <version>1.5.6</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.5.6</version>
        <scope>test</scope>
    </dependency>
    <dependency> …
Run Code Online (Sandbox Code Playgroud)

java unit-testing mocking powermock jacoco

9
推荐指数
0
解决办法
1263
查看次数

Jersey ResourceConfig 不会自动发现,但应用程序会吗?

当我有一个非常基本的Application. Jersey 时,它会自动发现项目中的所有 REST 资源,而我不必手动注册它们:

import javax.ws.rs.ApplicationPath;
@ApplicationPath("/rest")
public class Application extends javax.ws.rs.core.Application {

}
Run Code Online (Sandbox Code Playgroud)

但是当我切换到使用 Jersey specific 时ResourceConfig,自动发现似乎不起作用。我必须#registerClasses()或者添加如下所示的包:

@ApplicationPath("rest")
public class ResourceConfig  extends org.glassfish.jersey.server.ResourceConfig {
    public ResourceConfig() {
        super();        
        register(RolesAllowedDynamicFeature.class);
        super.packages(true, "org.example");
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法ResourceConfig自动发现 REST 资源,例如Application无需单独注册类或添加应用程序包?

java rest jax-rs jersey jersey-2.0

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

将bean注入Jersey @Path JAX-RS资源中是否需要bean-discovery-mode =“ all”?

我正在使用Payara 4.1.1.161。我有一个Jersey @Path JAX-RS资源,我要做的就是使用CDI @将一个bean注入其中。我已经尝试了许多不同的组合来使其工作,但是到目前为止,我成功使它成功的唯一方法是在beans.xml中设置bean-discovery-mode =“ all”。

我知道“带注释”是没有bean.xml的首选方式。但是,每次尝试使用“带注释的”时,调用JAX-RS资源都会失败,如下所示:

MultiException stack 1 of 1
org.glassfish.hk2.api.UnsatisfiedDependencyException: 
There was no object available for injection at
SystemInjecteeImpl(requiredType=InjectMe, parent=InjectResource,
qualifiers={}, position=-1, optional=false, self=false,
unqualified=null, 1000687916))
Run Code Online (Sandbox Code Playgroud)

或者我在部署如下所示的应用程序时遇到了故障:

Exception during lifecycle processing
java.lang.Exception: java.lang.IllegalStateException:
ContainerBase.addChild: start: org.apache.catalina.LifecycleException:
org.apache.catalina.LifecycleException:
org.jboss.weld.exceptions.DeploymentException: WELD-001408: 
Unsatisfied dependencies for type InjectMe with qualifiers @Default
at injection point [BackedAnnotatedField] 
@Inject private org.thoth.jaspic.web.InjectResource.me
Run Code Online (Sandbox Code Playgroud)

这是我的应用程序设置。

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">
</beans>
Run Code Online (Sandbox Code Playgroud)

JAX-RS应用

@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationResourceConfig  extends org.glassfish.jersey.server.ResourceConfig {
    public ApplicationResourceConfig() …
Run Code Online (Sandbox Code Playgroud)

java dependency-injection jax-rs cdi payara

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

当使用@Stateless bean时,Payara 5 @JMSConnectionFactory无法注入

我正在测试一些关于Paraya 5的JMS工作.具体来说是5.181.下面是我简单的无状态bean的代码. @JMSConnectionFactory失败!该JMSContext context变量始终null.然而@Resource(lookup = "jms/HelloWorldConnectionFactory")成功......任何想法为什么?我更喜欢使用JMSContext.

@Stateless
public class HelloWorldMessageSender {

    @JMSConnectionFactory("jms/HelloWorldConnectionFactory")
    protected JMSContext context;

    @Resource(lookup = "jms/HelloWorldConnectionFactory")
    protected ConnectionFactory connectionFactory;

    @Resource(lookup = "jms/HelloWorldQueue")
    protected Queue queue;

    public String send() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");
        String txt = String.format("Hello world message %s", sdf.format(new Date()));
        if (context != null) {
            System.out.printf("Use JMSContext to produce%n");
            context.createProducer().send(queue, txt);
        }
        if (connectionFactory != null) {
            System.out.printf("Use ConnectionFactory to produce%n");
            try (
                Connection connection …
Run Code Online (Sandbox Code Playgroud)

java ejb jms cdi payara

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

Spring Boot 是否需要 @Import 注释?

我正在使用 Spring Boot 2.1.5。我有多个用@Configuration.

我想知道是否有必要列出所有这些配置类@Import?或者组件扫描器会找到所有带@Configuration注释的类并自动执行吗?

spring spring-boot

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