我想知道单元测试私有方法是否是一个好习惯?
通常只应测试公共接口.
但是,我发现在复杂计算过程中,调用大量不同的私有方法,首先对私有方法进行单元测试更容易,然后对公共接口方法进行简单测试.
举个例子,假设你有一个音频播放器,你有功能:
void play(){ ... }
void pause(){ ... }
void seek(time t)
{
//All Private methods
checkIfValidTimeRange(...);
moveToFilePos(...);
fillBuffers(...);
}
Run Code Online (Sandbox Code Playgroud)
通常我会编写单元测试:checkIfValidTimeRange(...),moveToFilePos(...), fillBuffers(...).
但我不确定这样做是不是很好.
我在Maven项目中有Spring框架依赖项.我想附加Javadoc用于spring框架依赖项.
我添加到pom.xml以下行
<repositories>
<repository>
<id>springsource-repo</id>
<name>SpringSource Repository</name>
<url>http://repo.springsource.org/release</url>
</repository>
</repositories>
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
我已经安装了m2eclipse,我还在Download Artifact Sources/Javadoc设置中选中了选项.
当我跑步时mvn eclipse:eclipse,它没有显示任何警告.但是.jar没有下载javadoc 文件.
我当前的build.gradle如下所示。
repositories {
flatDir {
dirs 'lib'
}
maven {
mavenCentral()
}
dependencies {
compile "commons-logging:commons-logging:1.0.4",
"org.apache.httpcomponents:httpclient:4.4",
"org.apache.httpcomponents:httpcore:4.4",
"com.fasterxml.jackson.core:jackson-annotations:2.5.1",
"joda-time:joda-time:2.7",
compile files('../lib/abc.jar')
}
jar {
manifest{
attributes ("Version" : project.version, "${parent.manifestSectionName}")
attributes ("Name" : project.name, "${parent.manifestSectionName}")
}
from {
configurations.runtime.filter( {! (it.name =~ /abc.*\.jar/ )}).collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我已经在运行时删除了abc.jar,但是以同样的方式,我想删除更多的jar。简而言之,我想在肥罐中放几个罐,并且需要排除几个。那么我该如何实现呢?
我在DocuSign上创建了一个新的开发者帐户,生成了集成商密钥.现在我正在尝试提交检索帐户信息的GET请求,但我不断收到错误消息"未找到或禁用集成商密钥"以下是我使用fiddler"服务集成身份验证"提交的请求:
原始http请求
GET https://www.docusign.net/restapi/v2/login_information HTTP/1.1
X-DocuSign-Authentication: {"Username": "MyEmail","Password": "MyPassword","IntegratorKey": "IntegratorKeyCreatedFromAdminConsole"}
Host: www.docusign.net
Run Code Online (Sandbox Code Playgroud)
响应:
HTTP/1.1 401 Unauthorized
Cache-Control: no-cache
Content-Length: 128
Content-Type: application/json; charset=utf-8
X-DocuSign-TraceToken: 4694b892-09cb-41aa-b5fa-307d226e9dd0
Date: Tue, 10 Jan 2017 16:26:33 GMT
Strict-Transport-Security: max-age=31536000; includeSubDomains
{
"errorCode": "PARTNER_AUTHENTICATION_FAILED",
"message": "The specified Integrator Key was not found or is disabled."
}
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!!
是否可以使用Maven使用junit 5运行Spring Boot测试?我正在使用Spring Boot 2.0.0.M3,junit 5.0.0-M6,maven 3.5.0。其他junit5测试(没有spring上下文)也可以工作。
有一个简单的控制器:
@Controller
public class HomeController {
@GetMapping("/")
String home() {
return "home";
}
}
Run Code Online (Sandbox Code Playgroud)
并测试:
@ExtendWith(SpringExtension.class)
@WebMvcTest(HomeController.class)
@Import(SecurityConfig.class)
class HomeControllerTest {
@Autowired
private MockMvc mvc;
@Test
void shouldReturnHomeTemplate() throws Exception {
this.mvc.perform(get("/").accept(MediaType.TEXT_HTML))
.andExpect(status().isOk())
.andExpect(content().string(startsWith("<!DOCTYPE html>")));
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用intellij运行它时,一切正常,但是maven构建以失败告终:
[警告]分叉的JVM 1中的stdin流损坏。请参见转储文件somePath / target / surefire-reports / 2017-07-28T13-50-15_071-jvmRun1.dumpstream
--debug标志显示:
java.lang.OutOfMemoryError:Java堆空间
在内部,2017-07-28T13-50-15_071-jvmRun1.dumpstream我可以找到约100个相同的异常(每个spring log一个)
分叉的JVM 1中损坏的stdin流。Stream'13:50:15.914 [main]调试org.springframework.test.context.BootstrapUtils-从类[org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]实例化CacheAwareContextLoaderDelegate。java.lang.IllegalArgumentException:流标准输入已损坏。命令'第三个字符后的预期逗号'13:50:15.914 [main]调试org.springframework.test.context.BootstrapUtils-从类[org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]实例化CacheAwareContextLoaderDelegate。在org.apache.maven.plugin的org.apache.maven.plugin.surefire.booterclient.output.ForkClient $ OperationalData。(ForkClient.java:469)处。
Maven surefire插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.0.0-M6</version>
</dependency>
</dependencies>
</plugin>
Run Code Online (Sandbox Code Playgroud) 在我的程序中,我想要用户输入整数.我希望在用户输入非整数值时显示错误消息.我怎样才能做到这一点.我的程序是找到圈子的区域.在哪个用户将输入radius的值.但是如果用户输入了一个字符,我想要显示一条消息,说明输入无效.
这是我的代码:
int radius, area;
Scanner input=new Scanner(System.in);
System.out.println("Enter the radius:\t");
radius=input.nextInt();
area=3.14*radius*radius;
System.out.println("Area of circle:\t"+area);
Run Code Online (Sandbox Code Playgroud) java ×3
maven ×2
c++ ×1
docusignapi ×1
eclipse ×1
gradle ×1
javadoc ×1
junit5 ×1
private ×1
spring ×1
spring-boot ×1
testing ×1
unit-testing ×1