在构造函数中创建的对象进行测试时遇到困难。

huangapple go评论92阅读模式
英文:

Having difficulty to test an object created in a constructor

问题

public counterPublisher() {
    try {
        ObjectPublisher objPub = new ObjectPublisher.build().port(55).build();
    }
    catch(SocketException || UnknownHostException) {
        Logger.record("...");
    }
}

@Test(expected = SocketExcetion)
public void testConst() Throws Exception {

    doThrow(new SocketException()).when(ObjectPublisher).build();

    counterPublisher cp = new counterPublisher();

    // 不知道如何覆盖这部分,以达到90%的覆盖率
    // 我在jacoco中的分支覆盖率比例为0
    // 在网上找不到答案。

    // 要加一些背景信息:我尝试了网上找到的一切方法,但它们对这个问题没有用。我会感激任何帮助。
}

Note: I've translated the code portions as requested, but please keep in mind that the code you provided contains some syntax errors and inconsistencies. You may want to review and correct those issues.

英文:
public counterPublisher() {
try {
ObjectPublisher objPub = new ObjectPublisher.build().port(55).build();
}
catch(SocketException || UnknownHostException) {
Logger.record(“...”);
}





@Test(expected = SocketExcetion)
public void testConst() Throws Exception {

doThrow(new SocketException()).when(ObjectPublisher).build();

counterPublisher cp = new counterPublisher();



// don’t know how to cover this, so I can reach 90% coverage
// I’m getting branch coverage  ratio 0 in jacoco
// could not find an answer online. 

To add a context: i tried everything I found online but they didn’t work for this issue. I would appreciate any help

答案1

得分: 1

有三种解决方案,我可以考虑:一种是使用powermock,它允许您模拟静态方法。

但是,这不是我的首选。我个人不喜欢使用powermock来模拟静态或私有方法,因为如果我需要这样做,实际上是在暗示我的设计需要一些改进。

第二种方法是为您的ObjectPublisher创建一个工厂,类似于ObjectPublisherFactory。然后,您可以轻松地模拟该工厂,使其返回一个模拟的ObjectPublisher。

// 在您的类中创建工厂的实例:
private ObjectPublisherFactory factory;

public counterPublisher() {
try {
ObjectPublisher objPub = factory.build(55);
}
catch(SocketException || UnknownHostException) {
Logger.record(...);
}

现在,您可以使用模拟工厂而不是真实工厂来获得所需的结果。

第三种方法是在您的类中创建一个包私有方法来返回ObjectPublisher。

ObjectPublisher getObjectPublisher(int port) {
    return new ObjectPublisher.build().port(port).build();
}

现在,在您的测试中,您可以子类化正在测试的类,使getter方法返回模拟对象。

英文:

There are three solutions that I can think of: one is use powermock, which allows you to mock static methods.

That however, is not my preference. I, personally, don't like powermock to mock static or private methods because if I need this, this is in fact a hint that my design needs some work.

The second approach would be to create a factory for your ObjectPublisher, something like ObjectPublisherFactory. You can then easily mock the factory, to make it return a mock ObjectPublish.

// Create a instance of the factory in your class:
private ObjectPublisherFactory factory;

public counterPublisher() {
try {
ObjectPublisher objPub = factory.build(55);
}
catch(SocketException || UnknownHostException) {
Logger.record(“...”);
}

You can now use the mock factory instead of the real factory, to get the desired result.

A third approach is to create a package private method in your class to return the ObjectPublisher

ObjectPublisher getObjectPublisher(int port) {
    return new ObjectPublisher.build().port(port).build();
}

Now in your test you can subclass the test your are testing, making the getter return the mocked object.

答案2

得分: 0

这是因为您在方法内创建了对象。

使用PowerMocktito来模拟新实例。

ObjectPublisher objPub = PowerMockito.mock(ObjectPublisher.class);

PowerMockito.whenNew(MyQueryClass.class).withNoArguments().thenReturn(objPub);
doThrow(new SocketException()).when(objPub).build();
counterPublisher() // 调用此方法:

此外,请确保准备好JUnit单元测试。

@PrepareForTest(ClassThatCreatesTheNewInstance.class) // JUnit类注解。
英文:

This is because you created the Object inside the method.

Use Power Mocktito to mock the new instance.

ObjectPublisher objPub = PowerMockito.mock(ObjectPublisher.class);

PowerMockito.whenNew(MyQueryClass.class).withNoArguments().thenReturn(objPub);
doThrow(new SocketException()).when(ObjectPublisher).build();
counterPublisher()// call this method:

also, make sure you prepare the junit if u test.

@PrepareForTest(ClassThatCreatesTheNewInstance.class)//Junit Class annotation.

huangapple
  • 本文由 发表于 2020年8月3日 11:22:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63223297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定