Testing in leiningen - unit-testing

I am writing a small Clojure project using leiningen with the following directory structure:
project
+ src
+ org/example/project/core.clj
+ test
+ org/example/project/core.clj
When I run lein test, it reports that it ran 0 tests with 0 failures, but I purposely put a test designed to fail in the test/.../core.clj file.
I added the line :test-path test to my project.clj file and when I re-ran, got the error:
Exception in thread "main" java.lang.IllegalArgumentException: No implementation of method: :as-file of protocol: #'clojure.java.io/Coercions found for class: clojure.lang.Symbol (NO_SOURCE_FILE:0)
at clojure.lang.Compiler.eval(Compiler.java:5440)
at clojure.lang.Compiler.eval(Compiler.java:5391)
at clojure.core$eval.invoke(core.clj:2382)
at clojure.main$eval_opt.invoke(main.clj:235)
at clojure.main$initialize.invoke(main.clj:254)
at clojure.main$script_opt.invoke(main.clj:270)
at clojure.main$main.doInvoke(main.clj:354)
at clojure.lang.RestFn.invoke(RestFn.java:458)
at clojure.lang.Var.invoke(Var.java:377)
at clojure.lang.AFn.applyToHelper(AFn.java:174)
at clojure.lang.Var.applyTo(Var.java:482)
at clojure.main.main(main.java:37)
Caused by: java.lang.IllegalArgumentException: No implementation of method: :as-file of protocol: #'clojure.java.io/Coercions found for class: clojure.lang.Symbol
at clojure.core$_cache_protocol_fn.invoke(core_deftype.clj:471)
at clojure.java.io$fn__7328$G__7323__7333.invoke(io.clj:34)
at clojure.java.io$file.invoke(io.clj:401)
at leiningen.util.ns$namespaces_in_dir.invoke(ns.clj:38)
at leiningen.test$test.doInvoke(test.clj:45)
at clojure.lang.RestFn.invoke(RestFn.java:411)
at clojure.lang.Var.invoke(Var.java:365)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:482)
at clojure.core$apply.invoke(core.clj:542)
at leiningen.core$apply_task.invoke(core.clj:156)
at leiningen.core$_main.doInvoke(core.clj:212)
at clojure.lang.RestFn.applyTo(RestFn.java:138)
at clojure.core$apply.invoke(core.clj:540)
at leiningen.core$_main.invoke(core.clj:217)
at user$eval167.invoke(NO_SOURCE_FILE:1)
at clojure.lang.Compiler.eval(Compiler.java:5424)
... 11 more
I am running Clojure 1.2 and the latest lein (1.3.1).
Looks like a problem with either clojure.core or the lein code.

Had the same issue.
After a while I noticed that leins default path to tests are:
test/{project-name}/test/{file-under-test}.clj
^^^^
Note an extra test directory in the path.

As for the source layout. I think you basically shadow your test with the real core.clj file. That's probably why clojure.test doesn't find any tests. Did you try a different namespace and file name? eg. test/example/project/test_core.clj.

Related

Luminus and Re-Frame Uberjar Error

