我必须为现有代码编写JUnit测试用例,而我没有自由/机会来重写实际代码。在许多此类中,这些方法在内部捕获异常并仅记录该异常。并且异常不是基于可以操纵以尝试引发异常的任何参数,而是由方法中的代码引发的-例如-该方法可能会打开文件并捕获IOException并将其记录下来。基本上,许多方法只会吃掉所有可能的异常-我知道这是非常不好的做法,但是在这个时候,我们必须继续。
是否可以针对此类情况进行代码覆盖。由于这些类有很多这样的构造,因此它使该类的代码覆盖率非常低,我必须使覆盖率至少达到75%
我尝试了从不同示例中发现的不同事物,但是没有一个起作用。
更新
添加需要添加JUnit测试的代码示例。
public StringBuilder getTxtFiles(InputStream in) {
StringBuilder out = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
try {
while ((line = reader.readLine()) != null) {
out.append(line);
}
} catch (IOException e) {
System.out.println("Error Occurred ");
e.printStackTrace();
}
return out;
}
这只是一个例子。存在具有“新”的对象创建的不同种类,并且引发了不同的异常。我需要的是编写一个JUnit测试的示例,该测试将覆盖这种类型的catch子句。
更新2-另一个示例
要测试的类
package com.test.model;
import com.datastax.driver.core.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.annotation.Transient;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.mapping.Table;
import javax.persistence.Entity;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
@Entity
@Table("test_master")
public class TestMaster implements Serializable {
private static final Logger LOGGER = LoggerFactory.getLogger(TestMaster.class);
private static final long serialVersionUID = 1L;
@PrimaryKeyColumn(name = "test_number", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private String testNumber;
@Column(value = "test_requested_date")
private LocalDate testRequestedDate;
@Column(value = "transfer_date")
private LocalDate transferDate;
public LocalDate getTestRequestedDate() {
return testRequestedDate;
}
public void setTestRequestedDate(LocalDate testRequestedDate) {
this.testRequestedDate = testRequestedDate;
}
public void setOtherAppTestRequestedDate(String otherAppTestRequestedDate) {
this.otherAppTestRequestedDate = otherAppTestRequestedDate;
}
public void setOtherAppTestTransferdate(String otherAppTestTransferdate) {
this.otherAppTestTransferdate = otherAppTestTransferdate;
}
@Transient
private String otherAppTestRequestedDate;
@Transient
private String otherAppTestTransferdate;
public static Date subtractDays(Date date, int days) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(Calendar.DATE, -days);
return cal.getTime();
}
public String getTestNumber() {
return testNumber;
}
public void setTestNumber(String loanNumber) {
this.testNumber = testNumber;
}
public String getOtherAppTestRequestedDate() {
String ourformat ="";
LOGGER.info("getTestRequestedDate()--->"+getTestRequestedDate());
try {
if (getTestRequestedDate() != null && getTestRequestedDate().getMillisSinceEpoch() > 0) {
Date d = new Date(getTestRequestedDate().getMillisSinceEpoch());
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
ourformat = formatter.format(d.getTime());
System.out.println("getOtherAppTestRequestedDate()--->" + ourformat);
}
}catch(Exception e){
e.printStackTrace();
}
return ourformat;
}
public LocalDate getTransferDate() {
return transferDate;
}
public void setTransferDate(LocalDate transferDate) {
this.transferDate = transferDate;
}
public String getOtherAppTestTransferdate(){
String ourformat = "";
LOGGER.info("TranferDate"+getTransferDate());
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat stringFormatter = new SimpleDateFormat("yyyyMMdd");
try {
if (getTransferDate() != null && getTransferDate().getMillisSinceEpoch() > 0) {
ourformat= getTransferDate().toString();
Date date1=format.parse(ourformat);
ourformat=stringFormatter.format(date1);
System.out.println("printing getIdeaTransferdate" + ourformat);
}
}catch(Exception e){
e.printStackTrace();
}
return ourformat;
}
}
示例JUnit测试类
package com.test.model;
import com.datastax.driver.core.LocalDate;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@PrepareForTest({SimpleDateFormat.class})
@PowerMockIgnore("javax.management.*")
public class TestMasterTest {
@InjectMocks
TestMaster model;
@Test
public void testGetIdeaTransferdateException() throws ParseException {
SimpleDateFormat sdfMock = PowerMockito.mock(SimpleDateFormat.class);
PowerMockito.when(sdfMock.parse(anyString())).thenThrow(new ParseException("Exception cause", 1));
model.setTransferDate(LocalDate.fromYearMonthDay(2018, 1, 3));
when(model.getTransferDate()).thenReturn(null);
// Tried this with anyString() instead of null
// also tried with "when" clause above the model.setTransfer clause
Assert.assertEquals("20180103",model.getOtherAppTestTransferdate());
Assert.assertNotNull("Expected date : 20180103",model.getOtherAppTestTransferdate());
}
}
这是我得到的错误
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Or 'a static method call on a prepared class`
For example:
@PrepareForTest( { StaticService.class })
TestClass{
public void testMethod(){
PowerMockito.mockStatic(StaticService.class);
when(StaticService.say()).thenReturn(expected);
}
}
Also, this error might show up because:
1. inside when() you don't call method on mock but on some other object.
2. inside when() you don't call static method, but class has not been prepared.
at org.powermock.api.extension.reporter.MockingFrameworkReporterFactoryImpl$PowerMockitoReporter.missingMethodInvocation(MockingFrameworkReporterFactoryImpl.java:66)
at com.test.model.TestMasterTest.testGetIdeaTransferdateException(TestMasterTest.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$2.call(DelegatingPowerMockRunner.java:148)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$2.call(DelegatingPowerMockRunner.java:140)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.withContextClassLoader(DelegatingPowerMockRunner.java:131)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.run(DelegatingPowerMockRunner.java:140)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
2018-09-13 08:29:03.368-0400 INFO o.s.c.support.GenericApplicationContext Closing org.springframework.context.support.GenericApplicationContext@61c9c3fd: startup date [Thu Sep 13 08:29:00 EDT 2018]; root of context hierarchy
我该如何实现?
谢谢