How can I test a final class with private constructor?

barbara

How can I test a class like this (see code below)?

public final class A {

    public static final String FIRST = "1st";
    public static final String SECOND = "2nd";

    private A() {
        // NOP
    }
}

For now all my coverage tools say that constructor isn't covered with tests. My tests look like on this:

assertEquals(A.FIRST, "1st");
assertEquals(A.SECOND, "2nd");

How can I test my class?

UPD

This code solved my problem.

@Test
public void magic() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Constructor<A> constructor = A.class.getDeclaredConstructor();
    constructor.setAccessible(true);
    A instance = constructor.newInstance();

    assertNotNull(instance);
}

Yes, I agree that this isn't the best solution. But it works :)

Alex Kleiman

Reflection is probably the way to go: How do I test a class that has private methods, fields or inner classes?

By the way, this is possibly a duplicate of the question in the link provided.

Alternatively, could you create a wrapper method that is protected which simply forwards all calls to the private method?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How can references to private class members be dangerouse

来自分类Dev

How to extend a class with a different constructor?

来自分类Dev

How can you change the parameterless constructor of an Entity Framework 6 database-first generated class?

来自分类Dev

How can I unit test Eclipse Command Handlers?

来自分类Dev

JMockit dynamic partial mock a constructor of the class under test

来自分类Dev

Error in the program with the private constructor

来自分类Dev

How can I determine the default encoding in a portable class library?

来自分类Dev

How can I copy a templated instance with a virtual base class?

来自分类Dev

In PHPUnit, how can I mock a function that is not part of a class?

来自分类Dev

How can I zero just the padding bytes of a class?

来自分类Dev

Can we change the value of a final variable of a mutable class?

来自分类Dev

How to test controller class in MVC?

来自分类Dev

How to Access private parameterized method of private inner class with in a static class in JAVA

来自分类Dev

Can a class in C# 6.0 have a protected primary constructor?

来自分类Dev

Why do I need another constructor in an extended abstract class?

来自分类Dev

Can I create a class which can be unpacked?

来自分类Dev

How can I run a specific 'thread group' in a JMeter 'test plan' from the command line?

来自分类Dev

Why can I access a protected method in a test?

来自分类Dev

不变对象中的private final vs public final字段(java)

来自分类Dev

How to make wrapper class forward its constructor arguments to std::vector's constructor?

来自分类Dev

How to create private methods that can be redefined in subclasses in Dart?

来自分类Dev

How can i enable true ToolStripMenuItem in form1 when backgroundworker completed his operation in a new class?

来自分类Dev

How can I define an UUID for a class, and use __uuidof, in the same way for g++ and Visual C++?

来自分类Dev

How can I apply a jQuery function to all elements with the same class.?

来自分类Dev

Can I pass a Class type as a procedure parameter

来自分类Dev

Why I can instantiate a static inner class?

来自分类Dev

Can I create a class [] operator in Typescript

来自分类Dev

Circular dependency in the class constructor

来自分类Dev

Python: numba, how can constructor take a function as an argument?

Related 相关文章

  1. 1

    How can references to private class members be dangerouse

  2. 2

    How to extend a class with a different constructor?

  3. 3

    How can you change the parameterless constructor of an Entity Framework 6 database-first generated class?

  4. 4

    How can I unit test Eclipse Command Handlers?

  5. 5

    JMockit dynamic partial mock a constructor of the class under test

  6. 6

    Error in the program with the private constructor

  7. 7

    How can I determine the default encoding in a portable class library?

  8. 8

    How can I copy a templated instance with a virtual base class?

  9. 9

    In PHPUnit, how can I mock a function that is not part of a class?

  10. 10

    How can I zero just the padding bytes of a class?

  11. 11

    Can we change the value of a final variable of a mutable class?

  12. 12

    How to test controller class in MVC?

  13. 13

    How to Access private parameterized method of private inner class with in a static class in JAVA

  14. 14

    Can a class in C# 6.0 have a protected primary constructor?

  15. 15

    Why do I need another constructor in an extended abstract class?

  16. 16

    Can I create a class which can be unpacked?

  17. 17

    How can I run a specific 'thread group' in a JMeter 'test plan' from the command line?

  18. 18

    Why can I access a protected method in a test?

  19. 19

    不变对象中的private final vs public final字段(java)

  20. 20

    How to make wrapper class forward its constructor arguments to std::vector's constructor?

  21. 21

    How to create private methods that can be redefined in subclasses in Dart?

  22. 22

    How can i enable true ToolStripMenuItem in form1 when backgroundworker completed his operation in a new class?

  23. 23

    How can I define an UUID for a class, and use __uuidof, in the same way for g++ and Visual C++?

  24. 24

    How can I apply a jQuery function to all elements with the same class.?

  25. 25

    Can I pass a Class type as a procedure parameter

  26. 26

    Why I can instantiate a static inner class?

  27. 27

    Can I create a class [] operator in Typescript

  28. 28

    Circular dependency in the class constructor

  29. 29

    Python: numba, how can constructor take a function as an argument?

热门标签

归档