简体   繁体   English

使用 Junit 和 Mockito 在方法内创建对象的测试方法

[英]Testing Methods that create objects inside the method with Junit and Mockito

Hello I am fairly new to unit testing with Junit as well as Mockito.您好,我对使用 Junit 和 Mockito 进行单元测试还比较陌生。 I think I have a fairly reasonable understanding of the principles but I can't seem to find any explanations of what I am specifically trying to test online.我认为我对这些原则有相当合理的理解,但我似乎无法找到任何解释我具体尝试在线测试的内容。

I want to test a method, that calls several other methods (void and non-void), which also instantiates objects in the method body.我想测试一个方法,它调用其他几个方法(void 和 non-void),它也实例化方法体中的对象。 I unfortunately cannot share the code as it is not mine, but here is a general format:不幸的是,我不能分享代码,因为它不是我的,但这是一个通用格式:

class classToTest {
     private final field_1;

     public void methodToTest( string id, List object_1, List object_2) {
          try {
               Map<SomeObject_1, SomeObject_2> sampleMap = new HashMap<>();
               method_1(object_1, object_2); //void function modifies object_2
               field_1.method_2(id, object_2);
               Map<SomObeject_1, List<object>> groupedList = groupList(object_2)
               //Then some stuff is added to the sampleMap
          }
          //catch would be here
}

At the moment I only care about testing method_1, and I cannot test directly as it is a private method so I must go through this parent method call.目前我只关心测试method_1,我不能直接测试,因为它是一个私有方法,所以我必须通过这个父方法调用go。 I wish I could change the code but I have been asked to keep it the same and to test in this manner with Mockito and Junit.我希望我可以更改代码,但我被要求保持不变,并使用 Mockito 和 Junit 以这种方式进行测试。

I know I need to Mock an object of the class to Test as well as its parameter:我知道我需要模拟 class 的 object 来测试及其参数:

private classToTest classToTestObject;
@Mock private field_1 f1;

@Before
public void setup() {
     MockitoAnnotations.init.Mocks(this);
     classToTestObject = mock(classToTest.class, CALLS_REAL_METHODS);
}

but I don't know where to start my actual test, as in how I can essentially just execute that one method call and ignore all the rest.但我不知道从哪里开始我的实际测试,因为我基本上可以只执行一个方法调用并忽略所有 rest。 I can't just not ignore the other objects and method calls either as the main method will throw exceptions if they are not handled correctly.我不能只是不忽略其他对象和方法调用,因为如果处理不当,主要方法将引发异常。

Any help and guidance is much appreciated, sorry that I could not share the code.非常感谢任何帮助和指导,很抱歉我无法分享代码。 Thank You!谢谢你!

At the moment I only care about testing method_1, and I cannot test directly as it is a private method so I must go through this parent method call.目前我只关心测试method_1,我不能直接测试,因为它是一个私有方法,所以我必须通过这个父方法调用go。

Per your comment, and the note in your code:根据您的评论和代码中的注释:

method_1(object_1, object_2); //void function modifies object_2

You would set up a test that allows you to verify the expected final state of object_2 .您将设置一个测试,允许您验证 object_2 的预期最终object_2 You would do this with a real instance of the class, not a mock.您可以使用 class 的真实实例来执行此操作,而不是模拟。

@Test
public void method1Test() {
    // Assemble - your preconditions
    ClassToTest subject = new ClassToTest();
    List<SomeType> object_1 = new ArrayList();
    List<SomeOtherType> object_2 = new ArrayList();

    // Populate object_1 and object_2 with data to use as input
    // that won't throw exceptions. Call any methods on subject that put
    // it in the desired state

    // Act - call the method that calls the method under test
    subject.methodToTest("some id that makes the method run correctly", object_1, object_2);

    // Assert - one or more assertions against the expected final state of object_2
    assertThat(object_2).matchesYourExpectations();
}

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

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