I am using JUnit4 in an Eclipse IDE. I have one test file with 7 tests that run fine by selecting Run As JUnit . I added another file for another set of tests. I have 1 test in the file. 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. 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
I can run the RequestTest file and all tests pass. When I try to run the ResponseTest file, there was no output initially. I restarted Eclipse and when I run the response test, I get the error:
org.mockito.exceptions.base.MockitoException: Cannot instantiate @InjectMocks field named 'xmlTransaction' of type 'class...
I imported the XMLTransaction class. Yet it cannot be instantiated. I don't have a main method. I thought by adding @RunWith(MockitoJUnitRunner.class) it runs the class. It runs the other class file.
UPDATE It looks like the object that I want to mock needs a 1 argument constructor. The constructor would look like this:
XMLTransaction xmlTrans = new XMLTransaction(URL)
The URL is just a text string and not an actual URL. But how do I instantiate the object? If I put it under the @InjectMocks, I get the compile error:
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. I use this code under the @InjectMocks to do that:
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:
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
com.fedex.ground.tms.javaserver.dock.transaction.XMLTransaction. Mockito can only mock non-private & non-final classes. Underlying exception : java.lang.IllegalArgumentException: Could not create type
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. 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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.