differences between 2 JUnit Assert classes

The JUnit framework contains 2 Assert classes (in different packages, obviously) and the methods on each appear to be very similar. Can anybody explain why this is?

The classes I'm referring to are: junit.framework.Assert and org.junit.Assert.

Asked by: Cherry905 | Posted: 21-01-2022

Answer 1

The old method (of JUnit 3) was to mark the test-classes by extending junit.framework.TestCase. That inherited junit.framework.Assert itself and your test class gained the ability to call the assert methods this way.

Since version 4 of JUnit, the framework uses Annotations for marking tests. So you no longer need to extend TestCase. But that means, the assert methods aren't available. But you can make a static import of the new Assert class. That's why all the assert methods in the new class are static methods. So you can import it this way:

import static org.junit.Assert.*;

After this static import, you can use this methods without prefix.

At the redesign they also moved to the new package org.junit that follows better the normal conventions for package naming.

Answered by: Ada859 | Posted: 22-02-2022

Answer 2

JUnit 3.X: junit.framework.Assert

JUnit 4.X: org.junit.Assert

Prefer the newest one, especially when running JDK5 and higher with annotation support.

Answered by: Julia348 | Posted: 22-02-2022

Answer 3

There is in fact a functional change: org.junit.Assert will complain if you use the two-argument assertEquals() with float or double, while junit.framework.Assert will silently autobox it.

Answered by: Sienna908 | Posted: 22-02-2022

Answer 4

I believe they are refactoring from junit.framework to org.junit and junit.framework.Assert is maintained for backwards compatibility.

Answered by: Brad505 | Posted: 22-02-2022

Answer 5

In Android Studio (and so in IntelliJ too), you can globally exclude junit.framework from auto-import proposal.

You can set the scope between IDE or Project. If you don't have projects which use JUnit 3 you can safely stay with IDE scope.

Setting position:

Preferences -> Editor -> General -> Auto Import

enter image description here

Answered by: Roland286 | Posted: 22-02-2022

Answer 6

I did a rough source code compare and there are no serious changes. A lot of comments were added in org.junit.Assert and some refactorings are done. The only change is the comparison with Arrays. There are some code cleanups, but there's (imho) no functional change.

Answered by: Owen906 | Posted: 22-02-2022

Similar questions

java - What are the differences between local and non static inner classes?

I just want the differences on the basis of how members are accessible from enclosing class or from inner class of each other. In relation to static, final, effectively final members and why?

What are the differences between PHP and Java?

What are the main differences between PHP and Java that someone proficient in PHP but learning Java should know about? Edit: I mean differences in the syntax of the languages, i.e their data types, how they handle arrays & reference variables, and so forth :)

comparison - What are the differences between LLVM and java bytecode?

I dont understand the difference between LLVM and the java (bytecode), what are they? -edit- by 'what are they' i mean the differences between LLVM and java (bytecode) not what are LLVM and java.

tomcat - Java - Differences in AWT from 1.4 to 1.5 ( working on Unix and Windows )

This is with reference to my question: java.awt.HeadlessException Applets not displayed. There is a next question that has come up from the Sys Admins. They are asking - "Why does java1.5 now requires the explicit export DISPLAY in tomcat at all when java1.4 did not?" So what has changed in jav...

java - Differences between Ant and Maven

Closed. This question needs to be more focused. It ...

java - MySQL and SQLite differences in SQL

I'm writing java application that is using both SQLite and MySQL using JDBC. Are there any differences in SQL for those databases? Can I use same queries for both SQLite and MySQL, or is there any db specific stuff, that doesn't work on the other one? As far I've worked only with MySQL, so ...

what are the differences of Inheritance & java Beans?

What are the differences between Inheritance & JavaBeans?

Differences between Java and C# and .NET

Closed. This question is opinion-based. It is not c...

Differences between Java interfaces and Objective-C protocols?

I know Java, and now I'm learning Objective-C. What exactly are the differences between Java interfaces and Objective-C protocols?

What are the differences between BDD frameworks for Java?

java - Major differences between J2EE and C#/.Net when developing Web Services

Having been primarily a .NET guy up until starting a new job recently; I've only done Web-Service development and consumption in C#/.Net. However I'm embarking on the journey to learn the ins and outs of doing it on the J2EE platform, and I'm curious what the major differences are in this specific type of development. Note: I have familiarity with the Java Language at the console/simple interface l...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)