在这里使用JUnit 4.10是我的测试.
package geometry;
import org.junit.Assert.*;
import org.junit.Test;
public class SegmentTest extends junit.framework.TestCase {
@Test
public void SegmentParallelTest() {
Segment a = new Segment(10, 5, 20, 11);
Segment b = new Segment(20, 6, 30, 11);
assertFalse(a.isParallel(b));
}
public static junit.framework.Test suite() {
return new junit.framework.JUnit4TestAdapter(SegmentTest.class);
}
public static void main(String args[]) {
org.junit.runner.JUnitCore.main("geometry.SegmentTest");
}
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下错误消息......
junit.framework.AssertionFailedError: No tests found in geometry.SegmentTest
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:202)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at …Run Code Online (Sandbox Code Playgroud) 我想知道为什么这个断言在JUnit中有效:
assertEquals("[String1, String2, String3]", Arrays.asList("String1", "String2", "String3").toString());
Run Code Online (Sandbox Code Playgroud)
我看不到在List或Collection上的任何地方覆盖toString().
我很高兴它有效,但仍然很好奇.
谢谢,Declan
我正在测试抛出两个不同异常的方法.这是我的标题:
@Test (expected = A8InvalidInputException.class)
public void testGuessCharacter() throws A8InvalidInputException, A8AlreadyGuessedException
{
...
}
正文有两个try/catch块(在SO上搜索会产生一个帖子,说明你是如何测试抛出异常的),每个例外都有一个.在我看来,我应该将其分解为两种测试方法,特别是因为我只能有一个预期的属性.但是,当我这样做时,应该测试A8InvalidInputException的方法需要针对A8AlreadyGuessedException的try/catch,并且应该测试A8AlreadyGuessedException的方法需要针对A8InvalidInputException的try/catch.我不太确定如何编写这个测试.这是我试图测试的方法:
/**
* This method returns whether a specified character exists in the keyPhrase field
* @param guess a character being checked for in the keyPhrase field
* @return returns whether a specified character exists in the keyPhrase field
* @throws A8InvalidInputException if a non-valid character is passed as input to this method
* @throws A8AlreadyGuessedException if a valid character which has already been …Run Code Online (Sandbox Code Playgroud) Mockito.when为下面的代码行获取空指针异常。
when(entityManager.createQuery(any(String.class)).setParameter(any(String.class), any(String.class)).getSingleResult()).thenReturn("2");
Run Code Online (Sandbox Code Playgroud)
试图模拟声明为的实体管理器
@Mock
private EntityManager entityManager;
Run Code Online (Sandbox Code Playgroud)
任何帮助解决这个问题?
完成测试类
@RunWith(MockitoJUnitRunner.class)
public class ASDAOImplTest {
@InjectMocks
ASDAOImpl asdaoImpl=new ASDAOImpl();
@Mock
private EntityManager entityManager;
@Before
public void setUp()
{
ReflectionTestUtils.setField(asdaoImpl,"capLimit", 1);
}
@Test
@Ignore
public void validateCappingTest()
{
when(entityManager.createQuery(any(String.class)).setParameter(any(String.class), any(String.class)).getSingleResult()).thenReturn("2");
asdaoImpl.validateCapping("2");
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个代码,用于以下
import static org.junit.Assert.assertEquals;
System.out.println("obj1 name = " + obj1.getName());
System.out.println("obj1 value = " + (obj1.getvalue() == null ? "null" : "not null"));
System.out.println("obj2 name = " + obj2.getName());
System.out.println("obj2 value = " + (obj2.getvalue() == null ? "null" : "not null"));
assertEquals(obj2, obj1);
Run Code Online (Sandbox Code Playgroud)
产量
obj1 name = DC2
obj1 value = null
obj2 name = DC2
obj2 value = null
java.lang.AssertionError:
Expected :com.gms.contract.myClass.inventory.MyClass@795ce9b5
Actual :com.gms.contract.myClass.inventory.MyClass@280cb0b4
Run Code Online (Sandbox Code Playgroud)
是不是assertEquals假设按值进行比较?它在我看来它比较对象地址.但也许我错了?...
谢谢!
我在函数中有一行代码,它对 as 应用流操作ArrayList:
List<Entity> list = xyz();
long result = list.stream().map(n -> n.getData()).filter(n -> n == 1).count();
Run Code Online (Sandbox Code Playgroud)
现在,当我为此方法编写测试时,我应该这样做:
@Mock
private List<Entity> list;
//inside the test method
when(list.stream().map(any()).filter(any()).count()).thenReturn(someValue);
Run Code Online (Sandbox Code Playgroud)
我想到的是,当我们在代码中调用流操作时,我们基本上是走出类来调用这些函数。由于这是单元测试,我们应该留在我们的模块内。如果我有一些误解,请澄清。如果我们不需要嘲笑List,那为什么呢?
我以为我理解了两者之间的区别@Mock,@MockBean甚至我认为任何被模拟的对象都不会调用真正的方法,尽管当我在测试下运行时,我可以看到篮子已插入到 hsqldb 日志中。所以现在我对为什么@Mock使用时插入篮子而使用时不插入篮子感到有些困惑@MockBean。
INSERT INTO BASKET VALUES(5,'ABCDEFGHIJ','ACTIVE',1,'2019-01-18 12:00:36.066000','2019-01-18 12:00:36.066000')
Run Code Online (Sandbox Code Playgroud)
另一方面,如果我这样做,那么 hsqldb 是干净的。在这两种情况下测试都是成功的。
@MockBean
private BasketRepository basketRepo;
Run Code Online (Sandbox Code Playgroud)
测试班
@RunWith( SpringRunner.class )
@SpringBootTest( )
@ActiveProfiles( "test" )
public class BasketServiceTests
{
@SpyBean
private BasketService basketService;
@Mock
private BasketRepository basketRepo;
@Autowired
private UserAccountRepository userAccountRepo;
@Test
public void createBasketWithSameOrderRef() throws Exception
{
UserAccount customer = userAccountRepo.findById( 1 )
.orElseThrow( () -> new NotFoundException( "Customer not found" ) );
Basket basket = new Basket();
basket.setAudit( new Audit() …Run Code Online (Sandbox Code Playgroud) 我是Junit测试用例世界的新手,我只是想知道如果我开发了一个程序
class MapDemo1
{static final Logger logger = Logger.getLogger(MapDemo1.class);
/**
*
*/
public static void main(String arg[])
{PropertyConfigurator.configure("src/log4j.properties");
logger.info("-->Map");
// Map map=new TreeMap();
Map map=new HashMap();//HashMap key random order.
// System.out.println("Amit".hashCode());
map.put("Amit","Java");
map.put("Amit","Javas");
// map.put("mAit","J2EE");
//map.put("Saral","J2rrrEE");
/*map.put("ty","Spring");
map.put("Anupam","Hibernate");
map.put("Ravi",".Net");
map.put("Saral","Andriod");//same key but different value
map.put("Nitin","PHP");
map.put("hj","Spring1");*/
System.out.println("There are "+map.size()+" elements in the map.");
System.out.println("Content of Map are...");
Set s=map.entrySet();
Iterator itr=s.iterator();
while(itr.hasNext())
{
Map.Entry m=(Map.Entry)itr.next();
System.out.println(m.getKey()+"\t"+m.getValue()+"\t"+ m.hashCode());
}
]
}
Run Code Online (Sandbox Code Playgroud)
现在请告知这个junit测试用例是什么以及如何在seprate类中编写.
假设有一个结果List R和一个预期的List E.
List<String> R = ...
List<String> E = ...
Run Code Online (Sandbox Code Playgroud)
我如何使用JUnit assertTrue(…)来检查R至少包含所有元素E?
我需要帮助为此代码创建JUnit 4测试用例.
public static int computeValue(int x, int y, int z)
{
int value = 0;
if (x == y)
value = x + 1;
else if ((x > y) && (z == 0))
value = y + 2;
else
value = z;
return value;
}
Run Code Online (Sandbox Code Playgroud)
编辑
我想要这样的东西测试if else语句
public class TestingTest {
@Test
public void testComputeValueTCXX() {
}
…
@Test
public void testComputeValueTCXX() {
}
}
Run Code Online (Sandbox Code Playgroud) junit4 ×10
java ×8
junit ×5
unit-testing ×3
mockito ×2
collections ×1
exception ×1
spring-boot ×1
testcase ×1