Tag: junit

我如何知道代码是否在JUnittesting中运行?

在我的代码中,我只需要在JUnittesting中运行某些修复。 我如何知道代码是否在JUnittesting中运行? 有没有像JUnit.isRunning()== true?

PowerMocktesting – 设置类的静态字段

我很难find一种方法来设置一个类的静态字段。 基本上是这样的: public class Foo{ // … private static B b = null; } B是另一类。 除了setInternalStateFromContext()之外,还有什么办法可以在PowerMock中做到这一点吗? 使用上下文类方法似乎有点矫枉过正设置一个领域。 谢谢。

针对Java EE 6 API进行testing

我为JAX-RS编写了一个补充,并将Java EE 6 API作为Maven依赖包含在内。 <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> 然后我有一个小testing用例: @Test public void testIsWriteable() { class SpecialViewable extends Viewable { public SpecialViewable() { super("test"); } } FreeMarkerViewProcessor processor = new FreeMarkerViewProcessor(null); assertTrue(processor.isWriteable(SpecialViewable.class, null, null, MediaType.WILDCARD_TYPE)); } 但是我得到一个错误: java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/ws/rs/core/MediaType … […]

是否有可能在Eclipse中运行多个包中的JUnittesting?

是否可以同时运行多个包的JUnittesting,而无需手动创buildtesting套件。 例如,如果我有层次结构: code.branchone code.branchone.aaa code.branchone.bbb code.branchtwo code.branchtwo.aaa code.branchtwo.bbb 是否有可能: 运行code.branchone和后代包中的所有testing 运行所有的testing,比如说code.branchone.aaa和code.branchtwo.bbb 我看到手动创buildtesting套件的问题是,当新的testing出现时,您可能会忘记添加它们。

未find类:IntelliJ中的空testing套件

我只是在我的大学开始了计算机科学课程,而且我在IntelliJ上遇到了一些问题。 当我尝试运行unit testing时,我收到消息 Process finished with exit code 1 Class not found: "edu.macalester.comp124.hw0.AreaTest"Empty test suite. 我还在屏幕左侧看到一条标题为“未findtesting”的消息。 我的testing代码在这里: package edu.macalester.comp124.hw0; import org.junit.Test; import static org.junit.Assert.*; public class AreaTest { @Test public void testSquare() { assertEquals(Area.getSquareArea(3.0), 9.0, 0.001); } @Test public void testCircle() { assertEquals(Area.getCircleArea(3.0), 28.2743, 0.001); } } 我的项目代码在这里: package edu.macalester.comp124.hw0; import java.lang.Math; public class Area […]

使用MongoDB进行unit testing

我select的数据库是MongoDB。 我正在编写一个数据层API来从客户端应用程序中抽象实现细节 – 也就是说,我基本上提供了一个单独的公共接口(一个充当IDL的对象)。 我正在testing我的逻辑,因为我以TDD方式进行testing。 在每个unit testing之前,调用@Before方法来创build数据库单例,在此之后,当testing完成时,将调用@After方法来删除数据库。 这有助于提高unit testing的独立性。 几乎所有的unit testing,即执行上下文查询 ,都需要某种插入逻辑才能发生。 我的公共接口提供了一个插入方法 – 但是,使用这种方法作为每个unit testing的前驱逻辑似乎是不正确的。 真的,我需要一些嘲弄的机制,但是我还没有太多的嘲讽框架的经验,似乎Google没有返回一个可以用于MongoDB的嘲笑框架。 别人在这些情况下做什么? 也就是说,人们如何testing与数据库交互的代码呢? 此外,我的公共接口连接到一个外部configuration文件中定义的数据库 – 这似乎是不正确的使用这个连接,我的unit testing – 再次,这种情况,将受益于某种嘲笑?

如何为Spring Boot Controller端点编写unit testing

我有一个以下样本Spring Boot应用程序 引导主类 @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } 调节器 @RestController @EnableAutoConfiguration public class HelloWorld { @RequestMapping("/") String gethelloWorld() { return "Hello World!"; } } 为控制器编写unit testing最简单的方法是什么? 我尝试了以下,但它抱怨未能自动assemblyWebApplicationContext @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = DemoApplication.class) public class DemoApplicationTests { final String BASE_URL = "http://localhost:8080/"; @Autowired private WebApplicationContext wac; private MockMvc […]

TypeNotPresentExceptionProxy

从Surefire 2.6升级到Surefire 2.13时,在运行我的unit testing时,我得到一个TypeNotPresentExceptionProxy 。 java.lang.ArrayStoreException: sun.reflect.annotation.TypeNotPresentExceptionProxy at sun.reflect.annotation.AnnotationParser.parseClassArray(AnnotationParser.java:653) at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:460) at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:286) at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:222) at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69) at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52) at java.lang.Class.initAnnotationsIfNecessary(Class.java:3070) at java.lang.Class.getAnnotation(Class.java:3029) at org.apache.maven.surefire.common.junit4.JUnit4TestChecker.isValidJUnit4Test(JUnit4TestChecker.java:64) 在JUnit4TestChecker ,第64行看起来像这样: Annotation runWithAnnotation = testClass.getAnnotation( runWith ); 所以Surefire会检查@RunWith注解来确保它的types是有效的。 我们的testing使用Spring,因此@RunWith在我们的testing类中看起来像这样: @RunWith(SpringJUnit4ClassRunner.class) 似乎Surefire没有findSpringJUnit4ClassRunner类。 我不确定为什么在Surefire 2.6下,testing运行良好。 有任何想法吗?

Hamcrest库Matcher和Hamcrest核心CoreMatchers之间的区别

它看起来像hamcrest org.hamcrest.Matchers类是非常相似org.hamcrest.CoreMatchers (虽然它看起来像Matchers有更多)。 为什么我会select使用CoreMatchers (除了看起来类似于稍小),为什么这两个类如此相似呢?

从JUnittesting用例中找不到资源文件

概要 我的JUnittesting没有在执行过程中find他们需要的文件。 我正在使用Maven进行依赖pipe理和编译。 细节 testing用例所需的所有文件位于: src/test/resources 。 例如, src/test/resources/resourceFile.txt 。 要访问资源我使用下面的代码: URL url = getClass().getResource("/resourceFile.txt").getFile(); File file = new File(url); 但是,然后file.exists()返回false 。 而我得到的错误是: Tests in error: myJUnitTestCase(tests.MyJUnitTestClass): /home/me/workspace/Project%20Name/target/test-classes/resourceFile.txt (No such file or directory) 请注意,以下给出了相同的错误(注意删除/前缀): URL url = getClass().getClassLoader().getResource("resourceFile.txt").getFile(); File file = new File(url); 似乎来自src/test/resources的文件没有被复制到target/test-classes 。 有任何想法吗? 以下问题没有帮助 为什么我无法在Maven中使用Junittesting来访问src / test / resources? 在JUnit @BeforeClass中加载属性文件 如何处理Junit中的testing数据? […]