相关疑难解决方法(0)

当""== s为假但"".equals(s)为真时

编辑 感谢您的快速回复.请看看真正的问题是什么.这次我大胆了.

我理解==和.equals之间的区别.所以,这不是我的问题(我实际上添加了一些上下文)


我正在为空字符串执行以下验证:

if( "" == value ) { 
    // is empty string 
} 
Run Code Online (Sandbox Code Playgroud)

过去从db中获取值或从另一个节点反序列化对象时,此测试失败,因为两个字符串实例确实是不同的对象引用,尽管它们包含相同的数据.

所以这些情况的解决方案是

if( "".equals( value ) ) {
   // which returns true for all the empty strings
}
Run Code Online (Sandbox Code Playgroud)

我很好.这很清楚.

今天这又发生了一次,但它让我感到困惑,因为这次应用程序是一个非常小的独立应用程序,根本不使用网络,所以没有从数据库中提取新字符串,也不从另一个节点去激活.

所以问题是:


其他情况下:

"" == value // yields false 
Run Code Online (Sandbox Code Playgroud)

"".equals( value ) // yields true
Run Code Online (Sandbox Code Playgroud)

对于本地独立应用程序?

我很确定代码中没有使用新的String().

并且字符串引用可以是""的唯一方法是因为它直接在代码中分配""(或者我认为的那样),如:

String a = "";
String b = a;

assert "" == b ; // this …
Run Code Online (Sandbox Code Playgroud)

java comparison equality equals

33
推荐指数
4
解决办法
2万
查看次数

JUnit assertEquals更改字符串

我有一个JUnit测试,如下所示:

@Test
public void testToDatabaseString() {
  DateConvertor convertor = new DateConvertor();
  Date date = convertor.convert("20/07/1984:00:00:00:00");
  String convertedDate = convertor.toDatabaseString(date);

  assertEquals("to_date('20/07/1984:00:00:00:00', 'DD/MM/YYYY HH24:MI:SS')",convertedDate);
}
Run Code Online (Sandbox Code Playgroud)

测试失败说明:

org.junit.ComparisonFailure: expected:<to_date('20/07/1984[00:]00:00:00', 'DD/MM/YY...> but was:<to_date('20/07/1984[ ]00:00:00', 'DD/MM/YY...>
Run Code Online (Sandbox Code Playgroud)

特别感兴趣的是为什么预期值是:

to_date('20/07/1984[00:]00:00:00', 等等...

当我的测试中的字符串文字显然是:

"to_date('20/07/1984:00:00:00:00', 等等...

有谁能解释一下?为什么要添加"[00:]"?感谢帮助.

java junit

6
推荐指数
1
解决办法
5304
查看次数

为什么在我的String值之后有括号?

我有这样的测试:

String dir = "/foo";
String fileName = "hello.txt";
String pathString = dir + "/" + fileName;
String text = "hello world!";
MyTask task = new MyTask();

FileSystem fs = Jimfs.newFileSystem(Configuration.unix());

Path foo = fs.getPath(dir);
Files.createDirectory(foo);
Path hello = foo.resolve(fileName);
Files.write(hello, ImmutableList.of(text), StandardCharsets.UTF_8);

task.setFileSystem(fs);
String fileValue = task.readFile(pathString);

// Doing this just shows "fail whale: hello world!"
// fail("fail whale: " + fileValue); 

// Failure from here adds brackets
assertEquals(text, fileValue);
Run Code Online (Sandbox Code Playgroud)

我这样失败了:

org.junit.ComparisonFailure: expected:<hello world![]> but was:<hello world![
]>
Run Code Online (Sandbox Code Playgroud)

如果我反转参数,我可以看到它 …

java junit

4
推荐指数
1
解决办法
723
查看次数

JUnit 断言两个字符串是否相等

所以我查了这个问题并尝试过,但没有成功。

我的代码应该测试该方法是否通过使用Streams.

    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    PrintStream myStream = new PrintStream(outStream);
    System.setOut(myStream);
    o.doSomething(); //printing out Hi
    System.out.flush();
    System.setOut(savedOldStream);//setting it back to System.out
    assertEquals(outStream.toString(),"Hi");
Run Code Online (Sandbox Code Playgroud)

但是每次我运行 JUnit 时它都会失败。我也试过:assertTrue(outStream.toString().equals("Hi"));但这也不起作用。

这是 doSomething() 方法:

public void doSomething () {
    System.out.println("Hi");
}
Run Code Online (Sandbox Code Playgroud)

java junit outputstream

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

标签 统计

java ×4

junit ×3

comparison ×1

equality ×1

equals ×1

outputstream ×1