Is there any way to reinitialize a static class in Java?

I try to unit test a class that references static data from another class I can't "not" use this static class, but obviously running multiple tests has become a problem So my question is this Is there any way to reinitialize static classes in JUnit testing? Which test will not be affected by previous tests?

So in other words, some do this:

Foo.setBar("Hello");

// Somehow reinitialize Foo

String bar = Foo.getBar(); // Gets default value of bar rather than "Hello"

Unfortunately, I can't change foo, so I'm stuck

In the editor's opinion, I made my example a little too simple In the actual code, "bar" is set by the system property and is set as an internal static variable So once it starts running, I can't change it

Solution

If you use powermock, you can simulate static methods - that's what you should do

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>