I've got a small app generated by lein new luminus <app-name> +jetty +mongodb +re-frame +cider that errors when running the uberjar command. It works as a dev app run via lein repl.
I've seen similar errors being put down to a dependency mismatch as a result of using Datomic, but that's currently not a dependency in my project.
Here's the stacktrace generated by a call to lein uberjar:
See https://github.com/technomancy/leiningen/wiki/Repeatability)
Retrieving org/clojure/core.async/0.2.374/core.async-0.2.374.jar from central
Retrieving org/clojure/tools.analyzer.jvm/0.6.9/tools.analyzer.jvm-0.6.9.jar from central
Retrieving org/clojure/tools.analyzer/0.6.7/tools.analyzer-0.6.7.jar from central
Compiling myapp.env
Compiling myapp.elastic.common
Compiling myapp.elastic.indexer
Compiling myapp.web.config
Compiling myapp.web.core
Compiling myapp.web.db.core
Compiling myapp.web.handler
Compiling myapp.web.layout
Compiling myapp.web.middleware
Compiling myapp.web.routes.home
Compiling myapp.validation
Compiling ClojureScript...
Retrieving cljsbuild/cljsbuild/1.1.4/cljsbuild-1.1.4.pom from clojars
Retrieving org/clojure/clojurescript/0.0-3211/clojurescript-0.0-3211.pom from central
Retrieving org/clojure/clojure/1.7.0-beta1/clojure-1.7.0-beta1.pom from central
Retrieving cljsbuild/cljsbuild/1.1.4/cljsbuild-1.1.4.jar from clojars
Compiling "target/cljsbuild/public/js/app.js" from ["src/cljc" "src/cljs" "env/prod/cljs"]...
Compiling "target/cljsbuild/public/js/app.js" failed.
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.CharMatcher.javaUpperCase()Lcom/google/common/base/CharMatcher;, compiling:(/private/var/folders/ms/7lyl677n0zz5dnw0wxffj8j00000gn/T/form-init4762266950288731870.clj:1:125)
at clojure.lang.Compiler.load(Compiler.java:7391)
at clojure.lang.Compiler.loadFile(Compiler.java:7317)
at clojure.main$load_script.invokeStatic(main.clj:275)
at clojure.main$init_opt.invokeStatic(main.clj:277)
at clojure.main$init_opt.invoke(main.clj:277)
at clojure.main$initialize.invokeStatic(main.clj:308)
at clojure.main$null_opt.invokeStatic(main.clj:342)
at clojure.main$null_opt.invoke(main.clj:339)
at clojure.main$main.invokeStatic(main.clj:421)
at clojure.main$main.doInvoke(main.clj:384)
at clojure.lang.RestFn.invoke(RestFn.java:421)
at clojure.lang.Var.invoke(Var.java:383)
at clojure.lang.AFn.applyToHelper(AFn.java:156)
at clojure.lang.Var.applyTo(Var.java:700)
at clojure.main.main(main.java:37)
Caused by: java.lang.NoSuchMethodError: com.google.common.base.CharMatcher.javaUpperCase()Lcom/google/common/base/CharMatcher;
at com.google.javascript.jscomp.parsing.JsDocInfoParser.validTemplateTypeName(JsDocInfoParser.java:1233)
at com.google.javascript.jscomp.parsing.JsDocInfoParser.parseAnnotation(JsDocInfoParser.java:953)
at com.google.javascript.jscomp.parsing.JsDocInfoParser.parseHelperLoop(JsDocInfoParser.java:290)
at com.google.javascript.jscomp.parsing.JsDocInfoParser.parse(JsDocInfoParser.java:280)
at com.google.javascript.jscomp.parsing.IRFactory.createJsDocInfoParser(IRFactory.java:910)
at com.google.javascript.jscomp.parsing.IRFactory.handleJsDoc(IRFactory.java:692)
at com.google.javascript.jscomp.parsing.IRFactory.handleJsDoc(IRFactory.java:706)
at com.google.javascript.jscomp.parsing.IRFactory.transform(IRFactory.java:766)
at com.google.javascript.jscomp.parsing.IRFactory$TransformDispatcher.processAstRoot(IRFactory.java:1024)
at com.google.javascript.jscomp.parsing.IRFactory$TransformDispatcher.process(IRFactory.java:2712)
at com.google.javascript.jscomp.parsing.IRFactory.transformTree(IRFactory.java:356)
at com.google.javascript.jscomp.parsing.ParserRunner.parse(ParserRunner.java:129)
at com.google.javascript.jscomp.JsAst.parse(JsAst.java:142)
at com.google.javascript.jscomp.JsAst.getAstRoot(JsAst.java:52)
at com.google.javascript.jscomp.CompilerInput.getAstRoot(CompilerInput.java:118)
at com.google.javascript.jscomp.Compiler.parseInputs(Compiler.java:1443)
at com.google.javascript.jscomp.Compiler.parse(Compiler.java:775)
at com.google.javascript.jscomp.Compiler.compileInternal(Compiler.java:731)
at com.google.javascript.jscomp.Compiler.access$000(Compiler.java:83)
at com.google.javascript.jscomp.Compiler$2.call(Compiler.java:698)
at com.google.javascript.jscomp.Compiler$2.call(Compiler.java:695)
at com.google.javascript.jscomp.CompilerExecutor$2.call(CompilerExecutor.java:93)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Uberjar aborting because jar failed: Subprocess failed
I solved a similar problem by adding [com.google.guava/guava "21.0"] to the :dependencies in project.clj
Okay, so the clue was in the dependency mismatch. Although lein deps :tree didn't show anything awry, the issue was with monger depending on com.google.guava/guava.
By adding :exclusions [com.google.guava/guava] to each library that depends on guava (including monger) in the project.clj and moving org.clojure/clojurescript to the top of the dependency list, I was able to get the build working, as the correct version of guava for the version of Clojurescript was used, I suppose.

AmazonServiceException class not found

