Java – can you confuse code with unit tests?

I wanted to confuse our Java Web application code in our existing ant build script, but I encountered a problem with unit testing I confused the code after code compilation, before jar editing and before running unit tests

However, if I confuse my production code with my test code, all my tests will fail because they try to call methods that no longer exist because they have been renamed by the obfuscator I can mark some methods as not confusing so that external systems (such as our test suite) can use them, but since we are shooting for high unit test coverage, we need to mark all methods as tamper proof

If I also confuse test classes, I will encounter two problems:

1: The production class and test class are merged into the same output directory. I can't get them from production Exclude test classes from jar files

2: I can't run a normal ant batch call:

<batchtest todir="${basedir}/reports">
      <fileset dir="${basedir}/components/common/build-zkm">
           <include name="**/*Test.class"/>
      </fileset>
 </batchtest>

Because the obfuscator changed the name of the test

I can generate in war / . Run the obfuscator on the ear file, but I want our unit tests to run against the modified code to get rid of any errors caused by the obfuscator

I am currently working with Zelix klassmaster, but I am still in the evaluation stage, so if they can work better, I will accept other options

Solution

Can you tell it to run the obfuscator so that it can effectively refactor the code, including the reference of the test (that is, when the production name changes, the test code changes its reference), but don't confuse the test itself (that is, don't change the name or method of its test class)? In view of my previous experience with obfuscators, I hope to be able to work

For example, suppose we have sources that have not been obfuscated:

public class ProductionCode
{
    public void productionMethod() {}
}

public class ProductionCodeTest
{
    public void testProductionMethod()
    {
        new ProductionCode().productionMethod();
    }
}

You want to set the options for the obfuscator to make it effective:

public class Xyzzy
{
    public void ababa() {}
}

public class ProductionCodeTest
{
    public void testProductionMethod()
    {
        new Xyzzy(). ababa();
    }
}

In this way, your "run test" ant task should remain unchanged because the API of the test has not changed - just the implementation of the method

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
分享
二维码
< <上一篇
下一篇>>