jeudi 24 septembre 2015

usine jasmine testing on a chromebook offline

has anyone figured out how to use jasmine testing on a chromebook?

i've come across this:

http://ift.tt/1VbyzCk

which i'm not sure if it does or not. thanks!

Use Appium without starting application

I am testing an Android app with Appium but I need a way to test the application without Appium automatically trying to install and run the app. Is there a way for me to tell Appium that the app is already open?

Test controller with service injection

I am currently testing my AngularJS application and I am struggling with the tests of the controllers injecting a service.

Here is my controller:

.controller('UserCtrl', ['$scope', '$routeParams', 'User', function ($scope, $routeParams, User) {

    $scope.hello = "lol";

    $scope.userId = $routeParams.id;
    var user = new User($scope.userId);

    $scope.load = function(){
        user.load().then(
            function(user){
                $scope.name     = user.name;
            },
            function() {
                $scope.dataFail = 'error error' ;
            }
        );
    };

    $scope.load();
}]);

Here is my User service (that i want to inject in my controller's test)

.factory('user', ['$http', '$q', function ($http, $q) {

var User = function (id) {
    this.id = id;
    this.name = null;
};

User.prototype.load = function () {
    var deferred = $q.defer();

    $http.post('/user/' + this.id + '/load')
        .success(function(data) {
            var user = new User(data.id);
            user.name = data.name
            deferred.resolve(user);
        })
        .error(function(err, code) {
            deferred.reject(err);
        $log.error(err, code);
    });
    return deferred.promise;
};

return User;

And finally is my test :

describe('Controller: UserCtrl', function () {

var $controller, $scope, User;

beforeEach(module('octosendApp', function($provide){
    User = jasmine.createSpyObj('User', ['load']);

    User.load.and.returnValue({
        name: 'testName'
    });
    $provide.value('User', User);
}));

beforeEach(inject(function(_$controller_, $rootScope, _User_){
    $scope = $rootScope.$new();
    User = _User_;

    $controller = _$controller_('UserCtrl', {
        $scope: $scope,
        User : User,
        $routeParams: {id: 2}
    });
}));

it('should have a hello property', function() {
    expect($scope.hello).toBeDefined();
});

});

I get the following error :

TypeError: '[object Object]' is not a constructor (evaluating 'new User($scope.userId)')

I guess the problem is because my factory is object oriented and i can not use the constructor. i followed this tutoarial : http://ift.tt/1iOgyZQ and did the exact same, the only difference is the structure of the factory, which I do NOT want to change.

Thank you for your help.

Should model classes be tested?

Should model classes be tested?

Model classes does not contain logic, but usually contains some logic in the constructor for initialization.

These class are also shared, therefore it a very important that these class initializes correctly.

What is the standard for testing, should model classes be tested? Or the class that make use of them have responsibility to test them?

Check if file was created and deleted?

I need to test a program that creates temporary file. When run finishes it deletes the file. How can I check it file has be created and deleted.

I am thinking about sending some signals to process (like Ctrl-Z) to suspend it and check but should be simpler ways.

I am using bash in Linux.

play framework share testing code between subprojects

i have a play project with multiple subprojects. It is possible to share classes from the app folder between subprojects which depends on each other.

I have a class with methods which should be executed before every test. I need this class for nearly every test class and it isn't practicable to have this class in every test folder for each subproject. Is it generally possible to share classes in the test folder?

I have tested this but it doesn't seem to work. IntelliJ doesn't show any error and it seems to recognize the class i want to use. But when i execute the tests, it shows me that it can't find the class.

Thanks in advance for your responses.

Spring integration test with mockmvc throws org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported

I have a weird problem with a spring integrationtest. I have a working controller and application that runs on tomcat. However in my integration test I get the following stacktrace:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.readWithMessageConverters(HandlerMethodInvoker.java:651)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveRequestBody(HandlerMethodInvoker.java:610)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:359)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:173)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:442)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:430)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:688)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:156)
at com.test......
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:497)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:85)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:86)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:241)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
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:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

My test looks like this:

@Category(DbIntegrationTest.class)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={RootConfig.class})
@WebAppConfiguration
@ActiveProfiles({"dev", "fk_mock"})
public class WhenUserSubmitsNew  {

@Autowired private WebApplicationContext context;
protected MockMvc mvc;

@Before
public void setup() {
    buildMockMVC();
    createBasicUserSetup();
}

public void buildMockMVC() {

    mvc = MockMvcBuilders
            .webAppContextSetup(context)
            //.apply(springSecurity())
            .build();
}

public void createBasicUserSetup() {
    ...
}


@Test
public void shouldCreate() throws Exception {
    String date1 = "2015-05-05";
    String date2 = "2015-07-30";

    String json = "{\"ids\": [5, 6]}";

    String url = "/submitNew";
    mvc.perform(post(url)
            .with(user(user.getUserName()))
            .sessionAttr(AbstractController.USER_SESSION_ATTR, user)
            .contentType(MediaType.APPLICATION_JSON)
            .accept(MediaType.APPLICATION_JSON)
            .content(json))
        .andExpect(status().isCreated());
}
} 