I am using AWS SDK to upload files to S3 bucket when set java classpath to "aws-java-sdk-1.10.49.jar", the program works fine, but when replacing jar file to a later version "aws-java-sdk-1.11.35.jar", I get an exception "AmazonServiceException class not found as below."
Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/AmazonServiceException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.AmazonServiceException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
any help?
here we have three possible problems:
A simple example of NoClassDefFoundError is class belongs to a missing JAR file or JAR was not added into classpath or sometimes jar's name has been changed by someone like in my case one of my colleagues has changed tibco.jar into tibco_v3.jar and the program is failing with java.lang.NoClassDefFoundError and I were wondering what's wrong.
The class is not in Classpath, there is no sure shot way of knowing it but many times you can just have a look to print System.getproperty("java.classpath") and it will print the classpath from there you can at least get an idea of your actual runtime classpath.
Just try to run with explicitly -classpath option with the classpath you think will work and if it's working then it's a sure short sign that someone is overriding java classpath.
Take a look at this article

Lein Midje :autotest throwing exception (No namespace found)

I am trying to setup autotest for midje. Currently, running lein midje works. However running lein midje :autottest gives me the stack trace below.
I run through my different namespaces and they all compile
Compiling 1 source files to /Users/oloo/Documents/work/cmp/target/classes
warning: Supported source version 'RELEASE_6' from annotation processor 'org.sonatype.guice.bean.scanners.index.SisuIndexAPT6' less than -source '1.7'
1 warning
======================================================================
Loading (cmp.models cmp.util cmp.repository.orderdeliveryschedule-repository cmp.repository.facility-repository cmp.repository.facility-cycle-data-repository cmp.views.layout cmp.routes.facility cmp.repository.item-repository cmp.repository.procurement-plan-repository cmp.routes.login cmp.excel-util cmp.repository.district-repository cmp.repository.facility-order-repository cmp.repository.facility-issue-repository cmp.routes.import-data cmp.test-util cmp.routes.item cmp.routes.district cmp.repository.user-repository cmp.routes.reports cmp.repository.category-repository cmp.routes.facility-issue cmp.routes.procurement-plan cmp.routes.facility-order cmp.handler cmp.repl cmp.repository.level-repository)
log4j:WARN No appenders could be found for logger (com.mchange.v2.log.MLog).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.Exception: No namespace: cmp.routes.item found
at clojure.core$the_ns.invoke(core.clj:3760)
at clojure.core$ns_name.invoke(core.clj:3767)
at midje.repl$on_require_failure.invoke(repl.clj:387)
at midje.data.project_state$require_namespaces_BANG_$broken_source_file_QMARK___5874.invoke(project_state.clj:65)
at midje.data.project_state$require_namespaces_BANG_$shorten_ns_list_by_trying_first__5877.invoke(project_state.clj:69)
at midje.data.project_state$require_namespaces_BANG_.invoke(project_state.clj:75)
at midje.data.project_state$react_to_tracker_BANG_$fn__5889.invoke(project_state.clj:98)
at midje.repl$namespace_stream_checker.invoke(repl.clj:403)
at midje.data.project_state$react_to_tracker_BANG_.invoke(project_state.clj:94)
at midje.data.project_state$mkfn_COLON_scan_and_react$fn__5893$fn__5894.invoke(project_state.clj:109)
at clojure.lang.Atom.swap(Atom.java:37)
at clojure.core$swap_BANG_.invoke(core.clj:2160)
at midje.data.project_state$mkfn_COLON_scan_and_react$fn__5893.invoke(project_state.clj:107)
at midje.data.project_state$load_everything.invoke(project_state.clj:117)
at midje.repl$autotest.doInvoke(repl.clj:511)
at clojure.lang.RestFn.invoke(RestFn.java:397)
at midje.repl$autotest.doInvoke(repl.clj:524)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at user$eval6032.invoke(NO_SOURCE_FILE:1)
at clojure.lang.Compiler.eval(Compiler.java:6619)
at clojure.lang.Compiler.eval(Compiler.java:6609)
at clojure.lang.Compiler.eval(Compiler.java:6582)
at clojure.core$eval.invoke(core.clj:2852)
at clojure.main$eval_opt.invoke(main.clj:308)
at clojure.main$initialize.invoke(main.clj:327)
at clojure.main$null_opt.invoke(main.clj:362)
at clojure.main$main.doInvoke(main.clj:440)
at clojure.lang.RestFn.invoke(RestFn.java:421)
at clojure.lang.Var.invoke(Var.java:419)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:532)
at clojure.main.main(main.java:37)
Subprocess failed
See this: https://github.com/marick/Midje/issues/215
It says:
If a file being loaded by Midje at startup has a parse error (a reference to an unknown
symbol, etc.) that prevents it from being loaded successfully, Midje swallows the original
load error and instead prints the following error, which is much less useful.
I have suffered from this too: any little typo brings up this misleading error message.
I remember having problems with autotest and namespaces that :require or :use other namespaces using the [prefix ns1 ns2 ...] form. So, the following did not work:
(ns abc
(:use [abc def ghi]))
while this was okay:
(ns abc
(:use abc.def
abc.ghi))
Are you using that kind of syntax?

