Tag: unit testing

LazyInitializationException,同时使用TestNG对Spring中使用的Hibernate实体类进行unit testing

在我的Spring配置中,我已经要求会话在我的视图中保持打开状态: 但是,这个bean似乎并不认为我的TestNGunit testing是一种观点。 ;-)那没关系,但是unit testing是否有类似的bean,以便在unit testing时避免可怕的LazyInitializationException? 到目前为止,我的一半unit testing因此而死亡。 我的unit testing通常如下所示: @ContextConfiguration({“/applicationContext.xml”, “/applicationContext-test.xml”}) public class EntityUnitTest extends AbstractTransactionalTestNGSpringContextTests { @BeforeClass protected void setUp() throws Exception { mockEntity = myEntityService.read(1); } /* tests */ @Test public void LazyOneToManySet() { Set entities = mockEntity.getSomeEntitySet(); Assert.assertTrue(entities.size() > 0); // This generates a LazyInitializationException } } 我已经尝试将setUp()更改为: private SessionFactory sessionFactory […]

如何在Servlet-Applet场景中进行Unittests

我应该如何为Java / Tomcat Servlet-Applet应用程序创建和运行unit testing?

无法使用mockito模拟私有方法

我试图用power mockto模拟一个私有方法,阅读这篇文章后我得到了一些想法,我遵循相同的结构: 例 这是我的class级: public class test(){ private long verifyMarketEligibilityAndGetOfferDeliveryCalendar(long id) { some lins of code for connectiong to db } public long createOffer(long id){ return verifyMarketEligibilityAndGetOfferDeliveryCalendar(id); } } 这是我的模拟测试: test classUnderTest = PowerMockito.spy(new test()); PowerMockito.doReturn(10).when(classUnderTest, “verifyMarketEligibilityAndGetOfferDeliveryCalendar”, 10l); classUnderTest.createOffer(10); 现在我希望在调用createoffer之后,verifyMarketEligibilityAndGetOfferDeliveryCalendar不会调用,而是返回10个返回但由于某种原因,程序开始执行verifyMarketEligibilityAndGetOfferDeliveryCalendar类,从而执行db相关代码。 有人可以帮忙吗?

JUnit:运行一个具有不同配置的测试

我有2种测试方法,我需要使用不同的配置运行它们 myTest() { ….. ….. } @Test myTest_c1() { setConf1(); myTest(); } @Test myTest_c2() { setConf2(); myTest(); } //—————— nextTest() { ….. ….. } @Test nextTest_c1() { setConf1(); nextTest(); } @Test nextTest_c2() { setConf2(); nextTest(); } 我不能从一个配置运行它们(如下面的代码),因为我需要单独的方法来执行tosca。 @Test tests_c1() { setConf1(); myTest() nextTest(); } 我不想写那两个方法来运行每个测试,我该如何解决这个问题? 首先我想写自定义注释 @Test @RunWithBothConf myTest() { …. } 但也许还有其他解决方案吗?

平衡设计原则:unit testing

我正在写一个Bananagrams的模拟。 目前,我有一个GameMaster类,可以维护常见的碎片集合。 deal(Player)方法向该玩家交易一定数量的棋子。 我想为此编写unit testing。 但是,此时,我没有getter,因此无法检查对象的状态。 为什么不添加吸气剂? 我不想仅为测试添加代码到公共接口。 目前,没有其他理由公开这些function。 我该怎么办? 无论如何添加getter,混乱公共API(或希望将来需要它们)? 放弃unit testing? (听起来不错。) 或者,这是否表明我的公共界面存在缺陷?

使用AnnotationProcessor构建Maven,解析src / main / java中的文件,并生成源代码到generate-test-sources / test-annotations

我正在使用maven 3.0.3和Java7。 我有一个AnnotationProcessor,它应该解析src/main/java ( 不是 src/test/java )中带注释的java文件,并为JUnit-Tests生成Helper-Classes。 这些Helper-Class应该存储在target/generated-test-sources/test-annotations因为它们使用的库只在测试范围内可用。 (注意:只要这个依赖关系不在测试范围内,一切都可以正常工作,但是编译就会失败。它肯定只在测试范围/unit testing和编译测试类时才需要。) 我试了好几个配置而没有任何运气: 我将maven-compiler-plugin配置为在compile:compile期间使用AnnotationProcessor。 生成的HelperClass将存储在generated-sources/annotations 。 不在 generated-test-sources/test-annotations中。 结果是,不会使用Test-Scoped依赖项。 由于编译错误“无法找到符号”,构建失败。 失败 我使用了上面的配置并重新定义了generatedSourcesDirectory: ${project.build.directory}/generated-test-sources/test-annotations 生成的类将按预期存储在generated-test-sources/test-annotations中,但构建仍然失败,因为它尝试按上述方式编译该文件并错过了测试范围的依赖项。 失败 我尝试使用上面的配置并排除**/generated-test-sources/test-annotations/**/*.java以防止编译器在此阶段进行编译: **/generated-test-sources/test-annotations/**/*.java 没运气。 与上面相同的编译器错误。 失败 我将maven-compiler-plugin配置为在test-compile:testCompile期间使用AnnotationProcessor test-compile:testCompile 。 HelperClass理论上可能是在generated-test-sources/test-annotations中generated-test-sources/test-annotations ,但是AnnotationProcessor不会偶然发现位于src/main/java的带注释的类,而不是src/test/java中的AFAIK test-compile:testCompile期间test-compile:testCompile范围test-compile:testCompile 。 因此,找不到Annotated类,HelperClass将不会生成,因此不能存储在generated-test-sources 。 失败 试图在compile:testCompile期间运行compile:testCompile和test-compile:compile ,这在两种情况下都导致src / main / java的类尚未编译 – 因此编译错误。 失败 我真正想做的是: 配置编译器在compile:compile期间使用AnnotationProcessor compile:compile生成我的HelperClass到${project.build.directory}/generated-test-sources/test-annotations 但不要让maven编译它 然后在test-compile:testCompile期间test-compile:testCompile 。 我没有这样做。 我不确定我是否缺少重要的maven基础知识(概念),或者排除配置是否存在问题,或者不管它是什么。 […]

TestFx入门

我在使用Oracle的JavaFx HelloWorld应用程序使用TestFx时遇到了一些麻烦: public class HelloWorld extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle(“Hello World!”); Button btn = new Button(); btn.setText(“Say ‘Hello World'”); btn.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { System.out.println(“Hello World!”); } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); […]

JUnit:具有私有字段的测试构建器

我是初学者,我在类的构造函数中遇到了JUnit测试的问题。 我想测试的类名为IntSortedArray,如下所示: public class IntSortedArray { private int[] elements; private int size; public IntSortedArray() { this.elements = new int[16]; this.size = 0; } public IntSortedArray(int initialCapacity) throws IllegalArgumentException { if(initialCapacity < 0) { throw new IllegalArgumentException("Error – You can't create an array of negative length."); } else { elements = new int[initialCapacity]; size = 0; […]

是否可以在Intellij Idea中更改junit4 test的默认类名模板?

我们的测试类有一个名称约定: 对于unit testing:TestMyCoolClazz 对于集成测试:ITestMyCoolClazz 但每当Intellij Idea提示“创建测试”对话框窗口时,类名始终为MyCoolClazzTest。 所以基本上它在类和名称上添加了一个单词’Test’。 那可以改变它吗?

如何在使用JPA2时对EJB进行unit testing?

您将如何对使用JPA的EJB进行unit testing? 例如,如果我有一个Order实体和OrderEJB,它应该计算一个订单的总数(如下定义),我将如何在不触及数据库的情况下对EJB进行unit testing? 另外,您将如何定义实体的值,以便断言预期的计算? 以下是一些示例代码… @Entity public class Order { @Id private long OrderId; private LineItem[] items; } 和订单EJB @Stateless public class OrderEJB { EntityManager em; public double calculateOrderTotal(long orderId) { .. } } 如果我无法触摸数据库,您将如何进行单位测试calculateOrderTotal方法? 我不想实现DAO,因为我试图摆脱这种方法。 谢谢你的帮助。