标签: junit4

如何理解@Test和@BeforeClass?

我从JUnit开始并且不理解注释@Test@BeforeClass.
我有以下代码:

public class Toto { 
@BeforeClass
    public static void setupOnce() {
        final Thread thread = new Thread() {
            public void run() {
                Main.main(new String[]{"-arg1", "arg2"});
            }
        };
        try {
            thread.start();
        } catch (Exception ex) {
        }
    }
Run Code Online (Sandbox Code Playgroud)

为什么@BeforeClasssetupOnce()在这种情况下,什么是和线程?
我们应该@Test在每次Java测试之前添加吗?
所以如果我有30个Java测试,我应该@Test public void test()在每个Java文件中吗?

java junit junit4

2
推荐指数
1
解决办法
9724
查看次数

从java类运行测试用例

我说了10个测试用例,我想从java类运行它们,我只能在运行时获取Junit的类名.是否可以这样做.

请帮我.

java junit4

2
推荐指数
1
解决办法
290
查看次数

如何从命令行在JUnit中运行测试用例?

我正在尝试使用此命令从命令行运行JUnit测试用例:

F:\>java org.junit.runner.JUnitCore org.junit4.9b2.junit.SimpleTest
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore

Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: org.junit.runner.JUnitCore.  Program will exit.
Run Code Online (Sandbox Code Playgroud)

问题是什么?

java junit junit4

2
推荐指数
1
解决办法
2万
查看次数

通过build.gradle传递标签,而不是通过RunCukesSpec

当我以下列方式传递标签时,它完美地工作.

package features

import org.junit.runner.RunWith
import cucumber.junit.Cucumber
import geb.junit4.GebReportingTest

@RunWith(Cucumber.class)
@Cucumber.Options(format = ["pretty", "html:build/cucumber", "json-pretty:build/cucumber-report.json"])
                            ,tags = ["@login_neg"])

class RunCukesSpec extends GebReportingTest {}
Run Code Online (Sandbox Code Playgroud)

但我的目标是通过build.gradle&配置相同的东西,如果它成功,然后通过命令行.我尝试在下面作为初始步骤,并希望通过gradle test在命令行中运行来获得预期的结果.

test {
    testLogging.showStandardStreams = true

    args = ['--tags', '@login_neg',
            '--format', 'html:build/cucumber',
            '--format', 'json-pretty:build/cucumber-report.json',
            '--format', 'pretty']

}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,所有标签都在运行.

试过这个.但没有运气 gradle test -DCucumber.Options="--tags @login_neg"

版本:

------------------------------------------------------------
Gradle 1.9
------------------------------------------------------------

Build time:   2013-11-19 08:20:02 UTC
Build number: none
Revision:     7970ec3503b4f5767ee1c1c69f8b4186c4763e3d

Groovy:       1.8.6
Ant:          Apache Ant(TM) version 1.9.2 compiled on July 8 2013
Ivy:          2.2.0 …
Run Code Online (Sandbox Code Playgroud)

intellij-idea cucumber junit4 gradle

2
推荐指数
1
解决办法
2421
查看次数

JdbcTemplate类型的方法query(String,ResultSetExtractor <T>)不适用于参数(String,BeanPropertyRowMapper)

我有一个奇怪的编译问题。我无法解决此问题。同样的代码在另一个项目中也能正常工作

org.mockito.Mockito.when(jdbcTemplate.query(org.mockito.Matchers.anyString(), 
    org.mockito.Matchers.any(BeanPropertyRowMapper.class))).thenReturn(SOMELIST);
Run Code Online (Sandbox Code Playgroud)

我收到错误消息

The method query(String, ResultSetExtractor<T>) in the type JdbcTemplate is not applicable for the arguments (String, BeanPropertyRowMapper)

但是,当我这样做时,不会出现任何错误。但是我没想到这一点。

BeanPropertyRowMapper<MyClass> mapper = 
    new BeanPropertyRowMapper<MyClass>(MyClass.class);
org.mockito.Mockito.when(jdbcTemplate.query(org.mockito.Matchers.anyString(), 
    mapper)).thenReturn(SOMELIST);
Run Code Online (Sandbox Code Playgroud)

我不确定这是否是Eclipse问题。感谢您的帮助。

java spring mocking junit4 mockito

2
推荐指数
1
解决办法
6162
查看次数

Serenity BDD测试用例未在Chrome浏览器上运行

当我在步骤定义文件中使用下面的注释时,我的Serenity BDD测试用例在firefox上正常运行:

@Managed
public WebDriver driver;
Run Code Online (Sandbox Code Playgroud)

我想在chrome浏览器上运行相同的测试用例.所以,修改如下:

@Managed(driver = "chrome")
 WebDriver driver;
Run Code Online (Sandbox Code Playgroud)

还试过下面一个:

@Managed(driver = "chrome")
 ChromeDriver driver;
Run Code Online (Sandbox Code Playgroud)

在上述两种情况下,我的测试用例仍然通过打开Firefox而不是Chrome运行.我按照Serenity BDD指南中提到的确切步骤进行操作.您能否通过打开Chrome来帮助我了解如何执行Serenity BDD测试脚本.在此先感谢您的帮助.

bdd junit4 cucumber-jvm cucumber-junit cucumber-java

2
推荐指数
2
解决办法
7398
查看次数

如何在运行测试之前使JUnit4"等待"异步作业完成

我正在尝试为我的Android应用程序编写一个与云服务通信的测试.从理论上讲,测试的流程应该是这样的:

  1. 在工作线程中向服务器发送请求
  2. 等待服务器的响应
  3. 检查服务器返回的响应

我正在尝试使用Espresso的IdlingResource类来完成它,但它没有按预期工作.这是我到目前为止所拥有的

我的测试:

@RunWith(AndroidJUnit4.class)
public class CloudManagerTest {

FirebaseOperationIdlingResource mIdlingResource;

@Before
public void setup() {
    mIdlingResource = new FirebaseOperationIdlingResource();
    Espresso.registerIdlingResources(mIdlingResource);
}

@Test
public void testAsyncOperation() {
    Cloud.CLOUD_MANAGER.getDatabase().getCategories(new OperationResult<List<Category>>() {
        @Override
        public void onResult(boolean success, List<Category> result) {
            mIdlingResource.onOperationEnded();
            assertTrue(success);
            assertNotNull(result);
        }
    });
    mIdlingResource.onOperationStarted();
}
}
Run Code Online (Sandbox Code Playgroud)

FirebaseOperationIdlingResource

public class FirebaseOperationIdlingResource implements IdlingResource {

private boolean idleNow = true;
private ResourceCallback callback;


@Override
public String getName() {
    return String.valueOf(System.currentTimeMillis());
}

public void onOperationStarted() {
    idleNow = false; …
Run Code Online (Sandbox Code Playgroud)

java multithreading android junit4 android-espresso

2
推荐指数
2
解决办法
2537
查看次数

spring boot build package org.junit不存在

我在构建环境中构建Spring Boot应用程序时收到以下编译错误.但是,在我的机器上运行Junit测试或构建(打包)它时,我没有看到任何错误 -

src/main/java/com/abc/tests/AbcTest.java:包org.junit不存在

这是我的pom.xml -

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath />
</parent>
<dependencies>........
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

以下是我试过的步骤没有成功 -
1.在依赖部分中明确依赖junit 4.12以及[4.12].

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>  
</dependency> 
Run Code Online (Sandbox Code Playgroud)
  1. 添加带有和不带2.18.1版插件的maven-surefire-plugin
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <junitArtifactName> junit:junit:4.12</junitArtifactName>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以强制构建过程在pom.xml中使用4.12 junit版本而不是选择像3.8这样的旧版本?

谢谢

java spring unit-testing junit4 spring-boot

2
推荐指数
1
解决办法
2705
查看次数

哪个更好,Java反射或扩展类测试

有一个Java类,我正在尝试编写一个JUnit测试.

使用JaCoCo监控代码覆盖率.因此,需要从测试类调用私有方法.

使用Java反射从测试类调用main方法和私有方法.

我的问题是更好地扩展测试类中的主类(示例)或使用java反射来访问私有方法?目前我正在使用如下反射.

在测试类中扩展Java类是一个好习惯吗?
因此,我是新编写测试类的问题.我很感激你的帮助.

public class Example { 
    public static void main(String[] s) {
        method1();
        method2();
        ..........
    }

    private Employee method1(String str) {
        .........
    }

    private Employee method2(String str1) {
        .........
    }
}

public class ExampleTest { 
    @InjectMocks
    Example example;
    .....

    @Before
    public void setUp() {
        ........
    }
    @Test
    public void testMain() {    
        try {
            String[] addresses = new String[]{};
            Example loadSpy = spy(example);
            loadSpy.main(addresses);            
            assertTrue(Boolean.TRUE);           
        } catch (.. e) {
            .......
        }
        assertTrue(true);
    }
    @Test
    public void …
Run Code Online (Sandbox Code Playgroud)

java junit4

2
推荐指数
1
解决办法
99
查看次数

Writing a JUnit test case for a constructor

I have an assignment for school that involves learning how to use JUnit4. I need to make sure this constructor works properly, however, I am unsure of how to do so. There is a getter for the balance, so testing whether or not the constructor works with this is pretty easy, however, I don't know how to write a test case that involves user and pass as these do not have getters. Do I write a case for each individual …

java junit4

2
推荐指数
1
解决办法
83
查看次数