小编RaT*_*RaT的帖子

使用mockito或Jmockit模拟私有静态最终字段

我在我的类中使用私有静态最终LOGGER字段,我希望LOGGER.isInfoEnabled()方法返回false.如何使用mockito或jMockit模拟静态final字段

我的班级是:

  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;

  public class Class1 {

  private static final Logger LOGGER = LoggerFactory.getLogger(Class1.class);

    public boolean demoMethod() {
       System.out.println("Demo started");
       if (LOGGER.isInfoEnabled()) {
         System.out.println("@@@@@@@@@@@@@@ ------- info is enabled");
       } else {
         System.out.println("info is disabled");
       }
       return LOGGER.isInfoEnabled();
    }
  }
Run Code Online (Sandbox Code Playgroud)

它的junit是:

import mockit.Mocked;
import mockit.NonStrictExpectations;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.testng.Assert.*;
import com.source.Class1;

public class MyTest {

  @InjectMocks
  Class1 cls1;

  @BeforeMethod
  public void initMocks() { …
Run Code Online (Sandbox Code Playgroud)

java junit jmockit static-members mockito

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

启动后立即启动Spring启动应用程序

我正在尝试构建一个简单的SpringBoot应用程序.当我运行我的spring启动应用程序时,它在启动后立即关闭,下面是控制台日志:

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v1.4.1.BUILD-SNAPSHOT)

2016-09-06 18:02:35.152  INFO 22216 --- [           main] com.example.SpringBootDemo1Application   : Starting SpringBootDemo1Application on IN-FMCN882 with …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot

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

从jacoco覆盖率报告中排除jar文件的类

Jacoco代码覆盖率报告还包括我使用下面的maven依赖项添加的“系统路径jar”中的类

<dependency>
            <groupId>axis</groupId>
            <artifactId>axis</artifactId>
            <version>1.2.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/axis-1.2.1.jar</systemPath>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

我试图从jacoco插件中排除此jar文件,如下所示:

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.2.201409121644</version>
                <configuration>
                    <excludes>
                        <exclude>**/org/apache/**/*</exclude>
                        <exclude>**/org/globus/**/*</exclude>
                    </excludes>
                </configuration>

                <executions>
                    <execution>
                        <id>pre-unit-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <!-- Sets the path to the file which contains the execution data. -->
                            <destFile>${project.build.directory}/coverage-reports/jacoco.exec</destFile>
                            <!-- Sets the name of the property containing the settings for JaCoCo 
                                runtime agent. -->
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <!-- Sets the path to the file which contains the execution data. -->
                            <dataFile>${project.build.directory}/coverage-reports/jacoco.exec</dataFile>
                            <!-- …
Run Code Online (Sandbox Code Playgroud)

java maven jacoco

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

仅在编辑的代码上应用格式化程序-eclipse

当我尝试保存所做的更改时,我已经在eclipse中为我的代码实现了格式化程序,它将更改格式应用于整个文件。

是否可以仅在更改的完整文件代码中应用格式化程序?

@编辑

我想在保存时在现有文件上应用此格式化程序。假设我有一个包含10种方法和大约1000行的文件。我在该文件中添加了新方法并应用了保存。然后,我希望格式化程序仅将格式化程序应用于新添加的方法,而不应用于现有的10种方法。我必须处理项目中的每个文件。 @formatter:off注释在这里无济于事。

java eclipse save formatter

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

使用java.util.stream api + JAVA8解析字符串列表

我想使用新的java8 API(如stream,lambda,谓词)来使用下面的代码,并将其减少到最小行数

public static List<Long> validateChannelList(String channelList) {
    List<Long> channelListNumber = new ArrayList<Long>();
            String[] channels = channelList.split(",");
    for (String channel : channels) {
                    channelListNumber.add(Long.parseLong(channel));
                }
    }
Run Code Online (Sandbox Code Playgroud)

请使用JAVA8帮助将foreach循环或整个方法减少到最小行数

java collections lambda java-8 java-stream

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