简体   繁体   中英

How to test a method of a class with constructor arguments using Mockito/Powermock

I have a Mockito/PowerMockito issue!

The class to test is as below:

public class ClassToTest {

    private String string;

    public ClassToTest(String s) {
        this.string = s;
    }

    public String concatenate() {
        return string.concat(" - Done!");
    }

    public ClassToTest create(String s) {
        return new ClassToTest(s);
    }
}

The test class i wrote:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassToTest.class)
public class ClassToTestTest {

    @Test
    public void concatenate() throws Exception {
        ClassToTest classToTest = Mockito.mock(ClassToTest.class);
        PowerMockito.whenNew(ClassToTest.class).withArguments(Mockito.anyString()).thenReturn(classToTest);
        classToTest.concatenate();
    }
}

Question - How do i set a value of the instance variable named "string" from test class so that i can test concatenate method(concatenate method uses the constructor initialized "string" variable") Currently the debug point is not even going inside concatenate() method. I need to do this with either mockito/powermock.

Note - The example I have given is a representation of the issue i am facing in real time.

Any leads would help me.

Thanks in advance!!

Your test is pointless for several reasons.

  1. you mock the class you're trying to test
  2. you mock a method that is not even called in the test
  3. you don't verify the result of the method you call.

Your test can just be

ClassToTest tested = new ClassToTest("test"); // create instance
String concatResult = tested.concatenate();   // call method under test
assertThat(concatResult).isEqualTo("test - Done");  // verify result

No need to mock anything. If you want to test the create method (of which I don't see the point at all btw), you can do

ClassToTest tested = new ClassToTest(""); // the String is never used anyway
ClassToTest created = tested.create("test"); // call method under test
assertThat(created.concatenate()).isEqualTo("test - Done"); // verify

If you mock classes you're testing, you don't test the classes behavior, but only the mocked result. Consider

Don't do this

ClassToTest mock = mock(ClassToTest.class);
ClassToTest other = mock(ClassToTest.class);
when(mock.create(anyString()).thenReturn(other);
when(other.concatenate(anyString()).thenReturn("expected");
ClassToTest created = mock.create("test");
String result = created.concatenate("lala");
assertThat(result).isEqualTo("expected"); // duh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM