简体   繁体   English

如何在java中测试引用当前对象的构造函数?

[英]how do I test constructor in java which refers to current object?

My code doesn't meet the sonarqube code coverage the following piece of constructor says it needs test in sonarqube, I have written the following code to test it but it is not covering the code?我的代码不符合 sonarqube 代码覆盖率以下构造函数说它需要在 sonarqube 中测试,我编写了以下代码来测试它,但它没有覆盖代码? can some one help me where I am going wrong?有人可以帮助我哪里出错了吗?

@Test
public void OrderBuilderIT() {
    errorQueue.equals("amq:ORDER.T.SYSTEM.ERROR");
}

public OrderBuilder(String errorQueue) {
    this.errorQueue = errorQueue;
}

the code does not seem to be covered代码似乎没有被覆盖

I am assuming that you are trying to test the constructor call for the class with the error queue field value that you are passing while constructing and expecting the same value to be asserted.我假设您正在尝试使用错误队列字段值测试类的构造函数调用,该值是在构造和期望声明相同值时传递的。 Actually, your check does nothing in terms of an assertion.实际上,您的支票在断言方面没有任何作用。

Below is an assumption that you may be trying to achieve.以下是您可能试图实现的假设。 Also, should cover your sonar issue.此外,应该涵盖您的声纳问题。

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Test;
public class SomeConstructorTest {

private OrderBuilder orderBuilder;

 @Test
public void orderBuilderTestWithIncorrectErrorQueueMessage(){
    String expected = "amq:ORDER.T.SYSTEM.ERROR";
    String errorQueue = "dummyString";
    orderBuilder = new OrderBuilder(errorQueue);
    assertThat(expected, is(errorQueue)); // should fail if you are asserting string content
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何找到对象所指的内容? - How do I find what an object refers to? 'this'关键字也指向哪个当前类对象? - To which current class object, 'this' keyword refers too? Java - 方法如何知道“this”指的是哪个对象? - Java - How does a method know which object "this" refers to? 在Java中,使用DefaultSelenium对象在selenium中开始测试如何找到运行测试的浏览器? - In Java, starting a test in selenium with the DefaultSelenium object how do I find which browser the test is running on? 我怎么知道哪个参考“指”载体? - How do I know which reference 'refers' to a vector? 如何在Java中制作对象。 我有测试类但没有构造函数 - How to make object in java. I have Test Class but not constructor 如何在 java 中为具有随机 class 的构造函数创建单元测试 - How do I create a unit test for a constructor with Random class in java 如何测试返回复杂 object 的 function? - How do I test a function which returns a complex object? 如何对使用 Java UUID 的代码进行单元测试? - How do I unit test code which uses Java UUID? 如何测试在构造函数中也调用该方法的类的方法? (JUnit的) - How do I test methods of classes in which the method is also called in the constructor? (JUnit)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM