简体   繁体   English

在Eclipse中的测试包中添加了一个junit测试文件,但该文件无法运行

[英]Added a junit test file to the test package in eclipse but it will not run

I am using JUnit4 in an Eclipse IDE. 我在Eclipse IDE中使用JUnit4。 I have one test file with 7 tests that run fine by selecting Run As JUnit . 我有一个包含7个测试的测试文件,通过选择Run As JUnit可以正常运行 I added another file for another set of tests. 我为另一个测试集添加了另一个文件。 I have 1 test in the file. 我的文件中有1个测试。 I believe I created the test correctly... This the file / test 我相信我正确地创建了测试...这个文件/测试

@RunWith(MockitoJUnitRunner.class)
public class CloseSummaryOrCloseTrailerResponseTest {

    @InjectMocks
    XMLTransaction xmlTransaction;

    @Before
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void whenCloseSummaryResponseNoErrorExpectCorrectXmlMsgProduced ()
{
..code to run the test
}
}

When I select the file and chose 'Run As Junit' nothing happens. 当我选择文件并选择“ Run As Junit”时,什么也没有发生。 What am I missing? 我想念什么?

UPDATE Sorry for the incomplete information... 更新抱歉,信息不完整...

The project tree is as follows: src/java/...source files src/test/com/javaserver/transaction/RequestTest.java /com/javaserver/transaction/ResponseTest.java 项目树如下:src / java / ...源文件src / test / com / javaserver / transaction / RequestTest.java /com/javaserver/transaction/ResponseTest.java

I can run the RequestTest file and all tests pass. 我可以运行RequestTest文件,所有测试都通过。 When I try to run the ResponseTest file, there was no output initially. 当我尝试运行ResponseTest文件时,最初没有任何输出。 I restarted Eclipse and when I run the response test, I get the error: 我重新启动了Eclipse,当我运行响应测试时,出现了错误:

org.mockito.exceptions.base.MockitoException: Cannot instantiate @InjectMocks field named 'xmlTransaction' of type 'class... org.mockito.exceptions.base.MockitoException:无法实例化类型为“ class”的名为“ xmlTransaction”的@InjectMocks字段...

I imported the XMLTransaction class. 我导入了XMLTransaction类。 Yet it cannot be instantiated. 但是它不能被实例化。 I don't have a main method. 我没有主要方法。 I thought by adding @RunWith(MockitoJUnitRunner.class) it runs the class. 我认为通过添加@RunWith(MockitoJUnitRunner.class)可以运行该类。 It runs the other class file. 它运行另一个类文件。

UPDATE It looks like the object that I want to mock needs a 1 argument constructor. 更新看起来我要模拟的对象需要一个1参数构造函数。 The constructor would look like this: 构造函数如下所示:

XMLTransaction xmlTrans = new XMLTransaction(URL)

The URL is just a text string and not an actual URL. 该URL仅是文本字符串,而不是实际的URL。 But how do I instantiate the object? 但是,如何实例化对象? If I put it under the @InjectMocks, I get the compile error: 如果将其放在@InjectMocks下,则会出现编译错误:

Default constructor cannot handle exception type Exception thrown by implicit super constructor. 默认构造函数无法处理由隐式超级构造函数引发的异常类型。 Must define an explicit constructor 必须定义一个显式构造函数

UPDATE I need to use a PropertyManager class in order to create the XMLTransaction class. 更新我需要使用PropertyManager类来创建XMLTransaction类。 I use this code under the @InjectMocks to do that: 我在@InjectMocks下使用以下代码来做到这一点:

String wlUrl = PropertyManager.getInstance().getProperty(URL);

But then I get the error: Cannot handle Property exception. 但是然后我得到了错误:无法处理属性异常。

UPDATE I tried replacing @MockInjects to @Mock and used the init() to create the class: 更新我尝试将@MockInjects替换为@Mock并使用init()创建类:

 public final static String URL="WL_APPSERVER";  
    @Mock
    XMLTransaction xmlTransaction;  
    @Before
    public void initMocks() throws Exception {
        XMLTransaction xmlTransaction = new XMLTransaction(URL);
    }

I get the error: Mockito cannot mock this class: class 我收到错误:Mockito无法模拟此类:class

com.fedex.ground.tms.javaserver.dock.transaction.XMLTransaction. com.fedex.ground.tms.javaserver.dock.transaction.XMLTransaction。 Mockito can only mock non-private & non-final classes. Mockito只能模拟非私有和非最终类。 Underlying exception : java.lang.IllegalArgumentException: Could not create type 底层异常:java.lang.IllegalArgumentException:无法创建类型

If I add a default constructor without an argument, I get the same error. 如果添加不带参数的默认构造函数,则会出现相同的错误。 This is a public class and not final. 这是公开课,不是最终课程。

UPDATE I solved the problem above by adding an instance of the PropertyManager. 更新我通过添加PropertyManager的实例解决了上述问题。 This object needs a property file. 该对象需要一个属性文件。
The error now is that it can't find the property file. 现在的错误是它找不到属性文件。 I have this now. 我现在有这个。

@RunWith(MockitoJUnitRunner.class)
public class CloseSummaryOrCloseTrailerResponseTest {

     public final static String URL="WL_APPSERVER";
     public final static String PROP_FILE = "src/config/tms20.properties";

    @Mock
    XMLTransaction xmlTransaction; 

    @Mock
    PropertyManager propertyManager;

    @Before
    public void initMocks() throws Exception {
        MockitoAnnotations.initMocks(this);
        Properties p = new Properties();
        p.load(new FileReader(new File(PROP_FILE)));
        propertyManager.getInstance(PROP_FILE);
        XMLTransaction xmlTransaction = new XMLTransaction(URL);
    }

What do I need to identify the property file? 我需要识别什么属性文件?

Place your Tests in hierarchy like this: 将测试按如下所示放置在层次结构中:

project
    src
        main
             java      // source files
             resources // xml, properties etc
        test
             java      // source files
             resources // xml, properties etc

Code must be something like this: 代码必须是这样的:

 @RunWith(MockitoJUnitRunner.class)
 public class ExampleTest {

     @Mock
     private List list;

     @Test
     public void shouldDoSomething() {
         list.add(100);
     }
 }

https://static.javadoc.io/org.mockito/mockito-core/2.6.8/org/mockito/junit/MockitoJUnitRunner.html https://static.javadoc.io/org.mockito/mockito-core/2.6.8/org/mockito/junit/MockitoJUnitRunner.html

The problem was that the constructor was calling a private method to initialize the class. 问题在于构造函数正在调用私有方法来初始化类。 Since Mockito can't deal with private methods, I solved the problem by using PowerMock. 由于Mockito无法处理私有方法,因此我通过使用PowerMock解决了该问题。

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

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