Getting an exception when trying to run lein

I get the following exception when trying to run lein in linux.
Exception in thread "main" java.lang.NoClassDefFoundError: clojure.core.protocols$fn__5393
at java.lang.Class.initializeClass(libgcj.so.10)
at clojure.core.protocols__init.load(Unknown Source:16)
at clojure.core.protocols__init.<clinit>(Unknown Source)
at java.lang.Class.initializeClass(libgcj.so.10)
at java.lang.Class.forName(libgcj.so.10)
at clojure.lang.RT.loadClassForName(RT.java:1578)
at clojure.lang.RT.load(RT.java:399)
at clojure.lang.RT.load(RT.java:381)
at clojure.core$load$fn__4519.invoke(core.clj:4915)
at clojure.core$load.doInvoke(core.clj:4914)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.core__init.load(Unknown Source:5342)
at clojure.core__init.<clinit>(Unknown Source)
at java.lang.Class.initializeClass(libgcj.so.10)
at java.lang.Class.forName(libgcj.so.10)
at clojure.lang.RT.loadClassForName(RT.java:1578)
at clojure.lang.RT.load(RT.java:399)
at clojure.lang.RT.load(RT.java:381)
at clojure.lang.RT.doInit(RT.java:416)
at clojure.lang.RT.<clinit>(RT.java:302)
at java.lang.Class.initializeClass(libgcj.so.10)
at clojure.main.<clinit>(main.java:20)
at java.lang.Class.initializeClass(libgcj.so.10)
at java.lang.Class.forName(libgcj.so.10)
at java.lang.Class.forName(libgcj.so.10)
at jline.ConsoleRunner.main(ConsoleRunner.java:69)
Caused by: java.lang.VerifyError: verification failed at PC 214 in
clojure.core.protocols$fn__5393:invoke((Ljava.lang.Object;Ljava.lang.Object;Ljava.lang.Obje ct;)Ljava.lang.Object;): incompatible type on stack
at java.lang.Class.initializeClass(libgcj.so.10)
...25 more
I've followed the instructions on their github page, but no luck. I thought it would be fun to start learning clojure today but instead I've had nothing but problems, I don't even know what leiningen is, other than I apparently have to have it.
sorry about the frusteration,
Leiningen is a build tool to take the pain out of getting hacking on a new clojure project.
Before leiningen existed many people would try to "install" clojure by downloading a jar file out build it from source. Instead of doing any of that you just specify the version you want to use in the leiningen project.clj file and it installs it locally as a dependency for that project.
have you run:
lein new hello_clojure
cd hello_clojure
edit project.clj and set it to use clojure 1.3.0 :dependencies [[org.clojure/clojure "1.3.0"]
lein deps
lein repl
(+ 1 2 3 4)
When things go wrong with leiningen I generally:
lein upgrade
rm lib/*
lein deps
Problems like this are uncommon (for me) and usually fairly specific, so perhaps you would do well to drop into #clojure on irc.freenode.net and ask. It's a very friendly croud dont worry.

installing compojure

Following the instructions in http://en.wikibooks.org/wiki/Compojure/Getting_Started, I:
downloaded http://github.com/weavejester/compojure/tarball/0.3.2
extracted to ~/compojure
chdir to there
changed permission to executable
run ant deps
ant
run export CLASSPATH=~/compojure/compojure.jar
created compojure shell script
Clojure source file
chdir to there
run ./compojure hello.clj
Then, I got:
Exception in thread "main" java.lang.NoClassDefFoundError: clojure/lang/Script
Caused by: java.lang.ClassNotFoundException: clojure.lang.Script
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: clojure.lang.Script. Program will exit.
I tried changing Compojure shell script to use full classpath, but still didn't work.
Why not? How to install Compojure?
That page is outdated.
You should follow these instructions from compojure developers:
https://github.com/weavejester/compojure/wiki/Getting-Started