0
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

Gunther
  • 13
  • 5
  • 1
    Please do not post pseudo code. The above wouldn't even compile. Read [mcve] and enhance your question accordingly. Tell us what mocking framework (version) you are using and so on. Your question is really not clear as it is written right now. – GhostCat Aug 03 '20 at 03:39
  • And assuming that you are actually using mockito... I think you have to read some tutorials about it. You cant easily use that to intercept usages of new() like this. – GhostCat Aug 03 '20 at 03:41
  • Using Mockito. I’ll add some more context. But in short: I want to throw an exception when object is being created in try block – Gunther Aug 03 '20 at 03:44
  • 1
    You can't do that with mockito. And you shouldn't, either. You should rather try to write production code that can be easily be tested with simple tests, that never require more then using the basic features of mockito. – GhostCat Aug 03 '20 at 03:48

2 Answers2

1

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Marcio Lucca
  • 370
  • 2
  • 10
0

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.
  • 1
    Side note: using power mock should never be the first option. Rather strive to write production code that can be tested without using power mock. Power mock clashes with various code coverage tools for example. Long story short: do not recommend power mock to a newbie learning unit testing and mocking. That is like telling a future carpenter that using a huge big hammer is the only way to solve problems. Leading that person to do the wrong thing for the rest of their career... – GhostCat Aug 03 '20 at 03:46
  • Yes, I agree, but the way he states his problem this is the direct solution. There is one solution that leads him to refactor his code to be a testable one like Inversion of control or use a factory. But if he has no choice or it costly to refactor. then that's the escape. Not every system is perfect though. – Jose Marlon Ancajas Aug 03 '20 at 03:55
  • As said, it isn't necessarily the "direct" solution. The user tagged with jacoco, and as said: albeit possible, getting PowerMock to work with jacoco coverage measurements requires **extra** efforts: https://www.igorkromin.net/index.php/2018/02/20/jacoco-reports-missing-code-coverage-for-tests-using-powermock/ ... believe me: PowerMock is almost never "the answer". – GhostCat Aug 03 '20 at 06:51