简体   繁体   English

使用Java和Maven在编译时生成类

[英]Generate classes in compile time with java and maven

I have a project that have one junit test class, i need to generate multiple junit classes in compile time, what exactly i want to do is have the same junit class but with a change in a property value. 我有一个具有一个junit测试类的项目,我需要在编译时生成多个junit类,而我真正想做的是拥有相同的junit类,但属性值有所变化。

I could not accomplish this in runtime, this shold be done in compile time. 我无法在运行时中完成此工作,此工作在编译时完成。

I tried to see the aspectj but could not find how to generate classes in compile time. 我尝试查看Aspectj,但找不到在编译时如何生成类的方法。

is that even possible in java ?? 在Java中甚至有可能吗?

You can use @Parameterized as stated by Andy, or inherit from a base Test class : 您可以按照Andy的说明使用@Parameterized,或从基本Test类继承:

abstract class CommonTests {
    @Test
    public void testA() { 
        assertEquals(1, getValue()); 
        ...
    }

    @Test
    public void testB() { ...}
    ....
    abstract int getValue();
}

class Test1 extends CommonTests {
   int getValue() { return 1;}
}

class Test2 extends CommonTests {
   int getValue() { return 2;}
}

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

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