我已经实现了一个简单的构建器模式 - 下面的代码.代码执行并运行,但构建器类中的每个'with ..'方法都会显示一条警告,其中"方法的返回值永远不会被使用"
public static class StreamParserBuilder{
//optional - have defaults:
private long spanLimit1 = 2000L;
private long spanLimit2 = 100000L;
private long spanLimit3 = 3000000L;
private String[] coordinates = {"L1", "R2"};
private String outputDirectory = System.getProperty("user.dir");
private boolean isLastSteam = false;
//required from the builder.
private String[] args;
private String inputFile;
private String streamData;
private boolean isPaired;
public StreamParserBuilder(String[] args, String inputFile, String streamData, boolean isPaired){
this.args = args;
this.inputFile = inputFile;
this.streamData = streamData;
this.isPaired = isPaired; …Run Code Online (Sandbox Code Playgroud) 我有一个 useEffect 钩子,它在安装组件时加载,如下所示:
useEffect(() => {
listFiles().then(keys => {
setKeys(keys)
console.log(keys)
}).catch(err => {
showFail(err.message)
})
}, [])
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用反应测试库来测试该函数,只需使用渲染函数:
beforeEach(() => {
render(<Dashboard />)
})
Run Code Online (Sandbox Code Playgroud)
但是,当我运行任何解决承诺并设置状态的测试时:
jest.mock('../utils/storage', () => ({
listFiles: jest.fn(() => Promise.resolve([])),
}))
Run Code Online (Sandbox Code Playgroud)
我最终收到一条关于使用actto 包装事件的奇怪警告消息:
Warning: An update to Dashboard inside a test was not wrapped in act(...).
When testing, code that causes React state updates should be wrapped into act(...):
act(() => {
/* fire events that update state */
});
/* assert on the …Run Code Online (Sandbox Code Playgroud) javascript unit-testing reactjs react-testing-library react-hooks
我正在开发一个将从命令行调用的程序。根据调用程序时定义的不同标志,该软件将是一种具有不同功能的“套件”。其中一些功能与其他功能大不相同,唯一的相似之处在于它所操作的文件类型。
我想知道实现这种功能的最佳设计模式是什么?我已经阅读了外观设计模式,该模式将每个模块分成不同的子系统。在不同的门面下构建每个功能将保持模块化,并允许我构建一个类来处理要调用的门面。
这是最好的方法,还是你会推荐一些不同的东西?
谢谢,山姆
我正在尝试连接到本地机器上的 h2 数据库以创建一个 sql DataSource 对象。我正在运行 Windows,但在我的项目 app.properties 文件中定义数据文件的路径时遇到了一些问题。
假设本地目录数据文件的路径是:
D:\projects\myproject\data\project
Run Code Online (Sandbox Code Playgroud)
如何为此定义连接 url?
我尝试了很多事情,包括以下内容:
project.db.url = jdbc:h2:tcp://localhost\\\\D:\\projects\\myproject\\data\\project
Run Code Online (Sandbox Code Playgroud)
然后我想可能是 JDBC URL 的问题,所以我尝试了:
project.db.url = jdbc:h2:tcp:\\\\localhost\\\\D:\\projects\\myproject\\data\\project
Run Code Online (Sandbox Code Playgroud) 我运行的代码到目前为止一直运行良好,但遇到了一个奇怪的错误.出错的代码片段从输入字符串中获取"字段"并将其转换为long.
抛出异常时的输入字符串 - 这是从命令行复制的.我无法从文件中获取实际字符串,因为输入文件是144GB而我不是在寻找它!一切看起来都很正常.在文件中,元素应以制表符分隔.
SBL_XSMJR247_ID:2331788_ 99 mm17 35305666 70 89M =35305769 190 NNGTCTTGGAGATCACGAGGCCCATGACAGCATGGAACAAGTCATGTGAAGCCCAGCCAGACACGATGAAAAATTTATAGACAAAAAGA ~~JGIIJJ@CFGIJJJJIJJIJJJIEIGJJIJJJJJJJHIJIJIIFIIJJJHFHH?DFFCACEEDDDDDDDDBBDDDEEDDDDDDDD@B xl:i:35305666 xr:i:35305754 xs:i:89 xd:A:f xm:A:u xa:A:"" xL:i:35305666 xR:i:35305855 xS:i:190xW:i:14 xP:i:0 xQ:i:0 xC:A:"" xD:A:"" PG:Z:novoalign AS:i:12 UQ:i:12 NM:i:2 MD:Z:0G0C87 PQ:i:18 SM:i:70 AM:i:70
Run Code Online (Sandbox Code Playgroud)
抛出的错误消息:
nfeerror: For input string: "35305666" xL
Run Code Online (Sandbox Code Playgroud)
是的它有点难看..
最后运行的代码产生错误.
/** Searches the input line and returns the last element of the field, i.e xL:i:20 returns 20.
* @param line line to search
* @param field fieldName
* @return long field value
*/
private long findValueLong(String …Run Code Online (Sandbox Code Playgroud) 我有一个简单的单元测试,它启动我的应用程序并测试某些服务是否已实例化。一种只是健全性检查。
但是,这些测试并未在我的完整测试套件中运行,当单独运行时,我收到错误 No tests found for given includes: [com.example.AppTest](filter.includeTestsMatching)
这是我的单元测试
import com.sblukcic.cli.flags.CLIParser;
import org.junit.Rule;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.rules.TemporaryFolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.fail;
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class AppTest {
@Autowired
private ApplicationContext context;
@Autowired
private CLIParser parser;
@Rule
public static TemporaryFolder folder = new TemporaryFolder();
private static File nonEmptyFile;
@BeforeAll
public static void setUp() throws Exception {
folder.create();
nonEmptyFile …Run Code Online (Sandbox Code Playgroud) 我写了一些代码,里面有很多字符串创建.为了避免一遍又一遍地避免昂贵的字符串创建,我正在使用java StringBuilder类.
我的代码有点像这样:
public String createString(){
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < Integer.MAX_VALUE ; i++){ //its not actually this, but it's a large loop.
stringBuilder.append(appendString())
}
return stringBuilder.toString();
}
Run Code Online (Sandbox Code Playgroud)
该appendString方法还实例化a StringBuilder并附加到它.
为了节省时间,我注意到我可以让我的appendString方法返回a StringBuilder,而不是String避免使用如下.toString()方法返回stringBuilder :
public StringBuilder appendString(){
StringBuilder appendBuilder = new StringBuilder();
appendBuilder.append("whatever");
return appendBuilder;
}
Run Code Online (Sandbox Code Playgroud)
这一切都很好,直到我查看StringBuilder文档并意识到类append(StringBuilder stringBuilder)上没有方法StringBuilder,只有一个append(String string).
所以我的问题是,这段代码是如何运行的?难道铸造StringBuilder来String呢?
java ×5
unit-testing ×2
h2 ×1
javascript ×1
jdbc ×1
junit ×1
react-hooks ×1
reactjs ×1
spring ×1
spring-boot ×1
string ×1