如何对复杂的类进行Java单元测试

如何对复杂的类进行Java单元测试

本文介绍了如何对复杂的类进行Java单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在弄清楚如何用单元测试解决问题上遇到了困难.我几乎没有单元测试"经验.我仅在绝对必要且更改最少的情况下才尝试更改 classUnderTest .

I'm experiencing a difficult time on figuring out how to approach a problem with unit testing. I have little to no 'unit testing' experience. I am trying to change the classUnderTest only if absolutely necessary with minimal changes.

我正在使用 JUnit 4 ,并且我愿意尝试使用 Mockito JMockit 或任何其他有助于有效进行单元测试的库.我正在为数据库使用 JDBC .

I am using JUnit 4, and I am willing to try to use Mockito, JMockit or any other libraries that would help with efficient and effectively unit testing. I am using JDBC for the database.

classUnderTest 中,我正在访问静态数据库.

Within the classUnderTest, I'm accessing a static database.

SpecializedDao specializedDao = SpecializedDao.getSpecializedDao();
ObjectX objectResult = specializedDao.currentObjectXByTimestamp(x, x, x, x, x, x);

,并且我正在尝试使用具有有效配置和无效配置的单元测试用例.解决此问题的正确方法是什么?

and I am trying to use do unit test cases with a valid config, and an invalid config. What is the correct way to approach this problem.

一些可能解决方案是:

  1. 以某种方式传递或注入"类 ObjectX
  2. 的假 ObjectXResult
  3. 模拟数据库(即使可能使用静态数据库引用,是否使用引用伪造的SpecializedDao类的单独的单元测试servlet?)
  4. Mockito injectMocks spy 一起使用,何时(使用方法调用参数)返回结果或其他 Mockito 提供的实现.
  5. 打开任何其他解决方案.
  1. Somehow passing in, or 'injecting' a fake ObjectXResult of class ObjectX
  2. Mock a database (if even possible with static db reference, use separate unit-test servlet that references a fake SpecializedDao class?)
  3. Use Mockito with injectMocks, spy, when(methodcallwithparameters) thenReturn result, or some other implementation Mockito provides.
  4. Open to any other solution.

问题:

classUnderTest 中,我正在使用另一个复杂的处理 class2 ,它完成了很多工作,如下所示:

Problem:

Within the classUnderTest, I am using another complex processing class2 that does a lot of the work like so:

result = class2.execute(x, x, x, x, x, x);

class2 处理内容并返回结果ENUM或某些异常.如何通过将此特定单元测试的 scope 保留在 classUnderTest 上来进行处理. class2 访问数据库,并做了很多工作,这就是为什么它是它自己的类的原因,但是我认为最后三个测试用例依赖于彻底测试 classUnderTest 的处理. >.

class2 does processing stuff and returns a result ENUM or some exceptions. How do I handle this with keeping the scope of this specific Unit Test on classUnderTest. class2 accesses the database, and does a lot of the work which is why it is its own class but I think the final three test cases are dependent upon the processing to thoroughly test classUnderTest.

感谢您的支持,我试图尽可能清楚地提出这个问题.

Thanks for bearing with me, I tried to make the question with as much clarity as possible.

推荐答案

如果您关心开发好的,有用的测试,我的建议是模拟数据库或您的代码可能与之交互的其他复杂类.

My recommendation, if you care about developing good, useful tests, is to not mock the database, or other complex classes your code may interact with.

相反,请考虑要测试的代码将要交付的特定业务场景,并编写 realistic (即,不要用廉价的-常常是不正确的-替代真实的类/组件)它们)和有意义的(从实际需求的角度来看)针对每种情况进行测试.

Instead, think of the specific business scenarios that your code under test is meant to deliver, and write a realistic (ie, without replacing real classes/components with cheap - and often incorrect - imitations of them) and meaningful (from the point of view of real-world requirements) test for each scenario.

也就是说,如果您仍然想模拟该DAO类,则可以使用JMockit如下进行操作:

That said, if you still want to mock that DAO class, it can be done as follows using JMockit:

@Test
public void exampleTest(@Mocked SpecializedDao mockDao) {
    ObjectX objectResult = new ObjectX();

    new Expectations() {{
        mockDao.currentObjectXByTimestamp(anyX, anyY, anyZ); result = objectResult;
    }};


    // Call the SUT.
}

(anyX等不是实际的JMockit参数匹配字段,当然-实际的是anyIntanyStringany等)

(anyX, etc. are not the actual JMockit argument matching fields, of course - the real ones are anyInt, anyString, any, etc.)

这篇关于如何对复杂的类进行Java单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 23:42