我在我的类中使用私有静态最终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) 我正在尝试构建一个简单的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) 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) 当我尝试保存所做的更改时,我已经在eclipse中为我的代码实现了格式化程序,它将更改格式应用于整个文件。
是否可以仅在更改的完整文件代码中应用格式化程序?
@编辑
我想在保存时在现有文件上应用此格式化程序。假设我有一个包含10种方法和大约1000行的文件。我在该文件中添加了新方法并应用了保存。然后,我希望格式化程序仅将格式化程序应用于新添加的方法,而不应用于现有的10种方法。我必须处理项目中的每个文件。
@formatter:off注释在这里无济于事。
我想使用新的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 ×5
collections ×1
eclipse ×1
formatter ×1
jacoco ×1
java-8 ×1
java-stream ×1
jmockit ×1
junit ×1
lambda ×1
maven ×1
mockito ×1
save ×1
spring ×1
spring-boot ×1