首页 \ 问答 \ 为什么不叫@Before(Why isn't @Before being called)

为什么不叫@Before(Why isn't @Before being called)

在以下测试类中,我的测试对象未初始化,因为未调用Before。

有任何想法吗?

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CalcDateToolTest extends TestCase 
{
    DatesCalculator calc;

    @Before
    public void initialize() {
        System.out.println("Before");
        calc = new DatesCalculator("15/06/2013", "30/06/2013");
    }

    @Test
    public void testCalcDayDiff() {
        assertEquals(true, calc.getFromDate());
    }

    @After
    public void destroy() {
        calc = null;
    }
}

In the following test class my test object doesn't get initialised because Before isn't being called.

Any ideas?

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CalcDateToolTest extends TestCase 
{
    DatesCalculator calc;

    @Before
    public void initialize() {
        System.out.println("Before");
        calc = new DatesCalculator("15/06/2013", "30/06/2013");
    }

    @Test
    public void testCalcDayDiff() {
        assertEquals(true, calc.getFromDate());
    }

    @After
    public void destroy() {
        calc = null;
    }
}

原文:https://stackoverflow.com/questions/25821456
更新时间:2022-10-14 09:10

最满意答案

Scala到Haskell程序员:

Scala是一个严格和不纯的语言与一流的模块。 新类型被声明为模块(“类”/“特征”,具有微妙的差异)。 模块可以是采用通用量化类型参数的类型构造函数。 模块具有由值,可变变量和函数(称为“方法”)组成的成员,模块被隐式传递给其中称为this变量)。 模块可以具有也可以参数的类型成员。 类型成员是存在量化的,类型参数可以更高。 因为类型可以是第一类值的成员,所以Scala提供称为依赖路径的依赖类型的风格。

一流的功能也是模块。 一个函数是一个名为apply的方法。 一种方法不是一流的,但是提供了一种语法来将一个方法包装在一类函数中。 不幸的是,模块需要所有的类型参数,因此部分应用的一级函数不允许被普遍量化。 更一般地说,Scala完全缺乏一种等级高于1的直接机制,但是可以利用更高种类的参数化模块来模拟秩n类型。

而不是具有全局范围的类型类,Scala可以声明任何给定类型的隐式值。 这包括功能类型,提供隐式转换,因此提供类型扩展。 除了隐式转换,类型扩展由“扩展”机制提供,它允许您在模块之间声明子类型/超类型关系。 该机制可用于模拟代数数据类型,其中可以将超类型视为数据声明左侧的类型,其子类型作为右侧的值构造函数。 这也意味着ADT并不真正关闭。 Scala具有广泛的模式匹配功能,使用具有一流模式的虚拟化模式匹配器。

Scala支持子类型,这大大限制了类型推断。 但是推论的类型随时间的推移有所改善。 支持较高种类的推论。 然而,Scala缺乏有意义的系统,因此没有任何推断,也没有任何形式的统一。 如果引入了一个类型变量,它是一种*除非另有注释。 某些类型,如Any (所有类型的超类型)和Nothing (每种类型的子类型)在技术上都是各种类型,尽管它们不能应用于类型参数。

Haskell到Scala程序员:

Haskell是纯功能语言。 这意味着功能根本不允许有任何副作用。 例如,Haskell程序不会打印到屏幕上,而是返回IO[_]数据类型的值的函数,该数据类型描述IO子系统执行的一系列操作。

而Scala默认是严格的,并为非限制函数参数提供“by-name”注释,Haskell默认使用“by-need”语义进行懒惰,并为严格参数提供注释。

Haskell中的类型推断比Scala更完整,具有完全推断,即使是部分应用的多态类型构造函数。 这意味着类型注释几乎不是必需的。

最近对GHC编译器的扩展允许在Scala中没有等价的高级类型系统功能,例如rank-n类型,类型族和多种类型。

在Haskell中,模块是类型和函数的集合,但模块不是一流实体。 Implicits由类型类提供,但一旦声明为全局范围,并且它们不能像Scala一样显式传递。 给定类型的给定类型的多个实例由新类型机制解析,而在Scala中,这将通过范围界定或通过明确传递实例来解决。

由于Haskell不是“面向对象”,所以没有方法/功能的二分法。 每个函数都是第一类,默认情况下每个函数都是curried(没有Function1,Function2等)。


Scala To a Haskell programmer:

Scala is a strict and impure language with first-class modules. Data types are declared as "classes" or "traits" with subtle differences, and modules or "objects" are values of those types. Scala supports type constructors taking universally quantified type parameters. Objects/classes/traits have members which consist of values, mutable variables, and functions (called "methods", to which the module is implicitly passed as a variable called this). Modules may have type members which can also take parameters. Type members are existentially quantified and type parameters can be higher-kinded. Because types can be members of first-class values, Scala provides a flavour of dependent typing called path-dependent types.

First-class functions are also modules. A function is a module with a method named apply. A method is not first-class, but a syntax is provided to wrap a method in a first-class function. Unfortunately, a module requires all of its type parameters up front, hence a partially applied first-class function is not allowed to be universally quantified. More generally, Scala completely lacks a direct mechanism for types of rank higher than 1, but modules parameterized on higher-kinded types can be exploited to simulate rank-n types.

Instead of type classes with global scope, Scala lets you declare an implicit value of any given type. This includes function types, which provides implicit conversion, and therefore type extension. In addition to implicit conversions, type extension is provided by the "extends" mechanism which lets you declare a subtype/supertype relation among modules. This mechanism can be used to simulate algebraic datatypes where the supertype can be seen as the type on the left-hand side of a data declaration, and its subtypes as the value constructors on the right-hand side. Scala has extensive pattern-matching capabilities using a virtualized pattern matcher with first-class patterns.

Scala supports subtyping, and this limits type inference considerably. But type inference has improved over time. Inference of higher kinded types is supported. However, Scala lacks any meaningful kind system, and therefore has no kind inference and no kind unification. If a type variable is introduced, it is of kind * unless annotated otherwise. Certain types like Any (the supertype of all types) and Nothing (a subtype of every type) are technically of every kind although they cannot be applied to type arguments.

Haskell to a Scala programmer:

Haskell is a purely functional language. This means that functions are not allowed to have any side-effects at all. For example, a Haskell program doesn't print to the screen as such, but is a function that returns a value of the IO[_] datatype which describes a sequence of actions for the IO subsystem to perform.

Whereas Scala is strict by default and provides "by-name" annotation for nonstrict function arguments, Haskell is lazy by default using "by-need" semantics, and provides annotation for strict arguments.

Type inference in Haskell is more complete than in Scala, having full inference. This means that type annotation is almost never necessary.

Recent extensions to the GHC compiler allow advanced type system features that have no equivalent in Scala, such as rank-n types, type families, and kinds polymorphism.

In Haskell, a module is a collection of types and functions, but modules are not first-class entities. Implicits are provided by type classes, but these are globally scoped once declared, and they cannot be passed explicitly as in Scala. Multiple instances of a type class for a given type are resolved by wrapping with a newtype to disambiguate, whereas in Scala this would be solved simply by scoping or by passing instances explicitly.

Since Haskell isn't "object-oriented", there's no method/function dichotomy. Every function is first class and every function is curried by default (no Function1, Function2, etc).

Haskell has no subtype mechanism, but type classes can have a subclass relationship.

相关问答

更多
  • 我对斯卡拉的换能器概念不熟悉,或者术语无处不在,但是从上面发布的文本片段(以及我对换能器的了解)中,我可以这么说: 他们是非常不同的 它们的相似之处仅在于它们都涉及如何将一个集合转换为另一个集合 我可以告诉Scala Transducers: 从上面的定义可以看出,任何函数或可调用的签名都大致沿着 Stream[A] -> Stream[B] 因此,例如,在这种情况下,处理流的映射函数将被视为传感器。 而已; 真的很简单。 Clojure传感器: Clojure 传感器是一种将一种减少功能转换为另一种功能 ...
  • 首先,柯里首先是一个数学概念。 对于任何n元函数f:S 0 ×... S n →R ,你可以用n-1个参数定义一个新的函数fprime (刚刚发现一个降价错误!),其中第一个参数被替换一个常数。 所以,如果你有一个函数add(a,b) ,你可以定义一个新的函数add1(b) as add1(b) ::= add(1, b) ...阅读“:: =”为“被定义为”。 闭包更像是一个编程概念。 (当然,编程中的所有内容都是一个数学概念,但由于编程,闭包变得很有趣。)当你构造一个闭包时,你绑定了一个或多个变量; 您 ...
  • 真的,据我所知,要求人们没有事实上的标准,人们对于他们喜欢但没有什么具体证据的强烈意见,除了“在大多数情况下对我来说都是有效的”。 haskell-indentation包和haskell-indent包是最受欢迎的: 都有一个制表符循环 两者都是复杂的代码库 他们都有很多边缘的情况,他们分手 每个人都会在不同的情况下做正确的事情,但是再一次,没有单元测试,没有真正的解析器等等,只是一些作者所想到的特殊情况,所以文档和理解如何表现严谨的意义上是稀疏的。 haskell-indentation了解更多Hask ...
  • 最大的区别在于Scala不具有Hindley-Milner全局类型推断,而是使用一种本地类型推理的形式,要求您指定方法参数的类型以及重载或递归函数的返回类型。 这不是由类型擦除或JVM的其他要求驱动的。 这里所有可能的困难都可以克服,一直以来,只是考虑Jaskell - http://docs.codehaus.org/display/JASKELL/Home HM推论在面向对象的上下文中不起作用。 具体来说,当使用类型多态时(与类型类的ad-hoc多态性相反)。 这对于与其他Java库的强大互操作以及(在 ...
  • Scala到Haskell程序员: Scala是一个严格和不纯的语言与一流的模块。 新类型被声明为模块(“类”/“特征”,具有微妙的差异)。 模块可以是采用通用量化类型参数的类型构造函数。 模块具有由值,可变变量和函数(称为“方法”)组成的成员,模块被隐式传递给其中称为this变量)。 模块可以具有也可以参数的类型成员。 类型成员是存在量化的,类型参数可以更高。 因为类型可以是第一类值的成员,所以Scala提供称为依赖路径的依赖类型的风格。 一流的功能也是模块。 一个函数是一个名为apply的方法。 一种方 ...
  • 我会说一个接口是类型类SomeInterface t ,其中所有的值都有类型t -> whatever ( whatever都不包含t )。 这是因为使用Java和类似语言的继承关系,所调用的方法取决于它们所调用的对象的类型,而没有其他的。 这意味着很难使用一个接口来add :: t -> t -> t ,因为在多个参数上是多态的,因为接口没有办法指定参数类型和返回类型该方法与所调用对象的类型(即“自”类型)类型相同。 使用泛型,有一些方法来伪造这个通过使用通用参数的接口,该参数预期与对象本身是一样的,就像 ...
  • 全局变量和堆变量是两个不同的概念。 堆变量只是告诉您变量的空间分配位置,而全局变量则告诉您变量的范围。 全局意味着变量对任何东西都是可见的,它与本地相对,这意味着变量的可见性受到限制。 堆表示变量(指针)是动态分配的(例如:使用malloc),并且与不动态分配变量的堆栈相对。 因此,您可以拥有全局堆变量,全局堆栈变量,本地堆变量或本地堆栈变量。 在您的情况下,globalVar是全局的,因为它在任何函数的作用域(大括号)之外声明,而heapVar是main的本地。 由于对malloc的调用,heapVar在 ...
  • 你可以在Haskell中为Num [Char]添加一个实例,如果这是你想要的: {-# LANGUAGE FlexibleInstances #-} import Data.Function (on) import Data.Composition ((.:)) -- cabal install composition helper :: (Integer -> Integer -> Integer) -> (String -> String -> String) helper op = show . ...
  • 在整个这个答案中,我将讨论Haskell 98和SML。 系统F实际上都具有相同的基础。 这意味着你有基本的参数多态性 foo :: a -> b -> a foo : 'a -> 'b -> 'c SML提供了函子和模块以及Haskell类型类,但这两者都是真正构建在核心演算之上的。 最有趣的区别是Haskell是System Fw,它是System F的强化版本。特别是,它提供了更丰富的种类(类型类型)概念,允许像 data Foo f a = Foo (f a) 注意这里f是一个从一个类型到另一个 ...
  • 这里有一些声称完整的差异列表。 维基百科有一个比较它们的页面 。 There's a list of differences that claims to be complete here. And wikipedia has a page comparing them.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)