And my controller method:

    @RequestMapping(value="/submitNew", method=RequestMethod.POST)
public @ResponseBody Long create(HttpSession session, @RequestBody CustomForm form) {
    ...
}

my gradle.build

apply plugin: 'war'
apply plugin: 'eclipse-wtp'
group = '...'
version = '1.0'
sourceCompatibility = 1.8
targetCompatibility = 1.8
ext {
   springVersion = '4.2.0.RELEASE'
   springSecurityVersion = '4.0.2.RELEASE'
   springWSVersion = '2.2.2.RELEASE'
   aspectjVersion = '1.6.9'
   slf4jVersion = '1.7.5'
   tilesVersion = '3.0.5'
   jsonPathVersion = '0.8.1'
   servletApiVersion = '3.0.1'
   jacksonDatabindVersion = '2.6.1'
   h2Version = '1.3.156'
   postgresqlVersion = '9.3-1100-jdbc4'
   hibEntitymanagerVersion = '4.3.8.Final'
   //hibValidatorVersion = '4.1.0.Final'
   javaxInjectVersion = '1'
   dbcpVersion = '1.4'
   hamcrestVersion = '1.3'
   junitVersion = '4.11'
   mockitoCoreVersion = '1.9.5'
   jstlVersion = '1.2'
   slf4jAPI     = "org.slf4j:slf4j-api:${slf4jVersion}"
   slf4jJCL     = "org.slf4j:jcl-over-slf4j:${slf4jVersion}"
   slf4jLog4J   = "org.slf4j:slf4j-log4j12:${slf4jVersion}"
   log4JVersion = '1.2.16'
   log4J        = "log4j:log4j:${log4JVersion}"
}
repositories {
   mavenCentral()
}
dependencies {
    providedCompile "javax.servlet:javax.servlet-api:${servletApiVersion}"  
    //finns i båda build
     compile ("org.springframework:spring-webmvc:${springVersion}") {
        exclude group: 'commons-logging', module: 'commons-logging'
   }
   compile "org.springframework:spring-webmvc:${springVersion}"
   compile "org.springframework:spring-orm:${springVersion}"
   compile "org.springframework.security:spring-security-web:${springSecurityVersion}"
   compile "org.springframework.security:spring-security-config:${springSecurityVersion}"
   compile "org.springframework.ws:spring-ws-core:${springWSVersion}"
   compile "org.springframework.security:spring-security-crypto:${springSecurityVersion}"
//   compile 'org.springframework.data:spring-data-jpa:1.9.0.RELEASE'
   compile "com.fasterxml.jackson.core:jackson-databind:${jacksonDatabindVersion}"
   //compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.6.1"
   //compile "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.6.1"
   compile "org.apache.tiles:tiles-extras:${tilesVersion}"
   compile "org.apache.tiles:tiles-jsp:${tilesVersion}"
   compile "com.h2database:h2:${h2Version}"
   compile "org.postgresql:postgresql:${postgresqlVersion}"
   compile "org.hibernate:hibernate-entitymanager:${hibEntitymanagerVersion}"
//   compile "org.hibernate:hibernate-validator:${hibValidatorVersion}"
   compile "javax.inject:javax.inject:${javaxInjectVersion}"
   compile "commons-dbcp:commons-dbcp:${dbcpVersion}"
   testCompile "org.hamcrest:hamcrest-all:${hamcrestVersion}"
   testCompile "junit:junit:${junitVersion}"
   testCompile "org.springframework:spring-test:${springVersion}"
   testCompile "org.mockito:mockito-core:${mockitoCoreVersion}"
   testCompile "com.jayway.jsonpath:json-path:${jsonPathVersion}"
   testCompile "com.jayway.jsonpath:json-path-assert:${jsonPathVersion}"
   testCompile "org.springframework.security:spring-security-test:${springSecurityVersion}"
   runtime "javax.servlet:jstl:${jstlVersion}"
      // Logging:
   compile(slf4jAPI)
   runtime(
       slf4jJCL, slf4jLog4J
   )
   runtime(log4J) {
        exclude group: 'javax.mail',   module: 'mail'
        exclude group: 'javax.jms',    module: 'jms'
        exclude group: 'com.sun.jdmk', module: 'jmxtools'
        exclude group: 'com.sun.jmx',  module: 'jmxri'
   }
}

To make things more interesting I also have a unit test for the controller that works which uses standalone setup:

mockMvc = standaloneSetup(controller)
            .setMessageConverters(new MappingJackson2HttpMessageConverter())
            .build();

If anyone has any suggestions on how to move forward it would be extremely appreciated!!

Thanks Karl